class 12 ISC Sorting Method and Algorithm in blue j
-
Upload
sampuran-de -
Category
Documents
-
view
95 -
download
0
description
Transcript of class 12 ISC Sorting Method and Algorithm in blue j
Heap Sorting method and algorithmIn 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: 80
Step2: 80 / \ 32 31
Step3: 80 80 110 / \ / \ / \ 32 31 ==> 110 31 ==> 80 31 / / / 110 32 32
Step4: 110 / \ 80 31 / \ 32 5
Step5: 110 110 / \ / \ 80 31 ==> 80 40 / \ / / \ / 32 50 40 32 50 31
Step6: 110 110 120 / \ / \ / \ 80 40 ==> 80 120 ==> 80 110 / \ / \ / \ / \ / \ / \ 32 50 31 120 32 50 31 40 32 50 31 40
-------------------------------------------------------------------------------------
(1) 120 [1]
/ \
80 [2] 110 [3]
/ \ / \
32 [4] 50 [5] 31 [6] 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) 40 110 / \ Heaping / \ 80 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 80 80 / \ Heaping / \ Heaping / \ 80 40 ======> 31 40 ======> 50 40 / \ / \ / \ / \ / \ / \ 32 50 110 120 32 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 50 / \ Heaping / \ Heaping / \ 50 40 ======> 31 40 ======> 32 40 / \ / \ / \ / \ / \ / \ 32 80 110 120 32 80 110 120 31 80 110 120
(5)//now replace 50 with rightmost element i.e. 31 because 80, 110, 120 had been sorted.
31 40 / \ Heaping / \ 32 40 ======> 32 31 / \ / \ / \ / \ 50 80 110 120 50 80 110 120
(6)//now replace 40 with rightmost element i.e. 31 because 50, 80, 110, 120 had been sorted.
31 32 / \ Heaping / \ 32 40 ======> 31 40 / \ / \ / \ / \ 50 80 110 120 50 80 110 120
(7)//now replace 32 with rightmost element i.e. 31 because 40, 50, 80, 110, 120 had been sorted.
31 / \ 32 40 / \ / \ 50 80 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]))
Shell sortingQ. 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 12 14 9 15 45 32 95 40 5//step-3 i.e. sorting of marked elements
5 12 14 9 15 45 32 95 40 35//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 12 14 9 15 45 32 95 40 35//now sorting of marked elements
5 12 14 9 15 45 32 95 40 35//Now inc=2-1=1//Now every elements all marked because inc=1
5 12 14 9 15 45 32 95 40 35//sorting of marked elements
5 9 12 14 15 32 35 40 45 95
/*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 : 7Enter 1 number : 8Enter 2 number : 3Enter 3 number : 7Enter 4 number : 9
Enter 5 number : 1Enter 6 number : 24Enter 7 number : 2 **** Shell Sorting ****1 2 3 7 8 9 24 *****************************************/
Bubble sortingQ. 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 side2. Compare first two numbers3. 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 iteration6 3 7 1 4 5 2
3 6 7 1 4 5 2
3 6 7 1 4 5 2
3 6 1 7 4 5 2
3 6 1 4 7 5 2
3 6 1 4 5 7 2
3 6 1 4 5 2 7
Second iteration3 6 1 4 5 2 7
3 6 1 4 5 2 7
3 1 6 4 5 2 73 1 4 6 5 2 7
3 1 4 5 6 2 7
3 1 4 5 2 6 7
Third iteration3 1 4 5 2 6 7
1 3 4 5 2 6 7
1 3 4 5 2 6 7
1 3 4 5 2 6 7
1 3 4 2 5 6 7
Four iteration1 3 4 2 5 6 7
1 3 4 2 5 6 7
1 3 4 2 5 6 7
1 3 2 4 5 6 7
Five iteration1 3 2 4 5 6 7
1 3 2 4 5 6 7
1 2 3 4 5 6 7
Six iteration1 2 3 4 5 6 7
1 2 3 4 5 6 7
Seven iteration1 2 3 4 5 6 7
/*program to example of bubble sorting*/#include<stdio.h>#include<conio.h>#define SIZE 7int 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 : 6Enter number : 3Enter number : 7Enter number : 1Enter number : 4Enter number : 5Enter number : 21 2 3 4 5 6 7
Selection sortingQ. 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 10int 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 element : 21Enter element : 3Enter element : 45Enter element : 87Enter element : 72Enter element : 14Enter element : 54Enter element : 75Enter element : 44Enter element : 5
After selection sort the elements :
3 5 14 21 44 45 54 72 75 87
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 sortingQ. 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.
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 5 7 9 2 6 1
2 3 4 5 7 9 6 1
2 3 4 5 6 7 9 1
1 2 3 4 5 6 7 9
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. Let’s 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 7Here, 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, how’s arrange the less than and greater than elements? Be patient, you get answer soon.Now let’s 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 sortedExample 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 last);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 last){ 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];
Merge sortingQ. 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 Sublist0 340 7101 2 812 5823 4934 5 715 195 3856 7 4378 9
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 Sublist0 1 710 812 7152 3 4374 3405 6 7 8 582 3859 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 Sublist0 1 1952 3 340 3854 437 4935 582
6 7 710 7158 8129
At last, the list is gathered up again:
195 340 385 437 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 SHOWPASSvoid 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;}