You are on page 1of 38

Heap Sorting method and algorithm

In heap sorting process: First of all organizing the whole data to be sorted as a binary tree i.e. heap. And after that impletment heap sorting. Step 1: (How to data organizing as binary tree?) Remember only 2 rule: Rule:1. The parent node must be greater then child node. If parent node is not greater then to child node not replace it with parent node. And if binary tree is large then, if relaceing child node(now its parent node) is greater then to great-parent node then its also is replace the great-parent node. Rule:2. New element always insert in left side and after right side of parent node. If left side of parent element has already element then new element would be insert in right side. And if right side of parent node has already element then new element will be insert in left side. Step 2: (How to perform heap sorting:) Remove the topmost element(largest element) and replace it with the rightmost element. Step 3: Repeat steps 1 and 2 untill all elements is not sorted.

Heap Sorting method/process by example: max heap : 80 , 32 , 31 , 110 , 50 , 40 , 120 Step1: Step2: / 32 Step3: / 32 80 \ 31 80 \ 31 ==> 80 / \ 110 31 110 / \ ==> 80 80

31

/ 110 Step4: / 80 / \

/ 32

/ 32

110 \ 5

31

32 Step5:

110 / \ 80 31 / \ / 32 50 40

==>

110 / \ 80 40 / \ / 32 50 31

Step6: 80 32 / /

110 \ 50

110 120 \ / \ / \ 40 ==> 80 120 ==> 80 110 / \ / \ / \ / \ / \ 31 120 32 50 31 40 32 50 31 40

------------------------------------------------------------------------------------(1) / 80 / 32
[4] [2]

120

[1]

\ 110 /
[5] [3]

\ 50

\
[6]

31

40

[7]

// [1],[2],[3],[4],[5],[6],[7]: Index number // Now applying step 2(perform heap sorting) // Replace the topmost element with the rightmost element. (2) / 80 / \

40 \ 110 / \ Heaping =====> /

110 / \ 80 40 \ / \

32

50

31

120

32

50

31

120

(3) //now replace 110 with rightmost element i.e. 31 because 120 had been sorted. 31 / \ Heaping 80 40 ======> / \ / \ / 32 50 110 120 32 80 80 / \ Heaping / \ 31 40 ======> 50 40 \ / \ / \ / \ 50 110 120 32 31 110 120

(4) //now replace 80 with rightmost element i.e. 31 because 110, 120 had been sorted. 31 50 / \ Heaping / 50 40 ======> 31 / \ / \ / \ 32 80 110 120 32 80 \ / 40 \ 110 50 Heaping / \ ======> 32 40 / \ / \ 120 31 80 110 120

(5) //now replace 50 with rightmost element i.e. 31 because 80, 110, 120 had been sorted. / / 50 32 31 40 Heaping / \ 40 ======> 32 31 / \ / \ / \ 110 120 50 80 110 120 \

\ 80

(6) //now replace 40 with rightmost element i.e. 31 because 50, 80, 110, 120 had been sorted. 31 / \ Heaping 32 40 ======> / \ / \ / 50 80 110 120 50 32 / \ 31 40 \ / \ 80 110 120

(7) //now replace 32 with rightmost element i.e. 31 because 40, 50, 80, 110, 120 had been sorted. 31 32 / \ 50 80 / \ 40 / \ 110 120

Now all elements are sorted: 31 , 32 , 40 , 50 , 80 , 110 , 120

Q. Write a C program to sort 5 numbers using heap sorting method. Ans. /* c program for heap sorting method*/ #include<stdio.h> #include<conio.h> void manage(int *, int); void heapsort(int *, int, int); int main() { int arr[20]; int i,j,size,tmp,k; printf("\n\t------- Heap sorting method -------\n\n"); printf("Enter the number of elements to sort : "); scanf("%d",&size); for(i=1; i<=size; i++) { printf("Enter %d element : ",i); scanf("%d",&arr[i]); manage(arr,i); } j=size; for(i=1; i<=j; i++) { tmp=arr[1];

arr[1]=arr[size]; arr[size]=tmp; size--; heapsort(arr,1,size); } printf("\n\t------- Heap sorted elements -------\n\n"); size=j; for(i=1; i<=size; i++) printf("%d ",arr[i]); getch(); return 0; } void manage(int *arr, int i) { int tmp; tmp=arr[i]; while((i>1)&&(arr[i/2]<tmp)) { arr[i]=arr[i/2]; i=i/2; } arr[i]=tmp; } void heapsort(int *arr, int i, int size) { int tmp,j; tmp=arr[i];

j=i*2; while(j<=size) { if((j<size)&&(arr[j]<arr[j+1])) j++; if(arr[j]<arr[j/2]) break; arr[j/2]=arr[j]; j=j*2; } arr[j/2]=tmp; }

