Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/srbcheema1/Algo_Ds
Browse files Browse the repository at this point in the history
  • Loading branch information
achaJackson committed Oct 1, 2017
2 parents 19517fb + 70fc672 commit f17c9e5
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 76 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
31 changes: 0 additions & 31 deletions Searching Algorithms/LINEAR_SEARCH

This file was deleted.

96 changes: 96 additions & 0 deletions maths/convexHull/ConvexHullScan.cpp
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;

}
43 changes: 0 additions & 43 deletions maths/floydWarshal/FloydWarshall.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions maths/multiplication/multiply 2 numbers greater than 10^6.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
This program can be used to multiply 2 numbers grater than 10^6, usually a major hurdle in languages like C/C++
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
*/

Expand All @@ -10,7 +10,7 @@ int main()
ios_base::sync_with_stdio(false);
int n1, n2;
cin>>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)
Expand Down
19 changes: 19 additions & 0 deletions maths/prime/PrimeCheck.py
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")
32 changes: 32 additions & 0 deletions seaching algorithms/binary_search.cpp
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.
43 changes: 43 additions & 0 deletions seaching algorithms/jump_search.cpp
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;
}
13 changes: 13 additions & 0 deletions seaching algorithms/linear.cpp
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;
}
31 changes: 31 additions & 0 deletions tree/trie.cpp
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(){}

0 comments on commit f17c9e5

Please sign in to comment.