google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Algorithm of sorting

Friday, June 27, 2014

Algorithm of sorting

An algorithm to sort an elements using Insertion Sort.

Algorithm for Insertion sort:-
Step 1 : START
Step 2 : Input N
Step 3 : For I←0 to N -1
Read A [ I ]
( end of I for loop )
Step 4 : For I←1 to N -1 Do
Step 5 : J = I
Step 6 : While ( J > = 1 ) and ( A[ J ] < A [ J -1 ] )
Step 7 : TEMP = A [ J ]
Step 8 : A [ J ] = A [ J - 1 ]
Step 9 : A[ J - 1 ] = TEMP
Step 10 : J = J - 1
[ end of While loop ]
[ end of step I For loop ]
Step 11 : For I←0 to N -1
Step 12: Print A [ I ]
Step 13 : Stop

Consider the elements
A[0 ]    20
A[1]    16
A[2]    4
A [3]    3

Pass 1:
A[1],A[0]>16,20,4,3
Pass 2:
A[2],A[1]> 16,4,20,3
A[1],A[0]>4,16,20,3
Pass 3:
A[3],A[2]> 4,16,3,20
A[2],A[1]>4,3,16,20
A[1],A[0]> 3,4 16,20
Sorted order is 3,4,16,20

Algorithm for bubble sort:-

Step 1 : START
Step 2 : Input N
Step 3 : For I←0 to N -1
Step 4 : Read ( A [ I ] )
( end of for loop )
Step 5 : For I←1 to N -1
Step 6 : For J←0 to N –I - 1
Step 7 : If (A[ J ] > A [ J +1] )
Temp←A [ J ]
A [ J ]←A [ J + 1 ]
A [ J +1 ]←Temp
[ end of if ]
[ end of J loop ]
[ end of I loop ]
Step 8 : For J←0 to N -1
Print A [ J ]
Step 9 : Stop

Consider the following elements: 50 , 20, 10 , 05
Pass 1:- 50    20    10    05
             20    50    10     05
             20    10     50    05
             20     10    05    50
Pass 2:-  20    10    05    50
               10    20    05    50
               10    05    20     50
Pass 3:- 10    05    20    50
             05    10     20    50

Sorted order is 5, 10, 20 , 50

 Algorithm for selection sort:-

Step 1 : START
Step 2 : Input N
Step 3 : For I←0 to N -1
Step 4 : Read A [ I ]
Step 5 : For I←0 to N -2
Step 6 : S←A [ I]
Step 7 : Pos←I
Step 8 : For J←I + 1 to N -1
Step 9 : If ( A[ J] < S )
Step 10 : S←A [ J ]
Step 11 : Pos←J
[ end of if ]
[ end of J For loop ]
Step 12 : A [ Pos ]←a[ I ]
Step 14 : A[ I ]←S
[end of I For loop ]
Step 15: For I = 0 to N – 1
Step 16: Print A[ I ]
Step 17: Stop

An algorithm to find the maximum in an array:-

Step 1 : [Assume 1st element is the largest ]large = A [ 0 ]
Step 2 : POS = 0
Step 3 : [ Find the largest element in the array and its position]For I = 1 to N – 1 Do
Step 4 : If ( A [ I ] > large ) Then
Step 5 : large = A [ I ]
Step 6 : POS = 1
[ End if ]
[ End of step 3 for loop ]
Step 7: [ Print the largest and its position]Print “ Largest = “ , large , “ position = “ , pos
Step 8: Exit


An algorithm to find the minimum in an array:-

Step 1 : [Assume 1st element is the smallest]small = A [ 0 ]
Step 2 : POS = 0
Step 3 : [ Find the smallest element in the array and its position]
For I = 1 to N – 1 Do
Step 4 : If ( A [ I ] < small ) Then
Step 5 : small = A [ I ]
Step 6 : POS = 1
[ End if ]
[ End of step 3 for loop ]
Step 7: [ Print the smallest and its position]
Print “ smallest = “ , small , “ position = “ , pos
Step 8: Exit


Linear search method with an algorithm:-

Step 1 : START
Step 2 : input N
Step 3 : for I←0 to N -1
Read A [ I ]
Step 4 : Loc = -1
Step 5 : For I = 0 to N- 1 do
Step 6 : If ( ele = A[I]) Then
Step 7: Loc = I
Step 8 : Goto step 9
[ end if ]
[ end for ]
Step 9 : If ( Loc > = 0 ) then
Step 10: Print “ ele Found in location “ , Loc
Step 11: else
Step 12: Print “ ele not found ”

Step 13 Stop






 Algorithm for Binary Search:


Step 1: Low = 0
Step 2 : High = N – 1
Step 3 : Loc = -1
Step 4 : While ( Low < = High ) Do
Step 5 : M = ( Low + High ) / 2
Step 6 : If (ele = A[M ] ) Then
Step 7: Loc = M goto Step 12
[ end if ]
Step 8 : If (ele < A [M ] ) Then
 Step 9: High←M – 1
Step 10 : Else
Step 11 : Low←M + 1
[ end of if ]
[ end of While loop ]
Step 12 : if ( Loc > = 0 ) Then
Step 13: Print “ element , found , search successful ” , Loc
Step 14: else
Step 15 : Print “ Item not found , search Unsuccessful ”
[ end if ]
Step 16 : Stop

No comments:

Post a Comment

रामायण

रामायण दशरथ की तीन पत्नियाँ – कौशल्या, सुमित्रा , कैकेयी दशरथ के चार पुत्र – राम,लक्ष्मण,भरत,शत्रुघ्न दशरथ: राम के पिता और कौशल के राजा कौशल...