Shell sorting

Q. Write a c program that sort 10 numbers using shell sorting method. Ans. Shell sorting was first introduced by Donald Shell. It generalized as exchanging sort, such as bubble or insertion sorting, by allowing the comparison and exchange of elements that lie far apart. The running time of Shell sort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem. Steps for solution of shell sorting:
step.1:

Set up a inc number. inc number is set according to given elements in the list.
step.2:

mark each elements which is comes in inc element. For example, if list contains 10 elements then and we assume inc is 3 then, marking of elements such as next

marking element, add last element +3 to get next element and so on. Then marking element would be 1st element, 4th element(add 3), 7th element(add 3), 10th element.
89 46 99 12 33 14 69 41 33 28 1 2 3 4 5 6 7 8 9 10 [index number]

step.3:

sort marking elements such as smallest to greater is set as left to right and not change remain element. For example, we apply this step in above example:
12 46 99 28 33 14 69 41 33 89 step.4:

reduce inc number to one i.e. if inc number is earlier is 3 then now it would be 3-1 = 2.
step.5:

Repeat step 2,3 and 4 till all the elements not sorted. Let's understand shell sorting using example: 35 12 14 9 15 45 32 95 40 5
//assume inc=3 //Now marking 1st element, 1+3=4th element, //4+3=7th element, 7+3=10th element

35 5

12 12

14 14

9 9

15 15

45 45

32 95 32 95

40 40

5 35

//step-3 i.e. sorting of marked elements

//now inc=3-1=2 //new marking is 1st element, 1+2=3th element, //3+2=5 element, 5+2=7th element, 7+2=9th //element

5 5 5 5

12 12

14 14

9 9

15 15

45 45

32 32

95 95 95 40

40 40

35 35

//now sorting of marked elements

//Now inc=2-1=1 //Now every elements all marked because inc=1

//sorting of marked elements

12

14

15

45 32

32 35

40 35 45 95

12

14

15

/*c program for sorting array using shell sorting method*/ #include<stdio.h> #include<conio.h> int main() { int arr[30]; int i,j,k,tmp,num; printf("Enter total no. of elements : "); scanf("%d", &num); for(k=0; k<num; k++) { printf("\nEnter %d number : ",k+1);

scanf("%d",&arr[k]); } for(i=num/2; i>0; i=i/2) { for(j=i; j<num; j++) { for(k=j-i; k>=0; k=k-i) { if(arr[k+i]>=arr[k]) break; else { tmp=arr[k]; arr[k]=arr[k+i]; arr[k+i]=tmp; } } } } printf("\t**** Shell Sorting ****\n"); for(k=0; k<num; k++) printf("%d\t",arr[k]); getch(); return 0; } /*****************OUTPUT*************** ** Enter total no. of elements : 7 Enter 1 number : 8 Enter 2 number : 3

Enter Enter Enter Enter Enter

3 number : 7 4 number : 9 5 number : 1 6 number : 24 7 number : 2 **** Shell Sorting **** 1 2 3 7 8 9 24 *************************************** **/

Bubble sorting

Q. Write a program to accept 10 numbers from user and sort list using Bubble sorting method. Ans.
Logic of bubble sorting as follows:

In bubble sorting steps: 1. Start from left hand side 2. Compare first two numbers 3. If first_number > second_number than swap both number position. And if first_number < second_number than these compare next two numbers i.e. second_number and third_number. 4. Step-3 process repeat until there are no more numbers left to compared. 5. Bubble sorting completed. An example on bubble sort. To understand logic of Bubble Sorting, lets take random numbers: 6 3 7 1 4 5 2

First iteration
6 3 3 3 3 3 3 3 7 7 7 1 1 1 1 1 4 4 4 4 7 5 5 5 5 5 5 5 7 2 2 2 2 2 2 2 7 6 6 6 6 6 6 1 1 7 4 4 4

Second iteration
3 3 3 3 3 3 6 6 1 1 1 1 1 1 6 4 4 4 4 4 4 6 5 5 5 5 5 5 6 2 2 2 2 2 2 6 7 7 7 7 7 7

Third iteration
3 1 1 1 1 1 4 4 4 4 4 5 5 5 5 2 2 2 2 2 5 6 6 6 6 6 7 7 7 7 7 3 3 3 3

Four iteration
1 1 1 1 3 3 3 3 4 4 4 2 2 2 2 4 5 5 5 5 6 6 6 6 7 7 7 7

