From 5871aa041f35a8c7211be1659341856f252a2b26 Mon Sep 17 00:00:00 2001 From: Sarthak Tripathi Date: Sun, 1 Oct 2017 05:55:15 +0530 Subject: [PATCH 01/10] minor edits/fixes + Linear search + Improved multiplication for compatibility with more systems (10^6 is not supported in all systems) --- .../multiply 2 numbers greater than 10^5.cpp | 43 +++++++++++++++++++ seaching algorithms/LINEAR_SEARCH | 31 +++++++++++++ seaching algorithms/linear.cpp | 13 ++++++ 3 files changed, 87 insertions(+) create mode 100644 maths/multiplication/multiply 2 numbers greater than 10^5.cpp create mode 100644 seaching algorithms/LINEAR_SEARCH create mode 100644 seaching algorithms/linear.cpp diff --git a/maths/multiplication/multiply 2 numbers greater than 10^5.cpp b/maths/multiplication/multiply 2 numbers greater than 10^5.cpp new file mode 100644 index 00000000..b4211795 --- /dev/null +++ b/maths/multiplication/multiply 2 numbers greater than 10^5.cpp @@ -0,0 +1,43 @@ +/* +This program can be used to multiply 2 numbers grater than 10^5, usually a major hurdle in languages like C/C++ +in competitive programming, using this as a template one can perform implementations like Factorials of 100 etc +*/ + +#include +using namespace std; +int main() + { + ios_base::sync_with_stdio(false); + int n1, n2; + cin>>n1>>n2; + int c = 0, ans[100000] = {0}, i = 0, basei = 0, temp = n2, dig = 0, tmp; + while(n1) + { + if(n2) + { + c = ( (n1%10)*(n2%10) ); + ans[i] += c; + if(ans[i]>9) + { + ans[i+1] += ans[i]/10; + ans[i] = ans[i]%10; + } + c /= 10; + n2 /= 10; + dig++; + i++; + } + else + { + tmp = dig; + n2 = temp; + n1 /= 10; + i = ++basei; + c=0; + dig = basei; + } + } + for(int k = tmp-1 ;k>=0;k--) + cout< +using namespace std; + + +int main(){ + + int N,M; + + cin>>N; + int a[N]; + for(int i=0;i>a[i]; + } + ///Take input the element to search + cin>>M; + + ///Linear Search + int i; + + for(i=0;i +using namespace std; +int main() +{ + int array [] = {1,2,3,4,5,6,7,8,9,10}, search = 5, position; + for(position = 0 ; position < 10 ; position++) + if(search == array[position]) + cout< Date: Sun, 1 Oct 2017 06:02:21 +0530 Subject: [PATCH 02/10] added convexHull instead of floyd warshall --- maths/convexHull/ConvexHullScan.cpp | 96 ++++++++++++++++++++++++++++ maths/floydWarshal/FloydWarshall.cpp | 43 ------------- 2 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 maths/convexHull/ConvexHullScan.cpp delete mode 100644 maths/floydWarshal/FloydWarshall.cpp diff --git a/maths/convexHull/ConvexHullScan.cpp b/maths/convexHull/ConvexHullScan.cpp new file mode 100644 index 00000000..019532a5 --- /dev/null +++ b/maths/convexHull/ConvexHullScan.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +using namespace std; + +//This function decides if 3 points form a right turn or not +bool Is_Right_Turn(vector > Hull){ + pair P1, P2, P3; + int Determinant; + + //P1, P2 and P3 are the last 3 points in the given Hull + P1 = Hull[Hull.size() - 3]; + P2 = Hull[Hull.size() - 2]; + P3 = Hull[Hull.size() - 1]; + + /*Cross Product is given by the determinant of the following 3x3 matrix - + 1 P1.x P1.y + 1 P2.x P2.y + 1 P3.x P3.y + */ + Determinant = ((P2.first - P1.first) * (P3.second - P1.second) - (P2.second - P1.second) * (P3.first - P1.first)); + + //Points form right turn only if the determinant is less than 0 (Equal to zero means colinear) + return (Determinant < 0); +} + + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int Number_of_Points, X, Y, i; + + vector > Points, Upper_Hull, Lower_Hull; + + set > Convex_Hull; + + cin>>Number_of_Points; + + //Enter the points as (X,Y) coordinates + for(i = 0; i < Number_of_Points; i++){ + cin>>X>>Y; + Points.push_back(make_pair(X,Y)); + } + + //Sort the points first by X coordinate and then by Y coordinate + sort(Points.begin(),Points.end()); + + //Form the upper half of convex hull + Upper_Hull.push_back(Points[0]); + Upper_Hull.push_back(Points[1]); + + for(i = 2; i < Number_of_Points; i++){ + Upper_Hull.push_back(Points[i]); + while(Upper_Hull.size() >= 3){ + if(Is_Right_Turn(Upper_Hull)) + break; + else + //Remove the middle point from the hull + Upper_Hull.erase(Upper_Hull.end() - 2); + } + } + + //Form the lower half of the hull + Lower_Hull.push_back(Points[Number_of_Points - 1]); + Lower_Hull.push_back(Points[Number_of_Points - 2]); + + for(i = Number_of_Points-3; i>= 0; i--){ + Lower_Hull.push_back(Points[i]); + while(Lower_Hull.size() >= 3){ + if(Is_Right_Turn(Lower_Hull)) + break; + else + //Remove the middle point from the hull + Lower_Hull.erase(Lower_Hull.end() - 2); + } + } + + //Merge the Upper and Lower Hulls + for(auto &upper:Upper_Hull) + Convex_Hull.insert(upper); + for(auto &lower:Lower_Hull) + Convex_Hull.insert(lower); + + //Print the Convex Hull + for(auto &point:Convex_Hull) + cout<<"("< -#define FOR(i,n) for(size_t i=0;i>v>>e; - FOR(i,v){ - FOR(j,v) - { - if(i!=j) - floyd_warshall[i][j] = 99999999; - else floyd_warshall[i][j] = 0; - } - } - - FOR(i,e){ - cin>>a>>b>>weight; - floyd_warshall[a][b] = weight; - } - - FOR(k,v){ - FOR(i,v){ - FOR(j,v){ - temp = floyd_warshall[i][k] + floyd_warshall[k][j]; - if(temp < floyd_warshall[i][j]) - floyd_warshall[i][j] = temp; - } - } - } - cout< Date: Sun, 1 Oct 2017 06:12:27 +0530 Subject: [PATCH 03/10] added binary search --- .../multiply 2 numbers greater than 10^5.cpp | 43 ------------------- seaching algorithms/LINEAR_SEARCH | 31 ------------- seaching algorithms/binary_search.cpp | 32 ++++++++++++++ 3 files changed, 32 insertions(+), 74 deletions(-) delete mode 100644 maths/multiplication/multiply 2 numbers greater than 10^5.cpp delete mode 100644 seaching algorithms/LINEAR_SEARCH create mode 100644 seaching algorithms/binary_search.cpp diff --git a/maths/multiplication/multiply 2 numbers greater than 10^5.cpp b/maths/multiplication/multiply 2 numbers greater than 10^5.cpp deleted file mode 100644 index b4211795..00000000 --- a/maths/multiplication/multiply 2 numbers greater than 10^5.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* -This program can be used to multiply 2 numbers grater than 10^5, usually a major hurdle in languages like C/C++ -in competitive programming, using this as a template one can perform implementations like Factorials of 100 etc -*/ - -#include -using namespace std; -int main() - { - ios_base::sync_with_stdio(false); - int n1, n2; - cin>>n1>>n2; - int c = 0, ans[100000] = {0}, i = 0, basei = 0, temp = n2, dig = 0, tmp; - while(n1) - { - if(n2) - { - c = ( (n1%10)*(n2%10) ); - ans[i] += c; - if(ans[i]>9) - { - ans[i+1] += ans[i]/10; - ans[i] = ans[i]%10; - } - c /= 10; - n2 /= 10; - dig++; - i++; - } - else - { - tmp = dig; - n2 = temp; - n1 /= 10; - i = ++basei; - c=0; - dig = basei; - } - } - for(int k = tmp-1 ;k>=0;k--) - cout< -using namespace std; - - -int main(){ - - int N,M; - - cin>>N; - int a[N]; - for(int i=0;i>a[i]; - } - ///Take input the element to search - cin>>M; - - ///Linear Search - int i; - - for(i=0;i +using namespace std; + +int binarySearch(int array[], int left, int right, int search) +{ + if (right >= 1) + { + int mid = 1 + (right - 1)/2; + if (array[mid] == search) //Checks if the element is present at the middle + return mid; + if (array[mid] > search) //Since this is a sorted array, therefore if value of element is less than middle + return binarySearch(array, left, mid-1, search); //element, it lies in left part of array. Recursively searching again the left part. + + return binarySearch(array, mid+1, right, search); //If element was neither in Middle nor in left part, it can only be in the right. + } + + return -1; //If element was not found, return -1 +} +int main() +{ + int array [] = {1,2,3,4,5,6,7,8,9,10}, search = 5, position; + int size = sizeof(array)/ sizeof(array[0]); + int result = binarySearch(array, 0, size-1, search); //here left index for binary search is sent as 0, while right as size - 1 + if(result == -1) //therefore for an array of size 10, 9 will be sent (since arrays are 0 indexed) + cout<<"Element is not present in array"; + else + cout<<"Element is present at "< Date: Sun, 1 Oct 2017 06:13:05 +0530 Subject: [PATCH 04/10] minor fixes --- Searching Algorithms/LINEAR_SEARCH | 31 ------------------- .../multiply 2 numbers greater than 10^6.cpp | 4 +-- 2 files changed, 2 insertions(+), 33 deletions(-) delete mode 100644 Searching Algorithms/LINEAR_SEARCH diff --git a/Searching Algorithms/LINEAR_SEARCH b/Searching Algorithms/LINEAR_SEARCH deleted file mode 100644 index bfaa06f7..00000000 --- a/Searching Algorithms/LINEAR_SEARCH +++ /dev/null @@ -1,31 +0,0 @@ - -#include -using namespace std; - - -int main(){ - - int N,M; - - cin>>N; - int a[N]; - for(int i=0;i>a[i]; - } - ///Take input the element to search - cin>>M; - - ///Linear Search - int i; - - for(i=0;i>n1>>n2; - int c = 0, ans[1000000] = {0}, i = 0, basei = 0, temp = n2, dig = 0, tmp; + int c = 0, ans[100000] = {0}, i = 0, basei = 0, temp = n2, dig = 0, tmp; while(n1) { if(n2) From b54245b974843147f6fbf8bd4460721b70141329 Mon Sep 17 00:00:00 2001 From: Sarthak Tripathi Date: Sun, 1 Oct 2017 06:26:27 +0530 Subject: [PATCH 05/10] fixed errors --- seaching algorithms/binary_search.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/seaching algorithms/binary_search.cpp b/seaching algorithms/binary_search.cpp index 0d08441e..4999d967 100644 --- a/seaching algorithms/binary_search.cpp +++ b/seaching algorithms/binary_search.cpp @@ -8,7 +8,7 @@ int binarySearch(int array[], int left, int right, int search) { if (right >= 1) { - int mid = 1 + (right - 1)/2; + int mid = ((right-1)-left)/2; if (array[mid] == search) //Checks if the element is present at the middle return mid; if (array[mid] > search) //Since this is a sorted array, therefore if value of element is less than middle @@ -23,10 +23,10 @@ int main() { int array [] = {1,2,3,4,5,6,7,8,9,10}, search = 5, position; int size = sizeof(array)/ sizeof(array[0]); - int result = binarySearch(array, 0, size-1, search); //here left index for binary search is sent as 0, while right as size - 1 - if(result == -1) //therefore for an array of size 10, 9 will be sent (since arrays are 0 indexed) + position = binarySearch(array, 0, size-1, search); //here left index for binary search is sent as 0, while right as size - 1 + if(position == -1) //therefore for an array of size 10, 9 will be sent (since arrays are 0 indexed) cout<<"Element is not present in array"; else - cout<<"Element is present at "< Date: Sat, 30 Sep 2017 20:57:52 -0400 Subject: [PATCH 06/10] Create PrimeCheck.py --- maths/prime/PrimeCheck.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 maths/prime/PrimeCheck.py diff --git a/maths/prime/PrimeCheck.py b/maths/prime/PrimeCheck.py new file mode 100644 index 00000000..97ee7670 --- /dev/null +++ b/maths/prime/PrimeCheck.py @@ -0,0 +1,19 @@ +#Simple way to check for primes using Pyton. + +#Prompt user for a integer +Number = int(input("Enter any integer to check if prime number ")) +#Set starting divisor to 2 +Divisor = 2 +for Divisor in range (Divisor, Number) : #Test every number between 2 and the integer + if Number % Divisor == 0: + prime = False # Tell computer to make prime false + break #End loop + Divisor += 1 #Add 1 to the divisor + +else: + prime = True # If number % divisor does not equal 0, number must be a prime, so mark as true +#Print results +if prime == True : + print("Your number is a prime") +if prime == False : + print("Your number is not a prime") From 8045b85d030403a5586224b99629e56318684486 Mon Sep 17 00:00:00 2001 From: Sarthak Tripathi Date: Sun, 1 Oct 2017 06:43:56 +0530 Subject: [PATCH 07/10] Jump search added --- seaching algorithms/jump_search.cpp | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 seaching algorithms/jump_search.cpp diff --git a/seaching algorithms/jump_search.cpp b/seaching algorithms/jump_search.cpp new file mode 100644 index 00000000..f0cbe5e1 --- /dev/null +++ b/seaching algorithms/jump_search.cpp @@ -0,0 +1,43 @@ +/* +This code illustrates implementation of Jump Search in a sorted array. +*/ +#include +#include +using namespace std; + +int jumpSearch(int array[], int search, int size) +{ + int step = sqrt(size); //Best Jump block size is sqrt(size) [Reference: http://www.geeksforgeeks.org/jump-search/] + + int location = 0; //Store nearest found result while searching + + while (array[step-1] < search) //search for the element by jumping blocks while the current element is less that the one we + { //are searching for. + location = step; + step += sqrt(size); + if (location >= size) //If element not found till the end of array, return -1 + return -1; + } + + while (array[location] < search) //If array element was greater than the element we are searching for, do a linear + { //search from last known location. + location++; + if (location == step) + return -1; + } + if (array[location] == search) + return location; + + return -1; +} +int main() +{ + int array [] = {1,2,3,4,5,6,7,8,9,10}, search = 5, position; + int size = sizeof(array)/ sizeof(array[0]); + position = jumpSearch(array, search, size); + if(position == -1) + cout<<"Element is not present in array"; + else + cout<<"Element is present at "< Date: Sun, 1 Oct 2017 06:56:51 +0530 Subject: [PATCH 08/10] fixed file order --- .../position_in_sorted_array/eq_less_than.cpp | 0 .../position_in_sorted_array/first_occurance.cpp | 0 .../position_in_sorted_array/just_less_than_num.cpp | 0 .../position_in_sorted_array/position_in_sorted_array.cpp | 0 .../binary_search_extras}/ques/aggrcow.cpp | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename {binary_search => seaching algorithms/binary_search_extras}/position_in_sorted_array/eq_less_than.cpp (100%) rename {binary_search => seaching algorithms/binary_search_extras}/position_in_sorted_array/first_occurance.cpp (100%) rename {binary_search => seaching algorithms/binary_search_extras}/position_in_sorted_array/just_less_than_num.cpp (100%) rename {binary_search => seaching algorithms/binary_search_extras}/position_in_sorted_array/position_in_sorted_array.cpp (100%) rename {binary_search => seaching algorithms/binary_search_extras}/ques/aggrcow.cpp (100%) diff --git a/binary_search/position_in_sorted_array/eq_less_than.cpp b/seaching algorithms/binary_search_extras/position_in_sorted_array/eq_less_than.cpp similarity index 100% rename from binary_search/position_in_sorted_array/eq_less_than.cpp rename to seaching algorithms/binary_search_extras/position_in_sorted_array/eq_less_than.cpp diff --git a/binary_search/position_in_sorted_array/first_occurance.cpp b/seaching algorithms/binary_search_extras/position_in_sorted_array/first_occurance.cpp similarity index 100% rename from binary_search/position_in_sorted_array/first_occurance.cpp rename to seaching algorithms/binary_search_extras/position_in_sorted_array/first_occurance.cpp diff --git a/binary_search/position_in_sorted_array/just_less_than_num.cpp b/seaching algorithms/binary_search_extras/position_in_sorted_array/just_less_than_num.cpp similarity index 100% rename from binary_search/position_in_sorted_array/just_less_than_num.cpp rename to seaching algorithms/binary_search_extras/position_in_sorted_array/just_less_than_num.cpp diff --git a/binary_search/position_in_sorted_array/position_in_sorted_array.cpp b/seaching algorithms/binary_search_extras/position_in_sorted_array/position_in_sorted_array.cpp similarity index 100% rename from binary_search/position_in_sorted_array/position_in_sorted_array.cpp rename to seaching algorithms/binary_search_extras/position_in_sorted_array/position_in_sorted_array.cpp diff --git a/binary_search/ques/aggrcow.cpp b/seaching algorithms/binary_search_extras/ques/aggrcow.cpp similarity index 100% rename from binary_search/ques/aggrcow.cpp rename to seaching algorithms/binary_search_extras/ques/aggrcow.cpp From a551cfa721a7509cc0162c229e85d18b271c97c5 Mon Sep 17 00:00:00 2001 From: RonistoneJunior Date: Sun, 1 Oct 2017 00:46:46 -0300 Subject: [PATCH 09/10] Add Trie --- tree/trie.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tree/trie.cpp diff --git a/tree/trie.cpp b/tree/trie.cpp new file mode 100644 index 00000000..0778808d --- /dev/null +++ b/tree/trie.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +typedef long long int ll; +int ii,i,at; +struct no{ + no* nxt[26]; + no *root; + ll fim,qtdNo; + + inline no(char k){ + for(ii=0;ii<26;ii++) nxt[ii] = NULL; + root = this; + fim = qtdNo = 0; + } + inline bool insert(const string s, ll i){ + if(i == s.size()){ + fim = 1; + return false; + } + at = s[i] - 'a'; + if(!nxt[at]){ + nxt[at] = new no(s[i]); + nxt[at] -> root = root; + root -> qtdNo++; + } + else if(nxt[at] -> fim) return true; + return nxt[at] -> insert(s,i+1); + } +}; + main(){} From b0a2f49371bc26a71b2e371b9b3e68c5cbc386de Mon Sep 17 00:00:00 2001 From: emht Date: Sun, 1 Oct 2017 18:16:29 +0530 Subject: [PATCH 10/10] Update README.md --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index b9593518..ac4fc3e6 100644 --- a/README.md +++ b/README.md @@ -6,19 +6,31 @@ This repo helps you in competitive program as it contains many advanced algorith ## Contents: +### Search Algorithms - Linear Search - binary search + +### Sorting Algorithms - Bubble Sort - Selection Sort - insertion sort - merge sort - quick sort - radix sort + +### Shortest Path Algorithms - Dijkstra - Floyd Warshall + +### Common Data Structures - heap - queue - stack + - Array + - Linked List + +## Languages Used: + - C++ ## How to contribute: