You are on page 1of 9

SEARCHING AND SORTING

Searching is the process of finding an element within a list of elements stored in any order. If the element is found then search is successful otherwise unsuccessful. Sorting is the process of arranging data in some sequence i.e. in ascending or descending order. The advantage of doing sorting is to perform the search operation very fastly. Eg: Telephone directory Searching Algorithms Sorting Algorithms

Linear Search Merge

Binary Search Bubble Insertion Selection Quick

Sort Sort

Sort

Sort

Sort

Sorting Algorithms Bubble/exchange sort: In this technique each element is compared with its adjacent element. If the first element is larger than second then the position of elements are interchanged otherwise not. Then next element is compared with adjacent element and same process is repeated for all elements in the list. After completion of these pass the largest element occupies the last position. During the next pass the same process is repeated leaving the largest element. During this pass, the largest element occupies the n-1th position The same process is repeated until no more elements are left for comparison, and finally the array is sorted one. Algorithm: Program Code: 1. initialization set i=0 #include <stdio.h> 2.repeated steps 3 to 5 until i<n #include <conio.h> 3.set j=0 void main( ) 4.repeat step 5 until j<n-i-1 { 5.if a [ j ] > a[ j+1 ] then int arr[10] ; Set temp=a[ j ] int i, j, temp,n ; Set a[ j ]=a[ j+1 ] printf(enter size of array\n);

scanf(%d,&n); printf("Enter %d elements to the array\n",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); for ( i = 0 ; i < n ; i++ ) { for ( j = 0 ; j < n i -1 ; j++ ) { if ( arr[j] > arr[j + 1] ) { temp = arr[j] ; arr[j] = arr[j + 1] ; arr[j + 1] = temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i <n ; i++ ) printf ( "%d\t", arr[i] ) ; } Example: Initial elements are 11 15 2 13 6 Set a[ j+1 ]=temp End if 6. Exit Pass 0: a[0] a[1] 11 15 11 11 11 11 15 2 2 2 Pass 1: a[0] a[1] 11 2 2 2 2 11 11 11

a[2] a[3] a[4] 2 13 6 no swap 2 15 13 13 13 13 15 6 6 6 6 15 swap 15 2 swap 15 13 swap 15 6 largest element 15 is fixed

a[2] a[3] a[4] 13 6 15 swap 11 2 13 13 6 6 6 13 15 no swap

15 swap 13 6 15 next largest element 13 is fixed

Pass 2: a[0] a[1] 2 11 2 2 11 6

a[2] a[3] a[4] 6 13 15 no swap 6 11 13 13 15 swap 11 6

Pass 3: a[0] a[1] 2 6 2 6

a[2] a[3] a[4] 11 13 15 no swap 11 13 15 next largest element 6 is fixed

15 next largest element 11 is fixed

2. Selection sort: The selection sort technique is based upon the extension of the minimum or maximum technique. In each and every pass we can select min and location and perform swapping min and loc if min is greater than location otherwise not. Location specifies the position of the minimum element in the rest of the list other than min. If min and loc are same no swapping is done otherwise swap the elements at loc and min. For each and every the smallest element is fixed in their corresponding locations. Example: a[0] a[1] a[2] a[3] a[4] 16 15 2 13 6 pass1 a[0] a[1] a[2] a[3] a[4] 16 15 2 13 6 min a[0] a[1] 2 15 loc swap 16 and 2 a[2] a[3] a[4] 16 13 6

pass2

pass3

a[0] 2

min loc swap 15 and 6 a[1] a[2] a[3] a[4] 6 16 13 15 min loc swap 16 and 13

pass4

a[0] 2

a[1] 6

a[2] a[3] a[4] 13 16 15 min loc swap 16 and 15 a[2] a[3] a[4] 13 15 16 min

pass5

a[0] 2

a[1] 6

Algorithm: Procedure min(a,k,n,loc) An array a is in memory.this procedure finds the location loc of the smallest element among a[k],a[k+1]..a[n] during k pass Set min=a[k] and loc=k Repeat for j=k+1,k+2n if min>a[j] then

Program Code: #include<stdio.h> void main() { int n,i,j,temp,a[20]; printf("Enter total elements: "); scanf("%d",&n); printf("Enter %d elements: ",n); for(i=0;i<n;i++)

set min=a[j] and loc=j end if Return Selection Sort a[maxsize],n Repeat steps 2 & 3 for k=1,2n-1 Call min(a,k,n,loc) Set temp=a[k ] Set a[k ]=a[loc ] Set a[loc ]=temp exit

scanf("%d",&a[i]); selection_sort(a,n); printf("After sorting is: "); for(i=0;i<n;i++) printf(" %d",a[i]); } void selection_sort(int a[ ], int n) { int min,loc,temp,i,j; min=a[0]; for(i=0;i<n;i++) { min=a[i]; loc=i; for(j=i+1;j<n;j++) { if(a[j]<min) { min=a[j]; loc=j; } } if(loc!=i) { temp=a[i]; a[i]=a[loc]; a[loc]=temp; } } }