Five iteration
1 1 3 3 2 2 4 4 5 5 6 6 7 7

Six iteration
1 1 2 3 3 4 4 5 5 6 6 2

7 7

Seven iteration
1 2 3 4 5 6 7

/*program to example of bubble sorting*/ #include<stdio.h> #include<conio.h> #define SIZE 7 int main() { int i,j,temp; int arr[ SIZE ]; for(i=0; i<SIZE; i++) { printf("Enter Number : "); scanf("%d",&arr[i]); } for(i=0; i<SIZE ; i++) { for(j=0; j<(SIZE-1)-i; j++) { if( arr[j] < arr[j+1] ) { temp=arr[j];

arr[j]=arr[j+1]; arr[j+1]=temp; } } printf("%d\t",arr[j]); } getch(); return 0; } Output:Enter number : Enter number : Enter number : Enter number : Enter number : Enter number : Enter number : 1 2 3 4 5 6 3 7 1 4 5 2 6 7

Selection sorting

Q. Write a C program to to sort a list of elements using selection sort method. Ans. /*program to demonstration of selection method*/ #include<stdio.h> #include<conio.h> #define SIZE 10 int main() { int i,j,min,temp; int arr[SIZE]; for(i=0; i<SIZE; i++) { printf("Enter element : "); scanf("%d",&arr[i]); } for(i=0; i<SIZE; i++) { min=i; for(j=i+1; j<SIZE; j++) if(arr[j]<arr[min]) min=j; temp=arr[i]; arr[i]=arr[min]; arr[min]=temp; }

printf("After selection sort the elements:\n"); for(i=0; i<SIZE; i++) printf("%d\t",arr[i]); getch(); return 0; } Output:Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter 3 5 element element element element element element element element element element 14 21 : : : : : : : : : : 44 21 3 45 87 72 14 54 75 44 5 45 54 72 75 87

After selection sort the elements :

Here is an example of this sort algorithm sorting five elements:


64 25 12 22 11 11 25 12 22 64 11 12 25 22 64 11 12 22 25 64 11 12 22 25 64

Insertion sorting

Q. write a c program to implies the insertion sorting method. Ans. /* c program for insertion sorting method */ #include<stdio.h> #include<conio.h> int main() { int arr[30]; int i,j,size,tmp; printf("\n\t------ Insertion sorting method ---------\n\n"); printf("Enter total no. of elements : "); scanf("%d", &size); for(i=0; i<size; i++) { printf("Enter %d element : ",i+1); scanf("%d", &arr[i]); } for(i=0; i<size; i++) { for(j=i-1; j>=0; j--) { if(arr[j]>arr[j+1])

{ tmp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tmp; } else break; } } printf("\n\t------- Insertion sorted elements -------\n\n"); for(i=0; i<size; i++) printf(" %d",arr[i]); getch(); return 0; }
Example: The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the item under consideration is underlined. The item that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold. 37495261 37495261 37495261 34795261 34795261 34579261 23457961 23456791 12345679

Quick sorting

Quick sort is a divide and conquer algorithm. Its divided large list in mainly three parts: 1. Elements less than pivot element. 2. Pivot element. 3. Elements greater than pivot element. Where pivot as middle element of large list. Lets understand through example: List : 3 7 8 5 2 1 9 5 4 In above list assume 4 is pivot element so rewrite list as: 3 1 2 4 5 8 9 5 7 Here, I want to say that we set the pivot element(4) which has in left side elements are less than and right hand side elements are greater than. Now you think, hows arrange the less than and greater than elements? Be patient, you get answer soon. Now lets start understand the concept of quick sort. The steps are: 1. Pick a pivot element. 2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the

pivot is in its final position. This is called the partition operation. 3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. The base case of the recursion are lists of size zero or one, which never need to be sorted Example of quick sort process:

Steps of quick sorting

/*c program for quick sorting*/ #include<stdio.h> #include<conio.h> void qsort(int arr[20], int fst, int la st); int main() { int arr[30]; int i,size; printf("Enter total no. of the elements : "); scanf("%d",&size); printf("Enter total %d elements : \n",size); for(i=0; i<size; i++) scanf("%d",&arr[i]); qsort(arr,0,size-1);

printf("Quick sorted elements are as : \n"); for(i=0; i<size; i++) printf("%d\t",arr[i]); getch(); return 0; } void qsort(int arr[20], int fst, int la st) { int i,j,pivot,tmp; if(fst<last) { pivot=fst; i=fst; j=last; while(i<j) { while(arr[i]<=arr[pivot] && i<last) i++; while(arr[j]>arr[pivot]) j--; if(i<j) { tmp=arr[i]; arr[i]=arr[j]; arr[j]=tmp; } } tmp=arr[pivot]; arr[pivot]=arr[j];

arr[j]=tmp; qsort(arr,fst,j-1); qsort(arr,j+1,last); } }

