Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts

[Algorithm] - Selection sort, with Explanation and an example

selection sort

This algorithm sorts the given array containing n number of elements, into ascending order. This algorithm uses Brut Force method of problem solving. 

What's Brut Force Method?
This is a approach of problem solving in design and analysis of algorithms. It is a straight forward approach of problem solving. Which means the problem is solved directly based on the problem statements and definition of concepts involved. In simple words, its a type of Trial and Error problem solving.
  • Learn more about Brut Force method here.
  • and about Trial and error method here.

Algorithm:



Illustration:
Take an example array which is to be sorted

A = { 89 , 45 , 68 , 90 , 29 , 34 , 17 }

In the first line of the algorithm, This array is passed as an argument for the selection sort algorithm.

Now the logic in brief:
A loop is run n-1 number of times, At the beginning the first number is imagined to be minimum number in the array. Then for each iteration, the minimum number in the array which is next to the first number, is found. and after finding the minimum number, it is swapped (Exchanged) with the first number. Now at this point, First minimum number of the entire array is at the first position.
For the next iteration, second number is considered to be the minimum number, and any number which is lesser than the second number (if any) is found and swapped with the second element. Now the second minimum element in the array must be at the second position. This way, the process is continued for n-1 times, each time bringing a number to its appropriate (sorted) position in the array.

Detailed explanation:
  • The first for loop is run n-1 number of times, that is value of i ranging from 0 to n-2.(each time bringing a number to its appropriate position in the array)
  • Inside the first for loop, the 1st element (that is 0th element according to array indexing) is considered to be the minimum element by assigning the value of minimum to a[i].
  •  2nd for loop  runs from 'i+1'(number next to the considered minimum element) to 'n-1'(the last element). Inside this for loop, it is checked whether, the value at j in the array is lesser than a[i](the considered minimum element). If there is one, value of minimum is set to a[j]. at the end of second for loop, the minimum value at that iteration is found. Then it swapped with the a[i]. Now smallest element is sitting in its appropriate position.
  • Same thing is continued for n-1 numbers of times to bring the smallest elements in its appropriate position.


Example:
Consider the above told array

A = { 89 , 45 , 68 , 90 , 29 , 34 , 17 }

1.  i=0
minimum=a[0]=89;
for j loop check if a[j] is less than minimum(in this case 89). If it is lesser, them update the value of minimum=j.
at the end of this loop, the first minimum value of the array is found. That is 17. Minimum=6 (array index of the value 17)

a[0] is swapped with a[minimum]
89 is swapped with 17.

now array is
A = { 17 , 45 , 68 , 90 , 29 , 34 , 89 }




2. i=1
 minimum=a[1]=45;
now in j loop iteration, minimum is 29. so minimum=4.

a[1] is swapped with a[minimum]
45 is swapped with 29.

now array is
A = { 17 , 29 , 68 , 90 , 45 , 34 , 89 }



3. i=2
 minimum=a[2]=68;
now in j loop iteration, minimum is 34. so minimum=5.

a[2] is swapped with a[minimum]
68 is swapped with 34.

now array is
A = { 17 , 29 , 34 , 90 , 45 , 68 , 89 }



4. i=3
 minimum=a[3]=90;
now in j loop iteration, minimum is 45. so minimum=4.

a[3] is swapped with a[minimum]
90 is swapped with 45.

now array is
A = { 17 , 29 , 34 , 45 , 90 , 68 , 89 }



5. i=4
 minimum=a[4]=90;
now in j loop iteration, minimum is 68. so minimum=5.

a[4] is swapped with a[minimum]
90 is swapped with 68.

now array is
A = { 17 , 29 , 34 , 45 , 68 , 90 , 89 }


6. i=5
 minimum=a[5]=90;
now in j loop iteration, minimum is 89. so minimum=6.

a[5] is swapped with a[minimum]
90 is swapped with 89.

now array is
A = { 17 , 29 , 34 , 45 , 68 , 89 , 90 }


[Algorithm] - Sieve algorithm to find prime numbers, with an example

 Sieve algorithm

This algorithm generates prime number in the range of '0' to 'a given input number'


This post will tell you in detail about "Sieve algorithm with an example"

Algorithm:



Detailed Explanation: 

Consider the input to be 15. I.e., n=15. That is we have to generate prime numbers between 0 to n.

1. The first ‘for loop‘ iterates for p=2 upto n. Each time making A[p]=p. So after this for loop, the array must contain the values from 2 to n. [1 is not included since it never can be a prime number].

Then the value of array A must be now
   A={2,3,4,5,6,7,8,9,10,11,12,13,14,15}


2. The second for loop is the main logic in this algorithm.
  • Rather than explaining the logic, let me explain with the example. So that you yourself can understand the logic. 
  • This loop runs from ‘2’ to value less than Square root of 15. That is, loop will run for values 2 and 3 (Obvious).
  • Now inside the loop check whether A[p] not equal to 0. At the beginning, p=2 and A[2] is not equal to 0, because A[2]=2. (From the first step).
  • So value of j is assigned to 2*2 that is 4. And inside the while loop, all the values which are multiple of 2 are set to zero except the number 2 [Indicating that the multiples are no’t prime numbers]. This is done by making A[j] = 0 ( j ranging from 4 to 14, when j becomes 16, while loop terminates). 
  • Now at this point, all the multiples of 2 except the number 2 are removed from the array and made to zero. 
