sort and searches algorithm(bubble sort, insertion sort, selection sort, linear search, binary search)

 

Bubble Sort

  1. Start

  2. Declare size of arrayA [] and temp variable

  3. Apply, for( i=0 ; i<n-1 ; i++)

  4. If it is true then go to inner loop

  5. For( j=0 ; j<n-1-i ; j++) //Inner Loop//

  6. If A[ i ] > [ j+1];

  7. If it is true then execute following statements;

temp = A [j]

A [j] = A [j+1]

A [j+1] = temp;

  1. End if

  2. End for loop //inner loop//

  3. End



Insertion Sort



  1. Start

  2. Declare size of arrayA [ ] and temp variable

  3. Apply, for( i=1 ; i<n ; i++ )

  4. If it is true then

  5. temp = A[ i ]

j = i-1;

  1. then execute while loop

While (j >=0 && A[ j ] > temp)

  1. A[ j+1] = A[ j ] ;

j- - ;

  1. End while loop

  2. A[ j+1 ] = temp;

  3. End



Selection Sort



  1. Start

  2. Declare size of arrayA [] and min variable.

  3. Apply , for( i=0 ; i<n-1 ; i++ ) //outer loop//

  4. int min = i;

  5. for( j=i+1 ; j<n ; j++) //inner loop//

  6. then execute if statement

if ( A[ j ] < A[ min ]

then min = j;

  1. end if statement

  2. end for loop //inner loop//

  3. if (min! = i)

  4. then , swap A[i] , A[min]

  5. end if

  6. end



Linear Search

  1. Start

  2. Apply , for( i=0; i<n; i++)

  3. If condition is true then check

  4. If( a[i] == data )

  5. Print (“element found”)

  6. Break;

  7. End if

  8. If (i==n)

  9. Then print (“element not found”)

  10. End



Binary Search

  1. Start

  2. Create a function

  3. Binary search(a, n, data)

  4. L=0, R=n-1;

  5. While (L<R)

  6. Mid = (L+R)/2

  7. Then check; if( data == a[mid] )

  8. Return mid;

  9. Else if (data<a[mid])

  10. Then R= mid-1;

  11. Else

  12. L= mid+1;

  13. End else if

  14. Then simple, return -1;

  15. End

__________________________________________________________

Comments

Popular posts from this blog

Requirements Elicitation Techniques

Quiz game project in cpp