Merge sorting

Q. Write a C program to that sort 5 numbers using merge sorting. Ans. /* c program for merge sorting */ #include<stdio.h> #include<conio.h> void merge(int [],int ,int ,int ); void part(int [],int ,int ); int main() { int arr[30]; int i,size; printf("\n\t------- Merge sorting method -------\n\n"); printf("Enter total no. of elements : "); scanf("%d",&size); for(i=0; i<size; i++) { printf("Enter %d element : ",i+1); scanf("%d",&arr[i]); } part(arr,0,size-1); printf("\n\t------- Merge sorted elements -------\n\n"); for(i=0; i<size; i++) printf("%d ",arr[i]); getch();

return 0; } void part(int arr[],int min,int max) { int mid; if(min<max) { mid=(min+max)/2; part(arr,min,mid); part(arr,mid+1,max); merge(arr,min,mid,max); } } void merge(int arr[],int min,int mid,int max) { int tmp[30]; int i,j,k,m; j=min; m=mid+1; for(i=min; j<=mid && m<=max ; i++) { if(arr[j]<=arr[m]) { tmp[i]=arr[j]; j++; } else

{ tmp[i]=arr[m]; m++; } } if(j>mid) { for(k=m; k<=max; k++) { tmp[i]=arr[k]; i++; } } else { for(k=j; k<=mid; k++) { tmp[i]=arr[k]; i++; } } for(k=min; k<=max; k++) arr[k]=tmp[k]; }

Consider the following 9 numbers: 493 812 715 710 195 437 582 340 385

We should start sorting by comparing and ordering the one's digits:

Digit
0 1 2 3 4 5 6 7 8 9

340 710 812 582 493

Sublist

715 195 385 437

Notice that the numbers were added onto the list in the order that they were found, which is why the numbers appear to be unsorted in each of the sublists above. Now, we gather the sublists (in order from the 0 sublist to the 9 sublist) into the main list again: 340 710 812 582 493 715 195 385 437

Note: The order in which we divide and reassemble the list is extremely important, as this is one of the foundations of this algorithm. Now, the sublists are created again, this time based on the ten's digit:

Digit
0 1 2 3 4 5 6 7 8 9

Sublist
710 812 715 437 340

582 385 493 195

Now the sublists are gathered in order from 0 to 9: 710 812 715 437 340 582 385 493 195

Finally, the sublists are created according to the hundred's digit:

Digit
0 1 2 3 4 195

Sublist

340 385 437 493

5 6 7 8 9 At last, the list is gathered up again: 195 340 385 437

582 710 715 812

493

582

710

715

812

And now we have a fully sorted array! Radix Sort is very simple, and a computer can do it fast. When it is programmed properly, Radix Sort is in fact one of the fastest sorting algorithms for numbers or strings of letters.

#include <stdio.h> #define MAX 5 #define SHOWPASS void print(int *a, int n) { int i; for (i = 0; i < n; i++) printf("%d\t", a[i]); } void radixsort(int *a, int n) { int i, b[MAX], m = a[0], exp = 1; for (i = 0; i < n; i++) { if (a[i] > m) m = a[i]; } while (m / exp > 0) { int bucket[10] = { 0 }; for (i = 0; i < n; i++) bucket[a[i] / exp % 10]++; for (i = 1; i < 10; i++) bucket[i] += bucket[i - 1]; for (i = n - 1; i >= 0; i--) b[--bucket[a[i] / exp % 10]] = a[i]; for (i = 0; i < n; i++) a[i] = b[i]; exp *= 10;

#ifdef SHOWPASS printf("\nPASS print(a, n); #endif } } int main() { int arr[MAX]; int i, n;

: ");

printf("Enter total elements (n < %d) : ", MAX); scanf("%d", &n); printf("Enter %d Elements : ", n); for (i = 0; i < n; i++) scanf("%d", &arr[i]); printf("\nARRAY : "); print(&arr[0], n); radixsort(&arr[0], n); printf("\nSORTED : "); print(&arr[0], n); printf("\n"); return 0; }

Straight Insertion - Figure 15.2

Bubble Sorting - Figure 15.3

Quick Sorting - Figure 15.4

Straight Selection Sorting - Figure 15.5

Building a heap - Figure 15.7

Heap Sorting - Figure 15.8

Two-way merge sorting - Figure 15.10

Bucket Sorting - Figure 15.12

Radix Sorting - Figure 15.13

You might also like