sort and searches algorithm(bubble sort, insertion sort, selection sort, linear search, binary search)
Bubble Sort
Start
Declare size of arrayA [] and temp variable
Apply, for( i=0 ; i<n-1 ; i++)
If it is true then go to inner loop
For( j=0 ; j<n-1-i ; j++) //Inner Loop//
If A[ i ] > [ j+1];
If it is true then execute following statements;
temp = A [j]
A [j] = A [j+1]
A [j+1] = temp;
End if
End for loop //inner loop//
End
Insertion Sort
Start
Declare size of arrayA [ ] and temp variable
Apply, for( i=1 ; i<n ; i++ )
If it is true then
temp = A[ i ]
j = i-1;
then execute while loop
While (j >=0 && A[ j ] > temp)
A[ j+1] = A[ j ] ;
j- - ;
End while loop
A[ j+1 ] = temp;
End
Selection Sort
Start
Declare size of arrayA [] and min variable.
Apply , for( i=0 ; i<n-1 ; i++ ) //outer loop//
int min = i;
for( j=i+1 ; j<n ; j++) //inner loop//
then execute if statement
if ( A[ j ] < A[ min ]
then min = j;
end if statement
end for loop //inner loop//
if (min! = i)
then , swap A[i] , A[min]
end if
end
Linear Search
Start
Apply , for( i=0; i<n; i++)
If condition is true then check
If( a[i] == data )
Print (“element found”)
Break;
End if
If (i==n)
Then print (“element not found”)
End
Binary Search
Start
Create a function
Binary search(a, n, data)
L=0, R=n-1;
While (L<R)
Mid = (L+R)/2
Then check; if( data == a[mid] )
Return mid;
Else if (data<a[mid])
Then R= mid-1;
Else
L= mid+1;
End else if
End
__________________________________________________________
Comments
Post a Comment