Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
9acc631
Initial commit
manumeral Nov 3, 2015
3a1818d
Edit Distance completed
manumeral Nov 3, 2015
06e229a
Binary Indexed Tree added- Part 1
manumeral Nov 3, 2015
69d5cf8
Reference File Added
manumeral Nov 3, 2015
40b4084
Shortest Path Algorithms added
manumeral Dec 1, 2015
2101090
Merge branch 'master' of https://github.com/manumeral/Algorithms
manumeral Dec 1, 2015
8a230d1
Adding Minimum Spanning Tree Algos
manumeral Dec 1, 2015
f82088e
Longest Common Subsequence and Longest Increasing Subsequence Problem…
manumeral Dec 30, 2015
05da11d
Add Gitter badge
gitter-badger May 18, 2016
88ee0c6
Merge pull request #1 from gitter-badger/gitter-badge
swrdfish May 18, 2016
7d3e348
Change the location of gitter chat badge
swrdfish May 18, 2016
9af37b0
Create READMEDijkstra.md
prateekiiest Oct 24, 2016
8b5f426
Create rsa.c
prateekiiest Oct 25, 2016
0b185db
Merge pull request #3 from prateekiiest/patch-2
smitbose Dec 24, 2016
0fdae29
Deleting rsa.c
prateekiiest Dec 25, 2016
4e23495
Create rsa.py
prateekiiest Dec 25, 2016
d01ab81
Create rsa.py
prateekiiest Dec 25, 2016
4924f12
Update README.md
prateekiiest Dec 25, 2016
29734ee
Merge pull request #4 from prateekiiest/patch-3
smitbose Dec 25, 2016
7d564ac
Merge pull request #9 from prateekiiest/patch-5
smitbose Dec 25, 2016
cd61539
Merge pull request #10 from prateekiiest/patch-6
smitbose Dec 25, 2016
7fcb8a5
added gitignore
smitbose Dec 26, 2016
f085df5
Added search and sort
smitbose Dec 26, 2016
85a3340
Added search and sort
smitbose Dec 26, 2016
c496bc3
Add files via upload
prateekiiest Feb 4, 2017
74cb8d5
Add files via upload
prateekiiest Feb 4, 2017
b8594ab
Delete normaldist.png
prateekiiest Feb 4, 2017
33ea39f
Delete normaldistributionforarrayelements.png
prateekiiest Feb 4, 2017
52d39e8
Create bubble_sort.c
prateekiiest Feb 4, 2017
0456985
Create linear_search.c
prateekiiest Feb 4, 2017
5d7cd75
Create Binary_Search.c
prateekiiest Feb 4, 2017
ec78dc5
Create insertion_sort.c
prateekiiest Feb 4, 2017
ac0085d
Create selection_sort.c
prateekiiest Feb 4, 2017
b9cb9ac
Create Quick_Sort.c
prateekiiest Feb 4, 2017
f32d144
Create Merge_Sort.c
prateekiiest Feb 4, 2017
c59c98c
Merge pull request #23 from prateekiiest/master
smitbose Mar 12, 2017
579738f
dynamic memory allocation and modified variable names
abiduzz420 Nov 24, 2017
982a075
dynamic memory allocation in linear_search.c
abiduzz420 Nov 24, 2017
ddf3334
Merge pull request #27 from abiduzz420/master
prateekiiest Nov 24, 2017
029582e
declared swap function beforehand to avoid conflicting types warning …
manojsrivatsavsfu Nov 30, 2017
143e3ce
optimisation , conflicting types warning debugged
manojsrivatsavsfu Nov 30, 2017
850a960
counting sort algorithm
chirayujain98 Nov 30, 2017
76ec41e
Update Binary_Search.c
prateekiiest Dec 1, 2017
e0c5617
Merge pull request #32 from codeIIEST/test-branch
prateekiiest Dec 1, 2017
6134e0f
Merge branch 'master' into sorting
manojsrivatsavsfu Dec 1, 2017
cd2d6f0
Added Boyer-Moore string search algorithms
the-ethan-hunt Dec 1, 2017
e2b433a
j>=0 should be the first condition since c use shortciruit & operator
manojsrivatsavsfu Dec 1, 2017
6861453
updated mistake in README.md
manojsrivatsavsfu Dec 1, 2017
c3b52d4
Made minor optimisation
manojsrivatsavsfu Dec 1, 2017
b7d0568
Merge branch 'sorting' of https://github.com/manojkun/Algorithms into…
manojsrivatsavsfu Dec 1, 2017
7bf0cc4
Merge pull request #34 from manojkun/master
prateekiiest Dec 1, 2017
00dfdb7
Update New Text Document.txt
prateekiiest Dec 1, 2017
fb91030
Merge pull request #30 from chirayujain98/count
prateekiiest Dec 1, 2017
b246e80
Add Codacy badge
codacy-badger Dec 1, 2017
6a686ce
Merge pull request #35 from codacy-badger/codacy-badge
prateekiiest Dec 1, 2017
3fe0dbe
Update README.md
prateekiiest Dec 1, 2017
9789eac
Update README.md
prateekiiest Dec 1, 2017
87adfe6
Revert "counting sort algorithm"
prateekiiest Dec 1, 2017
2a0ea39
Merge pull request #36 from codeIIEST/revert-30-count
prateekiiest Dec 1, 2017
c4b0b68
Update README.md
prateekiiest Dec 1, 2017
9cceed4
revised counting sort
chirayujain98 Dec 1, 2017
48f7c48
Merge pull request #37 from chirayujain98/count_sort
prateekiiest Dec 1, 2017
91e9853
added lowest-common-ancestor
AdilMah Dec 1, 2017
aaeb518
aded level_order_traversal.c
AdilMah Dec 1, 2017
e615b03
Add files via upload
the-ethan-hunt Dec 1, 2017
cce33f3
Delete time_compleexity_graph.png
the-ethan-hunt Dec 1, 2017
bda580e
Add files via upload
the-ethan-hunt Dec 1, 2017
a2a05a9
create polynomial addition using linked lists
AdilMah Dec 1, 2017
ce0f4bd
updated level_order_traversal
AdilMah Dec 1, 2017
fb41d16
Delete lowest_common_ancestor.c
AdilMah Dec 1, 2017
889502a
Delete polynomial_addition_using_linked_lists.c
AdilMah Dec 1, 2017
94a6faa
Merge pull request #38 from AdilMah/master
prateekiiest Dec 1, 2017
7975a52
Basic Linklist
sgrG24 Dec 1, 2017
e522055
Update Linklist.cpp
sgrG24 Dec 1, 2017
640b0bf
Merge pull request #33 from the-ethan-hunt/master
prateekiiest Dec 1, 2017
6dda26a
created duplicates_removal_from_unsorted linked_ll
AdilMah Dec 1, 2017
9685108
corrected some comments
AdilMah Dec 1, 2017
d12deb4
The Knapsack problem.
Dec 1, 2017
19b29d1
Merge pull request #41 from AdilMah/master
prateekiiest Dec 1, 2017
0dc04b6
Add comments in each section
sgrG24 Dec 1, 2017
92ad249
Update Linklist.cpp
sgrG24 Dec 1, 2017
684cfba
Code added for bubble sort in c++
SaiSakethAluru Dec 1, 2017
41522d3
Add files via upload
shiveshchoudhary Dec 1, 2017
484b2d7
Delete interpolation_search.py
shiveshchoudhary Dec 1, 2017
f1e5ab3
Add files via upload
shiveshchoudhary Dec 1, 2017
f2beb64
Delete interpolation_search.py
shiveshchoudhary Dec 1, 2017
a5a7cb9
Add files via upload
shiveshchoudhary Dec 1, 2017
b9af8f4
Add files via upload
shiveshchoudhary Dec 1, 2017
a9538a6
Delete interpolation_search.py
shiveshchoudhary Dec 1, 2017
eae9b4c
Update interpolationsearch.c
shiveshchoudhary Dec 1, 2017
712118f
Update README.md
shiveshchoudhary Dec 1, 2017
f614e54
KMP-Algorithm
nandikeshsingh Dec 1, 2017
f59a569
Update Knuth-Morris-Pratt_Algorithm.cpp
nandikeshsingh Dec 1, 2017
68ca692
Update bubble_sort.c
prajwal72 Dec 1, 2017
31fb6a1
Merge pull request #40 from sgrG24/master
prateekiiest Dec 2, 2017
a3fa677
Added both Linked List codes into a single Directory
manojsrivatsavsfu Dec 2, 2017
8e3f8c4
Merge pull request #52 from manojkun/master
prateekiiest Dec 2, 2017
59fcc27
Update bubble_sort.c
prajwal72 Dec 2, 2017
c549897
Merge pull request #47 from kumarprajwal26/patch-1
prateekiiest Dec 2, 2017
9d47930
Update README.md
shiveshchoudhary Dec 2, 2017
3d88c66
Update README.md
prateekiiest Dec 2, 2017
709810b
Merge pull request #44 from shiveshchoudhary/master
prateekiiest Dec 2, 2017
eaa5fc9
Update README.md
shiveshchoudhary Dec 2, 2017
1a6357f
Merge pull request #53 from shiveshchoudhary/master
prateekiiest Dec 2, 2017
21860f8
Update knapsack.cpp
Dec 2, 2017
1884c40
Merge pull request #42 from heisenberg-2505/knapsack
prateekiiest Dec 2, 2017
43832d2
Update Knuth-Morris-Pratt_Algorithm.cpp
nandikeshsingh Dec 2, 2017
a333040
Added bubble sort c and cpp files to a new folder
SaiSakethAluru Dec 2, 2017
9394535
Merge pull request #43 from SaiSakethAluru/bubble_sort_cpp
prateekiiest Dec 2, 2017
543229b
Merge pull request #45 from nandikeshsingh/master
prateekiiest Dec 2, 2017
4daa138
Added CONTRIBUTING .md
the-ethan-hunt Dec 4, 2017
0f3cb96
Merge pull request #75 from the-ethan-hunt/patch-1
prateekiiest Dec 5, 2017
aa60973
Rename sort/Merge_Sort.c to sort/Merge_Sort/Merge_Sort.c
prateekiiest Dec 5, 2017
4387781
Rename sort/Quick_Sort.c to Competitive_Coding/Algorithms/Sort/Quick_…
prateekiiest Dec 5, 2017
995e7cd
Rename sort/Merge_Sort/Merge_Sort.c to Competitive_Coding/Algorithms/…
prateekiiest Dec 5, 2017
66f7971
Delete bubble_sort.cpp
prateekiiest Dec 5, 2017
3c63614
Delete bubble_sort.c
prateekiiest Dec 5, 2017
2e465ba
Rename sort/counting_sort.cpp to Competitive_Coding/Algorithms/Sort/C…
prateekiiest Dec 5, 2017
2121389
Rename sort/insertion_sort.c to Competitive_Coding/Algorithms/Sort/In…
prateekiiest Dec 5, 2017
ea4005f
Rename sort/selection_sort.c to Competitive_Coding/Algorithms/Sort/Se…
prateekiiest Dec 5, 2017
b37be4d
Rename sort/bubble sort/bubble_sort.c to Competitive_Coding/Algorithm…
prateekiiest Dec 5, 2017
8038bb5
Rename sort/bubble sort/bubble_sort.cpp to Competitive_Coding/Algorit…
prateekiiest Dec 5, 2017
9e3fd75
Delete README.md
prateekiiest Dec 5, 2017
55657b9
Rename search/Binary_Search.c to Competitive_Coding/Algorithms/Search…
prateekiiest Dec 5, 2017
f95819f
Rename search/linear_search.c to Competitive_Coding/Algorithms/Search…
prateekiiest Dec 5, 2017
ab44af2
Rename search/interpolationsearch.c to Competitive_Coding/Algorithms/…
prateekiiest Dec 5, 2017
f655220
Rename search/string_search/Knuth-Morris-Pratt_Algorithm.cpp to Compe…
prateekiiest Dec 5, 2017
03b4129
Rename search/string_search/boyer_moore_algorithm.c to Competitive_Co…
prateekiiest Dec 5, 2017
581e992
Rename Competitive_Coding/Algorithms/Search/Linear Search/interpolati…
prateekiiest Dec 5, 2017
f16d5db
Rename LinkedList/LinkedList_operations.cpp to Competitive_Coding/Dat…
prateekiiest Dec 5, 2017
0332155
Rename LinkedList/Removing_duplicates_from_Linked_List.cpp to Competi…
prateekiiest Dec 5, 2017
26c6917
Delete rsa.py
prateekiiest Dec 5, 2017
fdb9eb5
Rename Cryptography/RSA_Algortihm/rsa.py to Security Algorithms/Crypt…
prateekiiest Dec 5, 2017
a0f9c3b
Rename Longest_Increasing_Subsequence/lis_n_log_n.cpp to Competitive_…
prateekiiest Dec 5, 2017
7956481
Rename Longest_Increasing_Subsequence/lis_n_squared_sol.cpp to Compet…
prateekiiest Dec 5, 2017
a7325f0
Delete a.out
prateekiiest Dec 5, 2017
f850071
Rename Longest_Common_Subsequence/lcs.cpp to Competitive_Coding/Algor…
prateekiiest Dec 5, 2017
d3b85d4
Rename Knapsack/knapsack.cpp to Competitive_Coding/Algorithms/Greedy/…
prateekiiest Dec 5, 2017
1a2f225
Delete a.out
prateekiiest Dec 5, 2017
31d72ab
Rename Shortest_Path/dijsktra/dijsktra_implementation.cpp to Competit…
prateekiiest Dec 5, 2017
547fe50
Rename Shortest_Path/dijsktra/READMEDijkstra.md to Competitive_Coding…
prateekiiest Dec 5, 2017
8edd405
Delete a.out
prateekiiest Dec 5, 2017
d3c1be8
Delete a.out
prateekiiest Dec 5, 2017
b7c5096
Rename Shortest_Path/Floyd-Warshall/floyd_warshall.cpp to Competitive…
prateekiiest Dec 5, 2017
5802825
Rename Minimum_Spanning_Tree/KruskalMST.cpp to Competitive_Coding/Alg…
prateekiiest Dec 5, 2017
72eb2ea
Rename Minimum_Spanning_Tree/Prims.cpp to Competitive_Coding/Algorith…
prateekiiest Dec 5, 2017
999a1c8
Delete a.out
prateekiiest Dec 5, 2017
be96478
Delete time_complexity_graph.png
prateekiiest Dec 5, 2017
9c3bcf3
Delete README.md
prateekiiest Dec 5, 2017
52eb7ab
Rename BInary_Tree/level_order_traversal.c to Competitive_Coding/Data…
prateekiiest Dec 5, 2017
4d9161a
First draft of structural changes.
manumeral Dec 5, 2017
319aa1b
Merge pull request #84 from codeIIEST/test-branch
prateekiiest Dec 6, 2017
c91ddca
Create CODE_OF_CONDUCT.md
prateekiiest Dec 6, 2017
cb35064
Create LICENSE
prateekiiest Dec 6, 2017
a4f3041
Create ISSUE_TEMPLATE.md
prateekiiest Dec 6, 2017
8f15992
Create PULL_REQUEST_TEMPLATE.md
prateekiiest Dec 6, 2017
654f31f
HeapSort with readme (#97)
nitishtw Dec 6, 2017
bb5df78
Update ISSUE_TEMPLATE.md
prateekiiest Dec 6, 2017
533d2b4
Update CONTRIBUTING.md
prateekiiest Dec 6, 2017
bad56b0
resolved merge conflicts
Nihalh55 Dec 14, 2017
935615c
Merge branch 'master' of https://github.com/codeIIEST/Algorithms
Nihalh55 Dec 19, 2017
475b00a
GCD and Prime Factorization algos added
Nihalh55 Dec 19, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
6 changes: 4 additions & 2 deletions Competitive Coding/Graphs/Shortest Path/dijsktra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Dijkstra thought about the shortest path problem when working at the Mathematica
A year later, he came across another problem from hardware engineers working on the institute's next computer: minimize the amount of wire needed to connect the pins on the back panel of the machine. As a solution, he re-discovered the algorithm known as Prim's minimal spanning tree algorithm (known earlier to Jarník, and also rediscovered by Prim).
Dijkstra published the algorithm in 1959, two years after Prim and 29 years after Jarník.


**Algorithm**

>>>>>>> 56b74e922bff38aee9cfff9e7cfde1c6610adc50
Let the node at which we are starting be called the initial node. Let the distance of node Y be the distance from the initial node to Y. Dijkstra's algorithm will assign some initial distance values and will try to improve them step by step.

Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.
Expand All @@ -30,6 +30,7 @@ Otherwise, select the unvisited node that is marked with the smallest tentative
---------------------------------------------------

## Related Problems
>>>>>>> 56b74e922bff38aee9cfff9e7cfde1c6610adc50
The functionality of Dijkstra's original algorithm can be extended with a variety of modifications. For example, sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after the first optimal solution.

Dijkstra's algorithm is usually the working principle behind link-state routing protocols, OSPF and IS-IS being the most common ones.
Expand All @@ -48,15 +49,16 @@ Fast marching method can be viewed as a continuous version of Dijkstra's algorit
----------------------------------------------------------

## Applications
>>>>>>> 56b74e922bff38aee9cfff9e7cfde1c6610adc50

1. **Flight Agenda** -- A travel agent requests software for making an agenda of flights for clients. The agent has access to a data base with all airports and flights. Besides the flight number, origin airport and destination, the flights have departure and arrival time. Specifically the agent wants to determine the earliest arrival time for the destination given an origin airport and start time

2. **Telephone Network** -- In a telephone network the lines have bandwidth, BW. We want to route the phone call via the highest BW.


----------------------------------------------
**Source**

[Wikipedia](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)

>>>>>>> 56b74e922bff38aee9cfff9e7cfde1c6610adc50
[Dijkstra](http://www.csl.mtu.edu/cs2321/www/newLectures/30_More_Dijkstra.htm)
Empty file modified Competitive Coding/Linked List/Linked_List_operation.cpp
100644 → 100755
Empty file.
36 changes: 36 additions & 0 deletions Competitive Coding/Math/GCD and LCM/GCDEuclidsAlgorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//Program to calculate the GCD of a number by Euclids algorithm, and then find its LCM

#include<stdio.h>

//Function to calculate the GCD
//It is assumed that n >= m
int gcd(int m, int n){

if(m == 0)
return n;
else
return gcd((n%m) , m);
}

//Function to calculate LCM
int lcm(int m, int n, int gcdOfmn){

int lcm = (m * n) / gcdOfmn;
return lcm;
}

//Driver Function
int main(){

int m , n ,Gcd;
//Test 1
m = 120 , n = 210;
Gcd = gcd(m,n);
printf("\nThe GCD and LCM of %d and %d are %d and %d.", m , n , Gcd , lcm(m,n,Gcd));
//Test 2
m = 12 , n = 66;
Gcd = gcd(m,n);
printf("\n\nThe GCD and LCM of %d and %d are %d and %d.\n\n", m , n , Gcd , lcm(m,n,Gcd));
return 0;
}
//End driver function
41 changes: 41 additions & 0 deletions Competitive Coding/Math/GCD and LCM/GCDEuclidsExtendedAlgorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//Program to calculate the GCD of two numbers by Extended Euclid's Algorithm and
//to find the integers x and y in the equation mx + ny = gcd(m,n)
//n is assumed to be following the following constariant: n >= m

#include<stdio.h>

//Function to caluclate the GCD of two numbers with euclid's extended Algorithm
int gcdExtended(int m, int n, int *x, int *y){

//m and n are the integers
//x and y are the integers in the equation mx + ny = gcd(m,n)
//x1 and y1 are the intermediate x and y in the recursive calculate
int x1 , y1 , gcd;
if(m == 0){

*x = 0;
*y = 1;
return n;
}
gcd = gcdExtended(n%m,m,&x1,&y1);
*x = y1 - (n/m)*x1;
*y = x1;
return gcd;
}

//Driver Function
int main(){

int m , n , gcd , x , y;
//Test 1
m = 74 , n = 120;
gcd = gcdExtended(m,n,&x,&y);
printf("\n The GCD is %d and x = %d , y = %d.", gcd , x , y);
//Test 2
m = 12 , n = 22;
gcd = gcdExtended(m,n,&x,&y);
printf("\n The GCD is %d and x = %d , y = %d.", gcd , x , y);

return 0;
}
//Driver function ends
63 changes: 63 additions & 0 deletions Competitive Coding/Math/GCD and LCM/GCDSteinsAlgorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//Program to calculate the GCD of two numbers by Stein's Algorithm and

#include<stdio.h>
#include<math.h> //For pow function


int gcdSteins(int m ,int n, int *k){

//m and n are the integers.
//k counts the number of common '2' factors

//Base conditions
if(m == n)
return m;
if(m == 0)
return n;
if(n == 0)
return m;
//End of base conditions

//To check if m is even
if(~(m & 1) == 1){

//if n is even
if(~(n & 1) == 1){
*k = *k + 1;
return gcdSteins((m >> 1) , (n >> 1) , k); // m and n are divided by two. k is incremented
}

//if n is odd
return gcdSteins((m >> 1) , n , k); // m is divided by 2
}

//m is odd and n is even case
if(~(n & 1) == 1)
return gcdSteins(m ,(n >> 1) ,k); // n is divided by 2

//Both are odd case
if(m > n)
return gcdSteins(((m-n)>>1) , n , k);

return gcdSteins(((n-m)>>1) , m , k);
}

//Driver Function
int main(){

int m , n ,Gcd , k;
//Test 1
m = 120 , n = 210 , k = 0;
Gcd = gcdSteins(m,n,&k);
Gcd *= pow(2,k);
printf("\nThe GCD of %d and %d is %d.", m , n , Gcd);

//Test 2
m = 2 , n = 6 , k = 0;
Gcd = gcdSteins(m,n,&k);
Gcd *= pow(2,k);
printf("\nThe GCD of %d and %d is %d.\n\n", m , n , Gcd);

return 0;
}
//End driver function
28 changes: 28 additions & 0 deletions Competitive Coding/Math/GCD and LCM/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#CALCULATING GCD AND LCM USING THREE DIFFERENT APPROACHES
##Using Euclid's Algorithm

This algorithm works on the famous **Euclid's Algorithm** which states that 'GCD(m,n) = GCD(n%m , m)' and 'GCD(0,m) = m'.
It is coded in a recurisve manner.

LCM calculation has one common formula, namely, 'LCM(m,n) = (m*n)/GCD(m,n)'.

##Using Extended Euclid's Algorithm

The Euclid's Extended Algorithm states that there exists two integers x and y such that 'mx + ny = GCD(m,n)'.
This algorithm finds the GCD of the integers along with above said integers.

##Using Steins Algorithm

Stein’s algorithm or binary GCD algorithm is an algorithm that computes the greatest common divisor of two non-negative integers. Stein’s algorithm replaces division with arithmetic shifts, comparisons, and subtraction.

The algorithm:

1. If both a and b are 0, gcd is zero gcd(0, 0) = 0.
2. Gcd(a, 0) = a and gcd(0, b) = b because everything divides 0.
3. If a and b are both even, gcd(a, b) = 2*gcd(a/2, b/2) because 2 is a common divisor. Multiplication with 2 can be done with bitwise shift operator.
4. If a is even and b is odd, gcd(a, b) = gcd(a/2, b). Similarly, if a is odd and b is even, then gcd(a, b) = gcd(a, b/2). It is because 2 is not a common divisor.
5. If both a and b are odd, then gcd(a, b) = gcd(|a-b|/2, b). Note that difference of two odd numbers is even
6. Repeat steps 3–5 until a = b, or until a = 0. In either case, the GCD is power(2, k) * b, where power(2, k) is 2 raise to the power of k and k is the number of common factors of 2 found in step 2.



Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Program to find the divisors of an integer n in O(root n) time

#include<stdio.h>
#include<math.h>

// Function to print the divisors of integer n
void printDivisors(int n){

printf("\n\nThe divisors of %d are: ", n);
//Iterates from i=1 to root n and print divisors as i and n/i at one go
for (int i=1; i<=sqrt(n)+1; i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
printf("%d ", i);

else // Otherwise print both
printf("%d %d ", i, n/i);
}
}
printf("\n\n");
}

//Driver Function
void main()
{
printf("\n\n\tDIVISOR PROGRAM");
int n;
printf("\n\nEnter the integer: ");
scanf("%d" , &n);
printDivisors(n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//Program to find the prime factorization of a number in O(n^1/2) time

#include<stdio.h>
#include<math.h>

//Function to print prime factors of an integer n
void primeFactorization(int n){

printf("\n\nThe prime factors of %d are: ", n);

//To print factor as 2 until the number is odd
while(n % 2 == 0){

printf("2 ");
n = n / 2; //Dividing number by 2
}

//The number would be odd after exiting loop
//Factors will be checked from i = 3 to root n, increasing i by 2 every loop, as all even numbers are taken acre of
int i = 3 , sqrtN = sqrt(n);
while(i<=sqrtN){

//If i is a prime factor of n
while(n%i==0){

printf("%d ", i);
n = n / i;
}

i = i + 2;
}

//If n > 2 after these steps, this tells us that n is aprime numbers
if(n > 2){

printf("%d " , n);
}
printf("\n\n");
}

//Driver function
void main(){

printf("\n\n\tPRIME FACTORIZATION");
printf("\n\nEnter number: ");
int n;
scanf("%d" , &n);
primeFactorization(n);
}
//Main ends
43 changes: 43 additions & 0 deletions Competitive Coding/Math/Prime Factorisation and Divisors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#PRIME FACOTRIZATION AND DIVISOR ALGORITHM
##PRIME FACTORZATION

Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be “2 2 3”. And if the input number is 315, then output should be “3 3 5 7”.

###Algorithm

1. While n is divisible by 2, print 2 and divide n by 2.
2. After step 1, n must be odd. Now start a loop from i = 3 to square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.
3. If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.

###Working

The steps 1 and 2 take care of composite numbers and step 3 takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers. This is clear that step 1 takes care of even numbers. And after step 1, all remaining prime factor must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2.
Now the main part is, the loop runs till square root of n not till. To prove that this optimization works, let us consider the following property of composite numbers.

Every composite number has at least one prime factor less than or equal to square root of itself.

This property can be proved using counter statement. Let a and b be two factors of n such that a*b = n. If both are greater than √n, then a.b > √n, * √n, which contradicts the expression “a * b = n”.

In step 2 of the above algorithm, we run a loop and do following in loop
* Find the least prime factor i (must be less than √n,)
* Remove all occurrences i from n by repeatedly dividing n by i.
* Repeat steps a and b for divided n and i = i + 2. The steps a and b are repeated till n becomes either 1 or a prime number.

###Time Complexity

O(n^1/2), where n is the integer.


##DIVISORS

Given a natural number n, print all distinct divisors of it.

###Algorithm

1. Iterate from i = 1 to i = root(n) + 1.
2. If i divides n, print i along with (n/i), thus reducing time.

###Time Complexity

O(n^1/2), where n is the integer.

18 changes: 18 additions & 0 deletions Competitive Coding/Sorting/Heap Sort/Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Heap sort is a comparison based sorting technique based on Binary Heap data structure.
# Heapsort is an in-place algorithm, but it is not a stable sort.
# Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case scenarios.
# In max-heaps, maximum element will always be at the root. Heap Sort uses this property of heap to sort the array.

Heap sort algorithm is divided into two basic parts :
(1) Creating a Heap of the unsorted list.
(2) Then a sorted array is created by repeatedly removing the largest/smallest element from the heap, and inserting it into the array.
{3) The heap is reconstructed after each removal.

Time Complexity: Time complexity of heapify is O(logn).
Time complexity of BUILD-MAX-HEAP(A) is O(n)
and we run max_heapify N−1 times in heap_sort function,
therefore complexity of heap_sort function in each case (Best, worst and average) is O(nlogn)

Space Complexity: O(1)

## Documentation for HeapSort
* Heap sort is a comparison based sorting technique based on Binary Heap data structure.
* Heapsort is an in-place algorithm, but it is not a stable sort.
Expand All @@ -21,3 +38,4 @@ Here, Time complexity of heapify is O(logn) and time complexity of BUILD-MAX-HEA
* Space Complexity: O(1)

<img align="center" src="https://upload.wikimedia.org/wikipedia/commons/1/1b/Sorting_heapsort_anim.gif">

Loading