1 / 16

Today’s Agenda

Today’s Agenda. Sorting Techniques. Selection Sort Time Complexity O(n 2 ) for all best, average and worst case. No. of comparisons are atmost n(n-1)/2 No. of exchanges atmost are n-1 This is suitable where significant amount of data is associated with each element. Bubble Sort

jakeem-reid
Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Sorting Techniques

  2. Selection Sort Time Complexity O(n2) for all best, average and worst case. No. of comparisons are atmost n(n-1)/2 No. of exchanges atmost are n-1 This is suitable where significant amount of data is associated with each element. Bubble Sort Time Complexity Best Case O(n) Ave Case O(n2) Worst Case O(n2) No. of comparisons are order of n2 No. of exchanges are order of n2 This is suitable where small no. of elements are out of order. Compare Selection Sort & Bubble Sort

  3. Selection Sort for(i=0;i<n-1;i++) { min = a[i]; p = i; for(j=i+1;j<n;j++) { if(a[j]<min){ min = a[j]; p = j; } } a[p] = a[i]; a[i] = min; }

  4. Bubble Sort i=1; sorted=0; while(i<n && sorted == 0) { sorted = 1; for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1] = t; sorted = 0; } } i++; }

  5. Insertion Sort • Complexity: • Insertion of a single element: O(n) where n is size of list. • Insertion Sorting: Insert single element into lists from size 1 to size-1: 1 + 2 + … + (size – 1) = O(size2) • It is suitable for small data set and where small no. of elements are out of order in the data set.

  6. int insert(int res[],int x,int size) { int i=0, j, k, flag=0; if(size == 0) { res[0] = x; size = size + 1; } else { while(i< size) { if(res[i] < x || res[i] == x) i++; else { j = i; flag = 1; break; } } if(flag == 1) { for(k=size;k>j;k--) res[k] = res[k-1]; res[k] = x; } else res[i] = x; size = size + 1; } return size; }

  7. #define MAX 20 int main() { int a[10] = {50,16,20,35,18,8,14,41,3,39}; int res[MAX]; int i,size=0; for(i=0;i<10;i++) size = insertInOrder(res,a[i],size); for(i=0;i<size;i++) printf("%d ",res[i]); printf("\n"); }

  8. Programming Exercise • Typedef Struct • { ID idno; NAME name; AdmitYear yr; Group grp; • }Student; • Typedef Student stud[100];

  9. Write a procedure to sort the list of students using insertion sort algorithm. Key for sorting the list is admission year, Group, idno. • Write a compare function to compare two students based on the above key.

  10. comparePolice() ORDER comparePolice (police p1, police p2) { if(rank(p1)<rank(p2)) return LESSER; else if(rank(p1) == rank(p2)) { if(p1.id < p2.id) return LESSER; else if(p1.id == p2.id) return EQUAL; else return GREATER; } else return GREATER; }

  11. Sorting Technique • MergeSort • Top Down Design • Merging • Sorting

  12. Sorting – Top Down Design Sort list: A[0] to A[N-1] M Sort list: A[0] to A[N/2] Sort list: A[N/2+1] to A[N-1] M Sort list: A[0] to A[N/4 ] Sort A[N/4  +1] to A[N/2] M …

  13. MergeSort :Example 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 7 8 = 0 + 8 15 = 0 + 8- 1 = 0 + 2*8- 1 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 11 3 4 7 15 0 8 12 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 14 2 1 4 15 0 8 12 13 5 6 9 10 3 11 7 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 14 15 14 15 2 3 2 3 1 4 11 0 8 12 13 1 4 11 0 5 6 7 8 9 10 12 13 5 6 7 9 10

  14. Design Modules void merge(List ls1, List ls2, List lsNew) uses void mergesort(List unsortedLs)

  15. get next elements e1 and e2; if (e1 <= e2) move e1 to newlist; else move e2 to newlist; move all its elements to newlist; Algorithm Merge while (ls1 and ls2 are not empty) { } if (either List is still not empty) { }

  16. for each j from 0 to N/(2*k) - 1 { start = 2 * k * j; merge(ulist[start to start + k-1], ulist[start +k to start+2k-1], newList) } copy newList back into ulist k = 2 * k; // double the size of lists Algorithm MergeSort : (N = 2P) k=1; while k <= (N/2) { }

More Related