Chapter 7 Internal Sorting. Sorting Each record contains a field called the key. –Linear order: comparison. Measures of cost: –Comparisons –Swaps.

Slides:



Advertisements
Similar presentations
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Advertisements

Lower bound for sorting, radix sort COMP171 Fall 2005.
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
Using Divide and Conquer for Sorting
CSE 373: Data Structures and Algorithms
Internal Sorting A brief review and some new ideas CS 400/600 – Data Structures.
Sorting Algorithms and Average Case Time Complexity
Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Internal.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Quicksort.
Sorting Chapter 6. 2 Algorithms to know Insertion Sort Insertion Sort –O(n 2 ) but very efficient on partially sorted lists. Quicksort Quicksort –O(n.
Divide and Conquer Sorting
Analysis of Algorithms CS 477/677
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
CSE 373 Data Structures Lecture 19
CSE 373 Data Structures Lecture 15
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
Sorting Algorithms 2. Quicksort General Quicksort Algorithm: Select an element from the array to be the pivot Select an element from the array to be the.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Analysis of Algorithms CS 477/677
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Comparison of Optimization Algorithms By Jonathan Lutu.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Sorting. Sorting Terminology Sort Key –each element to be sorted must be associated with a sort key which can be compared with other keys e.g. for any.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
O(n) Algorithms. Binsort/Bucketsort: Algorithm /* Put a range of values in bins and then sort the contents of each bin and then output the values of each.
Sorting 1. Insertion Sort
Course notes CS2606: Data Structures and Object-Oriented Development Chapter 7: Sorting Department of Computer Science Virginia Tech Spring 2008 (The following.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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.
Chapter 5 Sorting There are several easy algorithms to sort in O(N 2 ), such as insertion sort. There is an algorithm, Shellsort, that is very simple to.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Description Given a linear collection of items x1, x2, x3,….,xn
Sorting We have actually seen already two efficient ways to sort:
Advanced Sorting Methods: Shellsort
CSC215 Lecture Algorithms.
Heapsort.
Mergesort.
COMP 352 Data Structures and Algorithms
CSE 326: Data Structures Sorting
CSE 373: Data Structures and Algorithms
Lower bound for sorting, radix sort
CSE 373 Data Structures and Algorithms
CSCI 333 Data Structures Chapter 6 14 and 16 October 2002.
These notes were prepared by the text’s author Clifford A. Shaffer
CSE 332: Sorting II Spring 2016.
Sorting We have actually seen already two efficient ways to sort:
Advanced Sorting Methods: Shellsort
Presentation transcript:

Chapter 7 Internal Sorting

Sorting Each record contains a field called the key. –Linear order: comparison. Measures of cost: –Comparisons –Swaps

Insertion Sort (1)

Insertion Sort (2) template template void inssort(Elem A[], int n) { for (int i=1; i<n; i++) for (int i=1; i<n; i++) for (int j=i; (j>0) && for (int j=i; (j>0) && (Comp::lt(A[j], A[j-1])); j--) (Comp::lt(A[j], A[j-1])); j--) swap(A, j, j-1); swap(A, j, j-1);} Best Case: Worst Case: Average Case:

Bubble Sort (1)

Bubble Sort (2) template template void bubsort(Elem A[], int n) { for (int i=0; i<n-1; i++) for (int i=0; i<n-1; i++) for (int j=n-1; j>i; j--) for (int j=n-1; j>i; j--) if (Comp::lt(A[j], A[j-1])) if (Comp::lt(A[j], A[j-1])) swap(A, j, j-1); swap(A, j, j-1);} Best Case: Worst Case: Average Case:

Selection Sort (1)

Selection Sort (2) template template void selsort(Elem A[], int n) { for (int i=0; i<n-1; i++) { for (int i=0; i<n-1; i++) { int lowindex = i; // Remember its index int lowindex = i; // Remember its index for (int j=n-1; j>i; j--) // Find least for (int j=n-1; j>i; j--) // Find least if (Comp::lt(A[j], A[lowindex])) if (Comp::lt(A[j], A[lowindex])) lowindex = j; // Put it in place lowindex = j; // Put it in place swap(A, i, lowindex); swap(A, i, lowindex); }} Best Case: Worst Case: Average Case:

Pointer Swapping

Summary InsertionBubbleSelection Comparisons: Best Case (n)(n)(n)(n) (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) Average Case (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) Worst Case (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) Swaps Best Case 00 (n)(n)(n)(n) Average Case (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) (n)(n)(n)(n) Worst Case (n2)(n2)(n2)(n2) (n2)(n2)(n2)(n2) (n)(n)(n)(n)

Exchange Sorting All of the sorts so far rely on exchanges of adjacent records. What is the average number of exchanges required? –There are n! permutations –Consider permuation X and its reverse, X’ –Together, every pair requires n(n-1)/2 exchanges.

Shellsort

Shellsort // Modified version of Insertion Sort template template void inssort2(Elem A[], int n, int incr) { for (int i=incr; i<n; i+=incr) for (int i=incr; i<n; i+=incr) for (int j=i; for (int j=i; (j>=incr) && (j>=incr) && (Comp::lt(A[j], A[j-incr])); j-=incr) (Comp::lt(A[j], A[j-incr])); j-=incr) swap(A, j, j-incr); swap(A, j, j-incr);} template template void shellsort(Elem A[], int n) { // Shellsort for (int i=n/2; i>2; i/=2) // For each incr for (int i=n/2; i>2; i/=2) // For each incr for (int j=0; j<i; j++) // Sort sublists for (int j=0; j<i; j++) // Sort sublists inssort2 (&A[j], n-j, i); inssort2 (&A[j], n-j, i); inssort2 (A, n, 1); inssort2 (A, n, 1);}

Quicksort template template void qsort(Elem A[], int i, int j) { if (j <= i) return; // List too small if (j <= i) return; // List too small int pivotindex = findpivot(A, i, j); int pivotindex = findpivot(A, i, j); swap(A, pivotindex, j); // Put pivot at end swap(A, pivotindex, j); // Put pivot at end // k will be first position on right side // k will be first position on right side int k = int k = partition (A, i-1, j, A[j]); partition (A, i-1, j, A[j]); swap(A, k, j); // Put pivot in place swap(A, k, j); // Put pivot in place qsort (A, i, k-1); qsort (A, i, k-1); qsort (A, k+1, j); qsort (A, k+1, j);} template template int findpivot(Elem A[], int i, int j) { return (i+j)/2; } { return (i+j)/2; }

Quicksort Partition template template int partition(Elem A[], int l, int r, Elem& pivot) { Elem& pivot) { do { // Move the bounds in until they meet do { // Move the bounds in until they meet while (Comp::lt(A[++l], pivot)); while (Comp::lt(A[++l], pivot)); while ((r != 0) && Comp::gt(A[--r], while ((r != 0) && Comp::gt(A[--r], pivot)); pivot)); swap(A, l, r); // Swap out-of-place values swap(A, l, r); // Swap out-of-place values } while (l < r); // Stop when they cross } while (l < r); // Stop when they cross swap(A, l, r); // Reverse last swap swap(A, l, r); // Reverse last swap return l; // Return first pos on right return l; // Return first pos on right} The cost for partition is  (n).

Partition Example

Quicksort Example

Cost of Quicksort Best case: Always partition in half. Worst case: Bad partition. Average case: T(n) = n /(n-1)  (T(k) + T(n-k)) Optimizations for Quicksort: –Better Pivot –Better algorithm for small sublists –Eliminate recursion k=1 n-1

Mergesort List mergesort(List inlist) { if (inlist.length() <= 1)return inlist; if (inlist.length() <= 1)return inlist; List l1 = half of the items from inlist; List l1 = half of the items from inlist; List l2 = other half of items from inlist; List l2 = other half of items from inlist; return merge(mergesort(l1), return merge(mergesort(l1), mergesort(l2)); mergesort(l2));}

Mergesort Implementation template template void mergesort(Elem A[], Elem temp[], int left, int right) { int left, int right) { int mid = (left+right)/2; int mid = (left+right)/2; if (left == right) return; if (left == right) return; mergesort (A, temp, left, mid); mergesort (A, temp, left, mid); mergesort (A, temp, mid+1, right); mergesort (A, temp, mid+1, right); for (int i=left; i<=right; i++) // Copy for (int i=left; i<=right; i++) // Copy temp[i] = A[i]; temp[i] = A[i]; int i1 = left; int i2 = mid + 1; int i1 = left; int i2 = mid + 1; for (int curr=left; curr<=right; curr++) { for (int curr=left; curr<=right; curr++) { if (i1 == mid+1) // Left exhausted if (i1 == mid+1) // Left exhausted A[curr] = temp[i2++]; A[curr] = temp[i2++]; else if (i2 > right) // Right exhausted else if (i2 > right) // Right exhausted A[curr] = temp[i1++]; A[curr] = temp[i1++]; else if (Comp::lt(temp[i1], temp[i2])) else if (Comp::lt(temp[i1], temp[i2])) A[curr] = temp[i1++]; A[curr] = temp[i1++]; else A[curr] = temp[i2++]; else A[curr] = temp[i2++];}}

Optimized Mergesort template template void mergesort(Elem A[], Elem temp[], int left, int right) { int left, int right) { if ((right-left) <= THRESHOLD) { if ((right-left) <= THRESHOLD) { inssort (&A[left],right-left+1); inssort (&A[left],right-left+1); return; return; } int i, j, k, mid = (left+right)/2; int i, j, k, mid = (left+right)/2; if (left == right) return; if (left == right) return; mergesort (A, temp, left, mid); mergesort (A, temp, left, mid); mergesort (A, temp, mid+1, right); mergesort (A, temp, mid+1, right); for (i=mid; i>=left; i--) temp[i] = A[i]; for (i=mid; i>=left; i--) temp[i] = A[i]; for (j=1; j<=right-mid; j++) for (j=1; j<=right-mid; j++) temp[right-j+1] = A[j+mid]; temp[right-j+1] = A[j+mid]; for (i=left,j=right,k=left; k<=right; k++) for (i=left,j=right,k=left; k<=right; k++) if (temp[i] < temp[j]) A[k] = temp[i++]; if (temp[i] < temp[j]) A[k] = temp[i++]; else A[k] = temp[j--]; else A[k] = temp[j--];}

Mergesort Cost Mergesort cost: Mergsort is also good for sorting linked lists. Mergesort requires twice the space.

Heapsort template template void heapsort(Elem A[], int n) { // Heapsort Elem mval; Elem mval; maxheap H(A, n, n); maxheap H(A, n, n); for (int i=0; i<n; i++) // Now sort for (int i=0; i<n; i++) // Now sort H.removemax(mval); // Put max at end H.removemax(mval); // Put max at end} Use a max-heap, so that elements end up sorted within the array. Cost of heapsort: Cost of finding K largest elements:

Heapsort Example (1)

Heapsort Example (2)

Binsort (1) A simple, efficient sort: for (i=0; i<n; i++) B[A[i]] = A[i]; B[A[i]] = A[i]; Ways to generalize: –Make each bin the head of a list. –Allow more keys than records.

Binsort (2) template template void binsort(Elem A[], int n) { List B[MaxKeyValue]; List B[MaxKeyValue]; Elem item; Elem item; for (i=0; i<n; i++) B[A[i]].append(A[i]); for (i=0; i<n; i++) B[A[i]].append(A[i]); for (i=0; i<MaxKeyValue; i++) for (i=0; i<MaxKeyValue; i++) for (B[i].setStart(); for (B[i].setStart(); B[i].getValue(item); B[i].next()) B[i].getValue(item); B[i].next()) output(item); output(item);}Cost:

Radix Sort (1)

Radix Sort (2) template template void radix(Elem A[], Elem B[], int n, int k, int r, int cnt[]) { int n, int k, int r, int cnt[]) { // cnt[i] stores # of records in bin[i] // cnt[i] stores # of records in bin[i] int j; int j; for (int i=0, rtok=1; i<k; i++, rtok*=r) { for (int i=0, rtok=1; i<k; i++, rtok*=r) { for (j=0; j<r; j++) cnt[j] = 0; for (j=0; j<r; j++) cnt[j] = 0; // Count # of records for each bin // Count # of records for each bin for(j=0; j<n; j++) cnt[(A[j]/rtok)%r]++; for(j=0; j<n; j++) cnt[(A[j]/rtok)%r]++; // cnt[j] will be last slot of bin j. // cnt[j] will be last slot of bin j. for (j=1; j<r; j++) for (j=1; j<r; j++) cnt[j] = cnt[j-1] + cnt[j]; cnt[j] = cnt[j-1] + cnt[j]; for (j=n-1; j>=0; j--)\ for (j=n-1; j>=0; j--)\ B[--cnt[(A[j]/rtok)%r]] = A[j]; B[--cnt[(A[j]/rtok)%r]] = A[j]; for (j=0; j<n; j++) A[j] = B[j]; for (j=0; j<n; j++) A[j] = B[j]; }} }}

Radix Sort Example

Radix Sort Cost Cost:  (nk + rk) How do n, k, and r relate? If key range is small, then this can be  (n). If there are n distinct keys, then the length of a key must be at least log n. –Thus, Radix Sort is  (n log n) in general case

Empirical Comparison (1)

Empirical Comparison (2)

Sorting Lower Bound We would like to know a lower bound for all possible sorting algorithms. Sorting is O(n log n) (average, worst cases) because we know of algorithms with this upper bound. Sorting I/O takes  (n) time. We will now prove  (n log n) lower bound for sorting.

Decision Trees

Lower Bound Proof There are n! permutations. There are n! permutations. A sorting algorithm can be viewed as determining which permutation has been input. A sorting algorithm can be viewed as determining which permutation has been input. Each leaf node of the decision tree corresponds to one permutation. Each leaf node of the decision tree corresponds to one permutation. A tree with n nodes has  (log n) levels, so the tree with n! leaves has  (log n!) =  (n log n) levels. A tree with n nodes has  (log n) levels, so the tree with n! leaves has  (log n!) =  (n log n) levels. Which node in the decision tree corresponds to the worst case?