Internal Sorting Each record contains a field called

  • Slides: 19
Download presentation
Internal Sorting • Each record contains a field called the key. • The keys

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 R 1, R 2, …, Rn with key values k 1, k 2, …, kn, respectively, arrange the records into any order s such that records Rs 1, Rs 2, …, Rsn have keys obeying the property ks 1≤ ks 2 ≤ … ≤ ksn. Measures of Cost: • Comparisons • Swaps

Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I<n;

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 42 20 17 13 28 14 23 15 I=1 20 42 17 13 28 14 23 15 I=2 17 20 42 13 28 14 23 15 I=3 13 17 20 42 28 14 23 15 I=4 13 17 20 28 42 14 23 15 I=5 13 14 17 20 28 42 23 15 I=6 13 14 17 20 23 28 42 15 I=7 13 14 15 17 20 23 28 42

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

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

Bubble Sort void bubsort(ELEM * array, int n) { for (int I=0; I<n-1; I++)

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 Best Worst Average Comparisons O(n 2) Swaps 0 O(n 2)

Selection Sort void selsort (ELEM * array, int n) { for (int I=0; I<n-1;

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 Best Worst Average O(n 2) Swaps 0 O(n)

Shell Sort • SKIP

Shell Sort • SKIP

Pointer Swapping • Instead of swapping records, swap pointers in an array. • Start

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

Heap Sort • Build Heap – O(N) • Remove smallest – O(log N) since

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

Quicksort • Divide and Conquer technique • Split the problem into 2 subproblems so

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.

Quicksort Algorithm void qsort (ELEM * array, int I, int j) { int pivotindex

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

Quicksort Partition int partition (ELEM * array, int l, int r, KEY pivot){ do

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

Partition Example initial 72 6 57 88 85 42 83 73 48 60 l

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)

Quicksort Example 72 6 57 88 60 42 83 73 48 85 88 83

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

Cost for Quicksort Best Case : Always partition in half O(n log n) Worst

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(n 2). Average case: n-1 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.

Mergesort Good for both internal and especially external sorting. The function merges 2 sorted

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

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

Mergesort Example 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

Mergesort Implementation void mergesort (ELEM * array, ELEM * temp, int left, int right){

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 i 1=left; int i 2=mid+1; for (int cur=left; curr<=right; curr++) { if (i 1==mid+1) array[curr]=temp[i 2++]; else if (i 2>right) array[curr]=temp[i 1++]; else if (temp[i 1]<temp[i 2]) array[curr]=temp[i 1++]; else array[curr]=temp[i 2++]; } }

Optimized Mergesort void mergesort(ELEM * array, ELEM * temp, int left, int right) {

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

Sorting Lower Bounds • Want to prove a lower bound for all possible sorting

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)