Sorting Techniques Insertion Sort Shell Sort Heap sort

  • Slides: 7
Download presentation
Sorting Techniques – Insertion Sort – Shell Sort – Heap sort

Sorting Techniques – Insertion Sort – Shell Sort – Heap sort

What is sorting? § Sorting means “Putting things in Order” or “arranging” § Arranging

What is sorting? § Sorting means “Putting things in Order” or “arranging” § Arranging can be done in two ways § Ascending Order ( Smallest to Largest ) § Descending Order ( Largest to Smallest ) § In default Sorting Means arranging in ascending order § There are many algorithms for sorting, some simple but perhaps inefficient others complicated but efficient. • We will look at seven algorithms for sorting

Insertion Sorting Working: § This is the way that most people sort playing cards

Insertion Sorting Working: § This is the way that most people sort playing cards § The cards are picked up one by one and put in the proper place. • The Elements are Picked one by one and put into the Proper Place

#define MAX 5 void main() { int A[MAX], i, j, current; cout<<"Enter "<<MAX<<" elements

#define MAX 5 void main() { int A[MAX], i, j, current; cout<<"Enter "<<MAX<<" elements to be sorted: "<<endl; for (i=0; i<MAX; i++) cin>>A[i]; for (i=1; i<MAX; i++) { current= A[i]; //33 j=i; //1 while ((j>0)&&(current<A[j-1])) { // 1>0 && 33 < 67 A[j]= A[j-1]; // 67 moved to next j--; A[j]= current; //33 moved to previous }}cout<<"The elements after Sort"<<endl; for (i=0; i<MAX; i++) cout<<A[i]<<endl; } 67 33 21 84 49 50 75 67 33 67 21 33 49 67 84 21 33 49 50 67 75 84 84 84

Shell Sort designed by Shell, is an improvement on the insertion sort. drawbacks of

Shell Sort designed by Shell, is an improvement on the insertion sort. drawbacks of insertion sort : The exchanging was done with adjacent elements. Improvement If exchanging is done with elements that are further apart then the algorithm would be more efficient. Method: Find an appropriate increment size (e. g. n/3 + 1) • Now do insertion sort on the elements separated by this increment.

67 S 67 h e 33 l l 21 S o r 67 t

67 S 67 h e 33 l l 21 S o r 67 t 33 21 33 84 21 75 50 Insertion Sort 50 67 75 49 Insertion Sort 49 21 84 49 33 84 75 IS IS 50 33 21 49 50 75 67 75 33 49 21 50 49 50 21 49 67 33 50 75 67 75 84 84 Inc=n/3+1 7/3+1=3 3/3+1=2

Shell Sort void Sort. Interval(int start, int increment, int a[]) { int i, j,

Shell Sort void Sort. Interval(int start, int increment, int a[]) { int i, j, current; i=start+increment; // I =3 while (i<MAX) // 3<7 { current=a[i]; //84 j=i; //j=3 while ((j>start) && (current<a[jincrement])) { // 3 >0 && 84 <67 a[j]=a[j-increment]; j=j-increment; a[j]= current; } i=i+increment; // I =6 } } void main() {int A[MAX], i, increment, start; cout<<"Enter "<<MAX<<" elements to be sorted: "<<endl; for (i=0; i<MAX; i++) cin>>A[i]; increment=MAX; do {increment=increment/3+1; for (start=0; start<increment; start++) Sort. Interval(start, increment, A); }while (increment>1); cout<<"The elements after Shell Sort"<<endl; for (i=0; i<MAX; i++) cout<<A[i]<<endl; }