Suppose we have to find the the gcd of twoo numbers a,b.
if a > b
then it follows:
a = b*q+r
where q = quotient and r is remainder.Now r must be smaller than b.
i.e.
b = r*q1 + r1, similarly
r = r1*q2 + r2
.....
.....
since remainder decreases with every step which at one time leads to 0.
i.e.
r(n-2)= r(n-1)*qn + rn, where rn = 0.
the final non-zero remainder will be the gcd of a,b which is r(n-1)
algorithm
GCD(a,b)
IF (b == 0) return a;
ELSE
return GCD(b, a/b);
Saturday, November 23, 2019
Bucket sort - Soring Algorithms
Bucket sort is the simplest sorting algorithm, And it have O(n) tine complexity
Suppose we have an array A of N elements.
Lets say each element is in range of 0 < a[i] <100
Then we simply take an array B of 100 elements.
And move through the array A, for each array element just increment the counter of that position in array B
For e.g.:
if A = [5,4,2,2,3]
B = [0,0,0,0,0]
After moving through each element of A
B would become [0,0,2,1,1,1]
which indicated we have two 2's one 3,4 and 5;
so we can simply give the sorted elements = 2,2,3,4,5.
Suppose we have an array A of N elements.
Lets say each element is in range of 0 < a[i] <100
Then we simply take an array B of 100 elements.
And move through the array A, for each array element just increment the counter of that position in array B
For e.g.:
if A = [5,4,2,2,3]
B = [0,0,0,0,0]
After moving through each element of A
B would become [0,0,2,1,1,1]
which indicated we have two 2's one 3,4 and 5;
so we can simply give the sorted elements = 2,2,3,4,5.
Subscribe to:
Posts (Atom)