1 / 19

Internal Sorting

Internal Sorting. Each record contains a field called the key . The keys can be places in a linear order. The Sorting Problem

gur
Download Presentation

Internal Sorting

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. Internal Sorting • Each record contains a field called the key. • The keys can be places in a linear order. The Sorting Problem Given a sequence of record R1, R2, …, Rn with key values k1, k2, …, kn, respectively, arrange the records into any order s such that records Rs1, Rs2, …, Rsn have keys obeying the property ks1≤ks2≤ … ≤ksn. Measures of Cost: • Comparisons • Swaps

  2. 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

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

  4. 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)

  5. 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)

  6. Shell Sort • SKIP

  7. 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]); }}

  8. Heap Sort • Build Heap – O(N) • Remove smallest • O(log N) since need to reheapify • Done N times, so O(N log N) • Must use a second array for heap • Can’t sort within the heap array. • Unless we use a max heap and put the value removed (largest) at the end of the array (which is now empty since removed a value).

  9. 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.

  10. 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;}

  11. 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; }

  12. 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)

  13. 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

  14. 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

  15. 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));}

  16. 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

  17. Mergesort Implementation void mergesort (ELEM * array, ELEM * temp, int left, int right){ int mid =(left+right)/2; if (left==right) return; mergesort(array, temp, left, mid); mergesort(array, temp, mid+1, right); for (int I=left; I<=right; I++) temp[I]=array[I]; int i1=left; int i2=mid+1; for (int cur=left; curr<=right; curr++) { if (i1==mid+1) array[curr]=temp[i2++]; else if (i2>right) array[curr]=temp[i1++]; else if (temp[i1]<temp[i2]) array[curr]=temp[i1++]; else array[curr]=temp[i2++]; } }

  18. 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--]; }

  19. Sorting Lower Bounds • Want to prove a lower bound for all possible sorting algorithms. • Sorting is O(n log n). • Sorting I/O takes Ω(n) time. • Will prove sorting bound is Ω(n log n). • Model comparison based sorting with a binary tree. • The tree has n! leaves. • n! ≈sqrt(2πn) (n/e)n • log(n!) ≈ c log(sqrt(n)) + n log (n/e) = Ω(n log n). • The tree is Ω(n log n) levels deep. • log n! = Ω(n log n)

More Related