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: 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 +#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<>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) 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") diff --git a/seaching algorithms/binary_search.cpp b/seaching algorithms/binary_search.cpp new file mode 100644 index 00000000..4999d967 --- /dev/null +++ b/seaching algorithms/binary_search.cpp @@ -0,0 +1,32 @@ +/* +This code illustrates a recursive implementation of binary search in a sorted array. +*/ +#include +using namespace std; + +int binarySearch(int array[], int left, int right, int search) +{ + if (right >= 1) + { + 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 + 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]); + 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 "< +#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 "< +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< +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(){}