The array after this iteration is 
A={2,3,0,5,0,7,0,9,0,11,0,13,0,15}  
Multiples of 2 are removed

3. Next step is to make the multiples of 3 to ‘Zero’ except the number 3, since it is prime.
  • For next iteration of for loop, p=3 and j starts from 6 to 15. Making the value A[j]=0 at each j values. Loop terminates when j becomes 18. After this iteration, Array contains
A={2,3,0,5,0,7,0,0,0,11,0,13,0,0}    
Multiples of 3 are removed

4. Now p=4, which becomes false condition for the first ‘for loop’ and we now contain the array A,with prime numbers and '0's. We now need to transfer the elements which are not zero, to a new array ‘L’. That is done in third for loop. (Which is Self explanatory).


Now at last, return L, which contains the prime numbers. 

So L={2,3,5,7,11,13}, the required prime numbers.


[Algorithm] - Middle school procedure to find GCD of two numbers, with an example

middle school proc


This post will tell you in detail about "Middle school procedure to find GCD of two numbers with an example"



This algorithm calculates the Greatest common divisor (GCD) of two input numbers. This algorithm works on the basis of common divisors of the two input numbers. The steps are as follows,

To find GCD of two numbers m and n,
step 1: Find the prime factors of m. Go to step 2.
step 2: Find the prime factors of n. Go to step 3.
step 3: Identify all the common factors between m and n. proceed to step 4.
step 4: Compute the product of all the common divisors of the two numbers. and the product is the required GCD.


Example:
Finding GCD of two 60 and 24:
= > GCD(60,24)

Write 60 in terms of product of its prime factors,
=> 60 = 2 x 2 x 3 x 5

Write 24 in terms of product of its prime factors,
=> 24 = 2 x 2 x 2 x 3

Find the common occurrence of the prime factors in both the numbers
common numbers are = 2 , 2 , 3 .

Therefore,
GCD = Product (2 , 2 , 3) = 12

The required GCD of 60 and 24 is 12.


[Algorithm] - Consecutive Integer checking method to find GCD of two numbers, with an example

Consecutive Integer checking method


This post will tell you in detail about "Consecutive Integer checking method to find GCD of two numbers with an example"


This algorithm calculates the Greatest common divisor (GCD) of two input numbers. The steps are as follows,

To find GCD of two numbers m and n,


step 1: Assign the value of minimum of {m,n} to t.
step 2: Divide m by t. If the reminder of the division is zero, go to step 3. Otherwise go to step 4.
step 3: Divide n by t. If the reminder is zero, return the value of t as the answer and stop. Otherwise, proceed to step 4.
step 4: Decrease the value of t by ¡®1¡¯ and go to step 2.


Example:
Finding GCD of two 60 and 24: 
= > GCD(60,24)

Iterations:

1.T = min{60,24) = 24.
   Divide m=60 by T=24 and check the reminder.
   60%24=12. (not zero), so decrease t by 1. Now T=23.

2.T = 23.
   Divide m=60 by T=23 and check the reminder.
   60%23=14. (not zero), so decrease T by 1. Now T=22.

3.T = 22.
   Divide m=60 by T=22 and check the reminder.
   60%22=16. (not zero), so decrease T by 1. Now T=21.

4.T = 21.
   Divide m=60 by T=21 and check the reminder.
   60%21=18. (not zero), so decrease T by 1. Now T=20.

5.T = 20.
   Divide m=60 by T=20 and check the reminder.
   60%20=0. (Zero, so divide n=24 by T=20),
   24%20=4 (Not zero), so decrease T by 1. Now T=19.

And so on....


13.At 13th iteration, T=12.
   Divide m=60 by T=12 and check the reminder.
   60%12=0. (Zero, so divide n=24 by T=12),
   24%12=0 (Zero), We got the GCD.
   Return value of t as the answer. 


The required GCD of 60 and 24 is 12.


[Algorithm] - Euclid's Algorithm to find GCD of two numbers, with an example

Euclid algorithm

This post will tell you in detail about "Euclid's Algorithm to find GCD of two numbers with an example"

This algorithm calculates the Greatest common divisor (GCD) of two input numbers. This algorithm uses recursive method of problem solving.

CODE:
ALGORITHM Euclid(m,n)
{
     if(n=0)
          return m
     else
      {
               R=m mod n
               m=n
               n=R
               return Euclid(m,n)
      }
}


Recursive technique:
 GCD (m , n ) = GCD ( n , m mod n )

The steps are as follows,

To find GCD of two numbers m and n, 
step 1: If n=0, return the value of m as the answer. Otherwise, proceed to step 2.
step 2: Divide m by n and assign the value of the reminder to R.
step 3: Assign value of n to m, and value of R to n. Go to step 1.


Example:
Finding GCD of two 60 and 24: 
= > GCD(60,24)

Recursive calls

1.Value of n=24 (not zero), 
   R= m mod n = 60%24 = 12.
   m=24
   n=12

2.Value of n=12 (not zero)
   R= m mod n = 24%12= 0.
   m=12
   n=0

3.Value of n=0 (Zero)
   Return value of m as the answer. I.e., m=12 is the answer.



The required GCD of 60 and 24 is 12.