SORTING METHODS FOR B.TECH

Preview:

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

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:

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

Pass I:

Compare : a [0] , a [1]

a [1] , a [2]

a [2] , a [3]

0 2 -3 5a

0 1 2 3

Pass II:

Compare : a [0] , a [1]

a [1] , a [2]

0 -3 2 5a

0 1 2 3

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

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

0 1 2 3 4

6

Pass 0 :

minimum element should be replaced with 0th element.

-3 0 8 5a

0 1 2 3 4

6

replaced

Pass I :

minimum element should be replaced with 1st element.

-3 0 8 5a

0 1 2 3 4

6

No replacement

Pass II :

minimum element should be replaced

with 2nd element.

-3 0 5 8a

0 1 2 3 4

6

replaced

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

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

0 1 2 3 4

3

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

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

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

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

Array after pass IV

This is final sorted array…..

-1 0 3 5a

0 1 2 3 4

8

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

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]

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

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

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

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

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

}

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

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

0 1 2 3 4

155 50

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

0 1 2 3 4 5 6 7 8

28 32 36 38 42

Examples :

1) Search x=10 in above array

Low High mid

0 8 4

0 3 1

2 3 2 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.

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

• 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

• 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

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

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

Recommended