forked from srbcheema1/Algo_Ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/srbcheema1/Algo_Ds
- Loading branch information
Showing
15 changed files
with
248 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <set> | ||
#include <utility> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
//This function decides if 3 points form a right turn or not | ||
bool Is_Right_Turn(vector <pair<int,int>> Hull){ | ||
pair <int,int> 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 <pair<int,int>> Points, Upper_Hull, Lower_Hull; | ||
|
||
set <pair <int,int>> 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<<"("<<point.first<<", "<<point.second<<")\n"; | ||
|
||
return 0; | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
This code illustrates a recursive implementation of binary search in a sorted array. | ||
*/ | ||
#include <iostream> | ||
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 "<<position+1<<"th position"; | ||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
This code illustrates implementation of Jump Search in a sorted array. | ||
*/ | ||
#include <iostream> | ||
#include <math.h> | ||
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 "<<position+1<<"th position"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
This code illustrates how Linear Search works | ||
*/ | ||
#include <iostream> | ||
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<<search<<" was found at "<<position+1<<"th position in the given array."; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <bits/stdc++.h> | ||
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(){} |