(OR) Selection sort is also implemented in another way as follows: Pass 0: Pass 1: a[0] a[1] a[2] a[3] a[4] a[0] a[1] a[2] a[3] a[4] 11 15 2 13 6 no swap 2 15 11 13 6 swap 15 11 11 2 2 2 15 15 15 15 2 11 11 11 13 13 13 13 6 6 6 6 swap 11 2 no swap no swap smallest element 2 is fixed 2 2 2 11 11 6 15 15 15 13 13 13 6 6 11 no swap swap 11 6 next smallest element 6 is fixed

Pass 2: a[0] a[1] 2 6 2 2 6 6

a[2] a[3] a[4] 15 13 11 swap 15 13 13 11 15 13 11 swap 13 11

Pass 3: a[0] a[1] 2 6 2 6

a[2] a[3] a[4] 11 13 15 no swap 11 13 15 next smallest element 13 is fixed

15 next smallest element 11 is fixed

and finally there is only one element 15 also in order

Algorithm: 1. initialize set i=0 2.repeat steps 3 to 5 until i<n 3.set j=i+1 4.repeat step 5 until j<n 5.if a [ i ] > a[ j ] then Set temp=a[ i ] Set a[ i ]=a[ j ] Set a[ j ]=temp End if 6. Exit

Program Code: #include <stdio.h> #include <conio.h> void main( ) { int arr[10] ; int i, j, temp,n ; printf(enter size of array\n); scanf(%d,&n); printf("Enter %d elements to the array\n",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); for ( i = 0 ; i < n ; i++ ) { for ( j = i+1 ; j < n ; j++ ) { if ( arr[i] > arr[j] ) { temp = arr[i] ; arr[i] = arr[j] ; arr[j] = temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i <n ; i++ ) printf ( "%d\t", arr[i] ) ; }

3. Insertion sort: In this algorithm, we select a element and that selected element is compared with remaining previous elements and insert that selected element in its appropriate position. In the comparison, if selected element is smaller than previous elements then perform either swapping and shifting the positions of all elements by 1 position. Example: a[0] a[1] a[2] a[3] a[4]

16

15

13

Compare a[1] with a[0], clearly a[1]<a[0] so insert a[1] before a[0] a[0] a[1] a[2] a[3] a[4] 15 16 2 13 6

Compare a[2] with a[0] and a[1], clearly a[2]<a[0] and a[1] so insert a[2] before a[0] a[0] a[1] a[2] a[3] a[4] 2 15 16 13 6

Compare a[3] with a[0], a[1], a[2] , clearly a[3]>a[0] a[3]<a[1] and a[2] so insert a[3] before a[1]

a[0] a[1] a[2] a[3] a[4] 2 15 16 13 6

Compare a[4] with a[0], a[1], a[2], a[3] , clearly a[3]>a[0] a[3]<a[1], a[2], a[3] so insert a[4] before a[1] a[0] a[1] a[2] a[3] a[4] 2 6 13 16 15

Algorithm: 1. initialize set k=1 2.repeat steps 3 to 6 until k<=n-1 3. set temp=a[k] 3.set j=k-1 4.while temp<a[j] and j>=0 then 5.Set a[ j+1 ]=a[j] Set j=j-1 End loop 6. set a[j+1]=temp 7. Exit

