540 likes | 658 Views
SORTING METHODS FOR B.TECH. Bubble Sort. This sorting technique requires n-1 passes to sort array of ‘n’ integers Algorithm : for i n-2 down to 0 for j 0 to I if ( a [j] > a [j+1]) swap [j] ,a [j+1]. Example:
E N D
Bubble Sort This sorting technique requires n-1 passes to sort array of ‘n’ integers Algorithm : for i n-2 down to 0 for j 0 to I if ( a [j] > a [j+1]) swap [j] ,a [j+1]
Example: Q. Show that the steps in which following arrays will get sorted in increasing order using bubble sort 5 0 2 -3 a 0 1 2 3
Pass I: Compare : a [0] , a [1] a [1] , a [2] a [2] , a [3] 0 2 -3 5 a 0 1 2 3
Pass II: Compare : a [0] , a [1] a [1] , a [2] 0 -3 2 5 a 0 1 2 3
Pass III: • Compare : a [0] , a [1] Pass II: Compare : a [0] , a [1] This is required sorted array…. -3 0 2 5 a 0 1 2 3
Program : #include<stdio.h> #include<conio.h> bubble(int a[],int n) { int i,j,t; for(i=n-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } } /* end bubble */
void main() { int a[100],i,n; printf("Enter no of elements:\n"); scanf("%d",&n); /* scan element of array */ for(i=0;i<=n-1;i++) { printf("Enter element :%d\t",i+1); scanf("%d",&a[i]); } bubble (a,n); /* print sorted array */ for(i=0;i<=n-1;i++) printf("%d\t",a[i]); } /* end main */
Selection Sort It requires n-1 passes to sort array of ‘n’ elements. In pass ‘i’ we select minimum value from location ‘i’ to location ‘n-1’.And this value is swapped with value of a [i]. Example : Sort an array using selection sort 5 0 8 -3 6 a 0 1 2 3 4
Pass 0 : minimum element should be replaced with 0th element. replaced a -3 0 8 5 6 0 1 2 3 4
Pass I : minimum element should be replaced with 1st element. No replacement a -3 0 8 5 6 0 1 2 3 4
Pass II : minimum element should be replaced with 2nd element. replaced a -3 0 5 8 6 0 1 2 3 4
Pass III : minimum element should be replaced with 1th element. This is required sorted array…. replaced a -3 0 5 6 8 0 1 2 3 4
Program : #include<stdio.h> #include<conio.h> Void Selsrt (int a [], Int n) { int min,i,t,j,p; for(i=1;i<=n-1;i++) { min=a[i]; p=i; for(j=i;j<=n-1;j++) { if(a[j]<min) { min=a[j]; p=j; } /* swap a [p], a [i] */ t=a [p]; a [p]=a [i]; a [i]=t; } /* end of first for loop */ } /* end of second for loop */ } /* end of selsrt */
void main() { int a[100],i,n; printf("Enter no of elements:\n"); scanf("%d",&n); /* scan element of array */ for(i=0;i<=n-1;i++) { printf("Enter element :%d\t",i+1); scanf("%d",&a[i]); } selsrt(a,n); /* print sorted array */ for(i=0;i<=n-1;i++) printf("%d\t",a[i]); } /* end main */
Insertion Sort It requires n-1 passes. Initially array is divided in to two partitions such tat sorted partition contains only one element and unsorted partition contains rest of elements. In every pass next element of unsorted part is inserted in the sorted part so that sorted part remains sorted. Example : Sort an array using selection sort 5 0 -1 8 3 a 0 1 2 3 4
sorted unsorted Given array : Pass I : Insert ‘0’ in to sorted partetion 5 0 -1 8 3 a 0 1 2 3 4 sorted unsorted 0 5 5 0 -1 8 3 a 0 1 2 3 4
sorted unsorted Array after pass I Pass II : Insert ‘-1’ in to sorted partition 0 5 -1 8 3 a 0 1 2 3 4 sorted unsorted -1 0 5 0 5 -1 8 3 a 0 1 2 3 4
sorted unsorted Array after pass II Pass III: Insert ‘8’ into sorted partition -1 0 5 8 3 a 0 1 2 3 4 sorted unsorted -1 0 5 8 3 a 0 1 2 3 4
sorted unsorted Array after pass III Pass IV : Insert ‘3’ into sorted portion -1 0 5 8 3 a 0 1 2 3 4 sorted unsorted -1 0 5 8 3 a 0 1 2 3 4
Array after pass IV This is final sorted array….. -1 0 3 5 8 a 0 1 2 3 4
Program : #include<stdio.h> #include<conio.h> insertion(int a[],int n) { int i,j,x; for(i=1;i<=n-1;i++) { j=i; x=a[i]; while(a[j-1]>x && j>0) { a[j]=a[j-1]; j=j-1; } a[j]=x; } }
void main() { int a[100],i,n; printf("Enter no of elements:\n"); scanf("%d",&n); /* scan element of array */ for(i=0;i<=n-1;i++) { printf("Enter element :%d\t",i+1); scanf("%d",&a[i]); } insertion(a,n); /* print sorted array */ for(i=0;i<=n-1;i++) printf("%d\t",a[i]); } /* end main */
Heap Sort Heap is defined as a sequential binary tree in which values are always added sequentially from left to right. Any array can be represented in the form of heap. Following rule will be always true For any child C : parent p = c/2 For any parent P : C (left)= P*2 C (right)= P*2 + 1
Example : Suppose Given array is Heap of above array is : 100 50 250 150 300 5 650 155 a 0 1 2 3 4 5 6 7 8 100 1 50 2 250 3 150 4 650 7 300 5 5 6 155 8
Max heap : It is a sequential binary tree in which every parent node must have a value larger than both the children. Min heap : It is a sequential binary tree in which every parent node must have a value lesser than both the children. Process of Heap sort : Heap sort works in two stages, 1) Convert the given array ‘a’ in to max heap. 2) Exchange a1 with a8. So 8th element is largest. And adjust first parent to convert remaining part of array again in the form of max heap.
Stage I :Convert the given array into max heap i.e. following conditions must be satisfied a [1] > a[2] , a[3] a [2] > a[4] , a[5] a [3] > a[6] , a[7] a [4] > a[8]
100 1 50 2 250 3 X = 150 Adjust value for parent ‘4’ Adjust value for parent ‘3’ 155 150 4 650 7 300 5 5 6 150 155 8 100 1 X = 250 650 50 2 250 3 250 155 4 650 7 300 5 5 6 150 8
100 1 300 50 2 650 3 Adjust value for parent ‘2’ Adjust value for parent ‘1’ 155 4 250 7 300 5 5 6 50 150 8 650 100 1 250 300 2 650 3 100 155 4 250 7 50 5 5 6 150 8
650 1 Final max heap will be ….. 300 2 250 3 155 4 100 7 50 5 5 6 150 8 650 300 250 155 50 5 100 150 a 0 1 2 3 4 5 6 7 8
Stage II : Exchange a1 with a8. So 8th element is largest. Repeat the stage I again for remaining part… And so on… 150 300 250 155 50 5 100 650 a 0 1 2 3 4 5 6 7 8 Largest element
Program : #include <stdio . h> adjust(int a[],int i,int n) { int j,x; j=i*2; x=a[i]; while(j<=n) { if(j<n && a[j]<a[j+1]) j=j+1; if(x>a[j]) break; a[j/2]=a[j]; j=j*2; }/ * end while */ a[j/2]=x; }/*end adjust */
void main() { int a[100],n,i,t; printf("Enter no of elements :\n"); scanf("%d",&n); /* scan array from 1 to n */ for(i=1;i<=n;i++) { printf("Enter element:%d ",i); scanf("%d",&a[i]); } /* heapify */ for(i=n/2;i>=1;i--) adjust(a,i,n); /* Now heap sort */ for(i=n;i>=2 ;i--) { t=a[1]; a[1]=a[i]; a[i]=t; adjust(a,1,i-1);/* reheapify */ } / * print sorted array */ for(i=1;i<=n;i++) printf("%d\t",a[i] ); }
Radix sort Program : # include <stdio.h> #include <conio.h> void radix (int a[], int n, int m) { int z, i, j, exp=1, count[10], bucket[100][10],k; int digit,col,row; for (z=1; z <= m; z++) { /* initialize all counts */ for (i=0; i<=9; i++) {count[i]= -1;} /* map all values of array in bucket */ for(i=0; i<=n-1;i++) { digit = (a[i]/exp) % 10; col = digit; count[digit]+1; row = count[digit]; bucket[row][col] = a[i]; }
/* copy of all buckets back in to array */ k = 0; for( i=0; i<=9; i++) { if (count[i] != -1) { for (j = 0; j <= count[j]; j++) { a[k] = bucket[j][i]; k++; } } } /* end of for loop */ exp = exp * 10; } /* end of major for loop */ } /* end radix sort */
void main() { int a[100], n, i; printf ("enter no of elements :"); scanf ("%d", &n); /* scan array fro 0 to n-1 */ for (i=0; i<= n-1; i++) { printf("enter element %d",i+1); scanf("%d", &a[i]); } radix (a, n, 3); /* print sorted array */ for(i=0; i <= n-1; i++) printf ("%d", a[i]); }
Quick Sort Functions : int partition(int a[],int l,int r) { int i,j,x,t; j=r; i=l ;x=a[l]; while(i<j) { while(a[i]<=x&&i<=r) i++; while (a[ j ]>x) j--; }
if (i<j) { t=a[i]; a[i]=a[j]; a[j]=t; } t=a[ l ]; a[ l ]=a[ j ]; a[ j ]=t; return j; }
Quick (int a[],int l,int r) { int p; if (l < r) p=partition (a,l,r); quick(a,l,p-1); quick(a,p+1,r); }
Linear Search It is also called as sequential search. In this values will be given and u have to only search the value whether present in array or not 150 300 250 155 50 a 0 1 2 3 4
Program : # include<stdio.h> # include <conio.h> Void linear (int a [],int n) { int i, x; printf (“enter the value to be searched”) ; scanf (“%d”,&x); i=0; while (a[i] !=x && I <= n-1) { i++; } if (i > n-1) printf(“value is not found \n”); else printf(“value found at %D \n”,i); }
Void main() { int a[100], n, I; printf (“enter no. of elements”); scanf (“%d”, &n); /* scan array from 0 to n-1 */ for (i=0;i<=n-1;i++) { printf (“enter element %d”, i+1); scanf (“%d”, & a[i]); } linear(a,n); }
Binary search • For binary search array should be sorted • Binary search is more faster than linear seach. 5 7 10 15 28 32 36 38 42 a 0 1 2 3 4 5 6 7 8
Examples : 1) Search x=10 in above array found
Program : # include<stdio.h> # include <conio.h> Void binary (int a[], int n) { int low high, mid, x; printf(“enter value to search”); scanf(“%d”,&x); low=0; high=n-1; mid = (low+high)/2;
while (low <= high && a[mid] != x) { if (a [mid]<x) low = mid +1; else high = mid -1; mid = (low+high)/2; } If (low >high) printf (“No such value”); else printf (“value is found at place %d”,mid); }
Void main() { int a[100], n, I; printf (“enter no. of elements”); scanf (“%d”, &n); /* scan array from 0 to n-1 */ for (i=0;i<=n-1;i++) { printf (“enter element %d”, i+1); scanf (“%d”, & a[i]); } binary(a,n); }
Hash Search Hashing is a searching technique in which all keys are mapped to some address of hash table. Mapping is done by Hash function. Hash function is a simple arithmetic function which transforms a kye in to address. Example : key % 10
Properties of a good hash function: • It should be easily computable. • It should give minimum collision. Collision : If two keys K1 and K2 get mapped to the same address ‘X’ and if K1 is already stored at that address then K2 can not be stored at that address. This is called as collision.
Collision handling Techniques : Linear probing :If two keys K1 and K2 get mapped to the same address ‘X’ and if K1 is already stored at that address then new address of K2 will be searched linearly (X+1, X+2, X+3,…) till vacant place is found where K2 can be stored. Hash function Key % 10 55 72 88 65 76 77 98 109 105 a 0 1 2 3 4 5 6 7 8 72 55 65 76 88 77 98 109 105 h 0 1 2 3 4 5 6 7 8 9 10 11 12 65 105 76 77 98 109