1 / 16

Insertion Sort

Insertion Sort. void inssort (ELEM * array, int n) { for (int I=1; I<n; I++) for (int j=I; (j>0) && (key(array[j])<key(array[j-1])); j--) swap (array[j], array[j-1]); } init I=1 I=2 I=3 I=4 I=5 I=6 I=7

devin-orr
Download Presentation

Insertion Sort

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. Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I<n; I++) for (int j=I; (j>0) && (key(array[j])<key(array[j-1])); j--) swap (array[j], array[j-1]); } init I=1 I=2 I=3 I=4 I=5 I=6 I=7 42 20 17 13 13 13 13 13 20 42 20 17 17 14 14 14 17 17 42 20 20 17 17 15 13 13 13 42 28 20 20 17 28 28 28 28 42 28 23 20 14 14 14 14 14 42 28 23 23 23 23 23 23 23 42 28 15 15 15 15 15 15 15 42

  2. Speed CASE Comparisons Swapping BEST n-1 0 WORST O(n2) O(n2) AVERAGE O(n2) O(n2)

  3. Bubble Sort void bubsort(ELEM * array, int n) { for (int I=0; I<n-1; I++) for (int j=n-1; j>I; j--) if (key(array[j]) < key(array[j-1])) swap (array[j], array[j-1]); } Example: smallest goes to top with each pass. Speed Analysis: CASE Comparisons Swaps Best O(n2) 0 Worst O(n2) O(n2) Average O(n2) O(n2)

  4. Selection Sort void selsort (ELEM * array, int n) { for (int I=0; I<n-1; I++) { int lowindex=I; for (int j=n-1; j>I; j--) if (key(array[j])<key(array[lowindex])) lowindex=j; swap(array[I], array[lowindex]); } } EXAMPLE: find smallest and put at top of rest of list. Speed Analysis: CASE Comparisons Swaps Best O(n2) 0 Worst O(n2) O(n) Average O(n2) O(n)

  5. Pointer Swapping • Instead of swapping records, swap pointers in an array. • Start array with 0, 1, 2, …., n-1. • Just needs extra level of subscripting in algorithms. void selsort (ELEM * array, int n) { for (int I=0; I<n-1; I++) { int lowindex= I; for (int j=n-1; j>I; j--) if (key(array[point[j]]) <key(array[point[lowindex]])) lowindex=j; swap(point[I], point[lowindex]); }}

  6. Quicksort • Divide and Conquer technique • Split the problem into 2 subproblems so that when each is solved independently, their solutions will be the solution to the whole problem. • Need to divide the problem such that all of one subproblem has all values less that all the values of the other subproblem. This way when each subproblem is sorted, the entire array will be sorted. • Now to sort each subset, we divide and conquer again. • Repeat until we have subproblems of size 0 or 1.

  7. Quicksort Algorithm void qsort (ELEM * array, int I, int j) { int pivotindex = findpivot(array,I,j); swap (array[pivotindex], array[j]); int k=partition(array, I-1,j, key(array[j])); swap (array[k], array[j]); if ((k-I)>1) qsort(array,I,k-1); if ((j-k)>1) qsort(array,k+1,j); } int findpivot (ELEM * array, int I, int j) {return (I+j)/2;}

  8. Quicksort Partition int partition (ELEM * array, int l, int r, KEY pivot){ do { while (key(array[++l]) < pivot); while (r && (key(array[--r]) >pivot)); swap (array[l], array[r]); } while (l<r); swap (array[l], array[r]); return l; }

  9. Partition Example initial 72 6 57 88 85 42 83 73 48 60 l r Pass 1 72 6 57 88 85 42 83 73 48 60 l r Swap 1 48 6 57 88 85 42 83 73 72 60 l r Pass 2 48 6 57 88 85 42 83 73 72 60 l r Swap 2 48 6 57 42 85 88 83 73 72 60 l r Pass 3 48 6 57 42 85 88 83 73 72 60 r l Swap 3 48 6 57 85 42 88 83 73 72 60 r l Rev. 48 6 57 42 85 88 83 73 72 60 Swap Cost: O(n)

  10. Quicksort Example 72 6 57 88 60 42 83 73 48 85 Pivot = 60 48 6 57 42 60 88 83 73 72 85 Pivot = 6 Pivot = 73 6 42 57 48 72 73 83 88 85 Pivot = 88 Pivot = 57 83 85 88 42 48 57 Pivot = 42 Pivot = 83 42 48 83 85

  11. Cost for Quicksort Best Case : Always partition in half O(n log n) Worst case : Bad partition - have a subproblem of size 1 each time. O(n2). Average case: T(n)= n+1+ 1/(n-1) k=1 (T(k)+T(n-k)) = O(n log n) Optimizations for Quicksort • Better pivot • Use better algorithm for small sublists • Eliminate recursion. n-1

  12. Heapsort • Use a min heap to get items in ascending order. • Create the heap – O(n). • Remove an item – O(log n). • Remove n items – O(n log n).

  13. Mergesort Good for both internal and especially external sorting. The function merge merges 2 sorted lists into one sorted list. More easily done when sorted. List mergesort(list inlist){ if (length(inlist)== 1) return inlist; list l1=half of items from inlist; list l2=other half of items from inlist; return merge(mergesort(l1), mergesort(l2));}

  14. Mergesort Example 36 20 17 13 28 14 23 15 36 20 17 13 28 14 23 15 36 20 17 13 28 14 23 15 20 36 13 17 14 28 15 23 13 17 20 36 14 15 23 28 13 14 15 17 20 23 28 36

  15. Mergesort Analysis • T(1)=1 • T(n)=2T(n/2)+n • Solve • T(n)/n=T(n/2)/(n/2)+1 • T(n/2)/(n/2)=T(n/4)/(n/4)+1 • T(n/4)/(n/4)=T(n/8)/(n/8)+1 … • T(2)/2=T(1)/1+1 add all equations • T(n)/n+T(n/2)/(n/2)…+T(2)/2=T(n/2)/(n/2)+…+T(1)/1 + log n • T(n)/n=T(1)+log n • T(n)=1+n log n=O(n log n)

  16. Optimized Mergesort void mergesort(ELEM * array, ELEM * temp, int left, int right) { int I, j, k, mid=(left+right)/2; if (left == right) return; mergesort(array, temp, left, mid); mergesort(array, temp, mid+1, right); for (I=left; I<=mid; I++) temp[I] = array[I]; for (j=1; j<=right-mid; j++) temp[right-j+1] = array[j+mid]; I=left; j=right; for (k=left; k<=right; k++) if (temp[I]<temp[j]) array[k]=temp[I++]; else array[k]=temp[j--]; }

More Related