Program Code: #include <stdio.h> #include <conio.h> void main( ) { int arr[10] ; int k, j, temp,n ; printf(enter size of array\n); scanf(%d,&n); printf("Enter %d elements to the array\n",n); for(k=0;k<n;k++) scanf("%d",&arr[k]);

for ( k = 0 ; k < n -1 ; k++ ) { temp=a[k]; j=k-1; while (temp<a[j] and j>=0) { a[ j+1 ]=a[j]; j=j-1; } a[j+1]=temp } printf ( "\n\nArray after sorting:\n") ; for ( k = 0 ; k <n ; k++ ) printf ( "%d\t", arr[k] ) ; } Quick sort/ Partition Exchange sort: Example: 0 16 1 15 2 12 3 11 4 6 5 10

select left=0, right=6 and newleft, newright=6 pivot=a[(left+right)/2]=a[(0+5)/2]=a[2]=12 Increment newleft by 1 until a[newleft]<pivot from left to right Decrement newright by 1 until pivot<a[newright] from right to left 0 16 1 15 2 12 pivot 3 13 4 6 5 10

newleft

newright

Here newleft<=newright(0<=5) swap a[newleft] and a[newright] i.e swap 16 and 10 0 1 2 3 4 5 10 15 12 13 6 16 newleft pivot newright Increment newleft by 1 until a[newleft]<pivot from left to right Decrement newright by 1 until pivot<a[newright] from right to left Here newleft<=newright(1<=4) swap a[newleft] and a[newright] i.e swap 15 and 6 0 1 2 3 4 5 10 6 12 13 15 16 newright pivot newleft

Here newleft>newright(3>1) partition the given array into 2 halves 1st upto newright and 2nd up to right 0 10 13 1 2 6 12 15 16 3 4 5

Apply above steps repeatedly for each and every partition Parition1: 0 1 2 10 6 12 select left=0, right=2 and newleft, newright=6 pivot=a[(left+right)/2]=a[(0+2)/2]=a[1]=6 Increment newleft by 1 until a[newleft]<pivot from left to right Decrement newright by 1 until pivot<a[newright] from right to left 0 1 2 10 6 12 newleft pivot newright Here newleft<=newright(0<=1) swap a[newleft] and a[newright] i.e swap 10 and 6 0 6 1 10 2 12 Parition2: 3 4 5 13 15 16 select left=0, right=2 and newleft, newright=6 pivot=a[(left+right)/2]=a[(3+5)/2]=a[4]=15 Increment newleft by 1 until a[newleft]<pivot from left to right Decrement newright by 1 until pivot<a[newright] from right to left 3 4 5 13 15 16 newright pivot newleft Here newleft>newright(5>3) partition the given array into 2 halves 1st upto newright and 2nd up to right 3 13 15 Parition4: 4 15 16 5 4 16 5

newright newleft pivot Here newleft>newright(1>0) partition the given array into 2 halves 1st upto newright and 2nd up to right 0 6 10 1 12 2

Parition3: 1 10 12 2

select left=4, right=5 and newleft, newright=6 pivot=a[(left+right)/2]=a[(4+5)/2]=a[4]=15 Increment newleft by 1 until a[newleft]<pivot from left to right Decrement newright by 1 until pivot<a[newright] from right to left 4 5 15 16

select left=1, right=2 and newleft, newright=6 pivot=a[(left+right)/2]=a[(1+2)/2]=a[1]=10 Increment newleft by 1 until a[newleft]<pivot from left to right Decrement newright by 1 until pivot<a[newright] from right to left 1 2 10 12

pivot newleft newright Here newleft>newright(5>4) partition the given array into 2 halves 1st upto newright and 2nd up to right 4 15 16 5

pivot newleft newright Here newleft>newright(2>1) partition the given array into 2 halves 1st upto newright and 2nd up to right 1 10 12 2

Finally the sorted array using quick sort is 0 1 2 3 10 12 13 15 16 4 5

You might also like