54
SORTING METHODS FOR B.TECH

SORTING METHODS FOR B.TECH

Embed Size (px)

DESCRIPTION

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: - PowerPoint PPT Presentation

Citation preview

Page 1: SORTING METHODS FOR B.TECH

SORTING METHODS FOR B.TECH

Page 2: 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]

Page 3: SORTING METHODS FOR B.TECH

Example:

Q. Show that the steps in which following

arrays will get sorted in increasing order

using bubble sort

5 0 2 -3a

0 1 2 3

Page 4: SORTING METHODS FOR B.TECH

Pass I:

Compare : a [0] , a [1]

a [1] , a [2]

a [2] , a [3]

0 2 -3 5a

0 1 2 3

Page 5: SORTING METHODS FOR B.TECH

Pass II:

Compare : a [0] , a [1]

a [1] , a [2]

0 -3 2 5a

0 1 2 3

Page 6: SORTING METHODS FOR B.TECH

Pass II:

Compare : a [0] , a [1]

This is required sorted array….

Pass III:

Compare : a [0] , a [1]

-3 0 2 5a

0 1 2 3

Page 7: SORTING METHODS FOR B.TECH

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 */

Page 8: SORTING METHODS FOR B.TECH

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 */

Page 9: SORTING METHODS FOR B.TECH

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 -3a

0 1 2 3 4

6

Page 10: SORTING METHODS FOR B.TECH

Pass 0 :

minimum element should be replaced with 0th element.

-3 0 8 5a

0 1 2 3 4

6

replaced

Page 11: SORTING METHODS FOR B.TECH

Pass I :

minimum element should be replaced with 1st element.

-3 0 8 5a

0 1 2 3 4

6

No replacement

Page 12: SORTING METHODS FOR B.TECH

Pass II :

minimum element should be replaced

with 2nd element.

-3 0 5 8a

0 1 2 3 4

6

replaced

Page 13: SORTING METHODS FOR B.TECH

Pass III :

minimum element should be replaced

with 1th element.

This is required sorted array….

-3 0 5 6a

0 1 2 3 4

8

replaced

Page 14: SORTING METHODS FOR B.TECH

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 */

Page 15: SORTING METHODS FOR B.TECH

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 */

Page 16: SORTING METHODS FOR B.TECH

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 8a

0 1 2 3 4

3

Page 17: SORTING METHODS FOR B.TECH

Given array :

Pass I :

Insert ‘0’ in to sorted partetion

5 0 -1 8a

0 1 2 3 4

3

5 0 -1 8a

0 1 2 3 4

3

sorted unsorted

0 5

sorted unsorted

Page 18: SORTING METHODS FOR B.TECH

Array after pass I

Pass II :

Insert ‘-1’ in to sorted partition

0 5 -1 8a

0 1 2 3 4

3

sorted unsorted

-1 0 5

0 5 -1 8a

0 1 2 3 4

3

sorted unsorted

Page 19: SORTING METHODS FOR B.TECH

Array after pass II

Pass III:

Insert ‘8’ into sorted partition

-1 0 5 8a

0 1 2 3 4

3

sorted unsorted

-1 0 5 8a

0 1 2 3 4

3

sorted unsorted

Page 20: SORTING METHODS FOR B.TECH

Array after pass III

Pass IV :

Insert ‘3’ into sorted portion

-1 0 5 8a

0 1 2 3 4

3

sorted unsorted

-1 0 5 8a

0 1 2 3 4

3

sorted unsorted

Page 21: SORTING METHODS FOR B.TECH

Array after pass IV

This is final sorted array…..

-1 0 3 5a

0 1 2 3 4

8

Page 22: SORTING METHODS FOR B.TECH

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;

}}

Page 23: SORTING METHODS FOR B.TECH

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 */

Page 24: SORTING METHODS FOR B.TECH

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

Page 25: SORTING METHODS FOR B.TECH

Example :

Suppose Given array is

Heap of above array is :

100 50 250a

0 1 2 3 4 5 6 7 8

150 300 5 650 155

100 1

50 2 250 3

300 5 5 6 650 7150 4

155 8

Page 26: SORTING METHODS FOR B.TECH

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.

Page 27: SORTING METHODS FOR B.TECH

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]

Page 28: SORTING METHODS FOR B.TECH

Adjust value for parent ‘4’

Adjust value for parent ‘3’

100 1

50 2 250 3

300 5 5 6 650 7150 4

155 8150

155X = 150

100 1

50 2 250 3

300 5 5 6 650 7155 4

150 8

X = 250

250

650

Page 29: SORTING METHODS FOR B.TECH

Adjust value for parent ‘2’

Adjust value for parent ‘1’

100 1

50 2 650 3

300 5 5 6 250 7155 4

150 8

300

50

100 1

300 2 650 3

50 5 5 6 250 7155 4

150 8

650

250

100

Page 30: SORTING METHODS FOR B.TECH

Final max heap will be …..650 1

300 2 250 3

50 5 5 6 100 7155 4

150 8

650 300 250a

0 1 2 3 4 5 6 7 8

155 50 5 100 150

Page 31: SORTING METHODS FOR B.TECH

Stage II : Exchange a1 with a8. So 8th

element is largest.

Repeat the stage I again for remaining part…

And so on…

150 300 250a

0 1 2 3 4 5 6 7 8

155 50 5 100 650

Largest element

Page 32: SORTING METHODS FOR B.TECH

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 */

Page 33: SORTING METHODS FOR B.TECH

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] );}

Page 34: SORTING METHODS FOR B.TECH

Radix sortProgram :

# 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];

}

Page 35: SORTING METHODS FOR B.TECH

/* 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 */

Page 36: SORTING METHODS FOR B.TECH

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]);}

Page 37: SORTING METHODS FOR B.TECH

Quick SortFunctions :

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--; }

Page 38: SORTING METHODS FOR B.TECH

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;

}

Page 39: SORTING METHODS FOR B.TECH

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);

}

Page 40: SORTING METHODS FOR B.TECH

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 250a

0 1 2 3 4

155 50

Page 41: SORTING METHODS FOR B.TECH

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); }

Page 42: SORTING METHODS FOR B.TECH

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);}

Page 43: SORTING METHODS FOR B.TECH

Binary search

• For binary search array should be sorted

• Binary search is more faster than linear

seach.

5 7 10 15a

0 1 2 3 4 5 6 7 8

28 32 36 38 42

Page 44: SORTING METHODS FOR B.TECH

Examples :

1) Search x=10 in above array

Low High mid

0 8 4

0 3 1

2 3 2 found

Page 45: SORTING METHODS FOR B.TECH

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;

Page 46: SORTING METHODS FOR B.TECH

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);

}

Page 47: SORTING METHODS FOR B.TECH

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);}

Page 48: SORTING METHODS FOR B.TECH

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

Page 49: SORTING METHODS FOR B.TECH

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.

Page 50: SORTING METHODS FOR B.TECH

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.

55 72 88 65a

0 1 2 3 4 5 6 7 8

76 77 98 109 105

72h

0 1 2 3 4 5 6 7 8 9 10 11 12

55 65 76 88 77 98 109 105

65 105 76 77 98 109

Hash function

Key % 10

Page 51: SORTING METHODS FOR B.TECH

• Direct chaining: In this method hash table is a collection of pointers. Each pointer points to a linked list (chain).

Size of hash table is decided from hash function. E.g. If hash function is key % 10 then hash table will have 0 to 9 elements.

55 72 88 65a

0 1 2 3 4 5 6 7 8

76 77 98 109 105Hash function

Key % 10

h

0 1 2 3 4 5 6 7 8 9 72 76 10977

65

105

88

98

55

Page 52: SORTING METHODS FOR B.TECH

• Using Bucket with more slots :In this method hash table is declared as a matrix having rows and columns. Number of columns are decided from hash function. E.g. If hash function is (key % 10) then column no.will be 0 to 9.

55 72 88 65a

0 1 2 3 4 5 6 7 8

76 77 98 109 105Hash function

Key % 10

72 55 76 77 88

65 98

105

0 1 2 3 4 5 6 7 8 9

h

Page 53: SORTING METHODS FOR B.TECH

Hashing Methods :

1) Hash by Division : address = key % m

Address is in the range 0 to m-1.

2) Mid Square hashing :

Address is always middle two digits of the square of the keys .

E.g. If key 55

(55) * (55) = 3 0 2 5

02 will be the address

Page 54: SORTING METHODS FOR B.TECH

3) Folding :

Address will be always sum of folding of a big key.

E.g. Key 123456789 Make the folding with two digits.

1 2

3 4

5 6

7 8

0 9

1 8 9

Don’t consider carry

89 is address