Chapter 10: Sorting1 Sorting. Chapter 10: Sorting2 Chapter Outline How to use standard sorting functions in How to implement these sorting algorithms:

Slides:



Advertisements
Similar presentations
ITEC200 Week10 Sorting. pdp 2 Learning Objectives – Week10 Sorting (Chapter10) By working through this chapter, students should: Learn.
Advertisements

Visual C++ Programming: Concepts and Projects
Chapter 10: Sorting1 Sorting Based on Chapter 10 of Koffmann and Wolfgang.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Spring 2010CS 2251 Sorting Chapter 8. Spring 2010CS 2252 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn.
C++ Plus Data Structures
Sorting Chapter 10.
QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record,
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Section 8.4 Insertion Sort CS Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.
Sorting HKOI Training Team (Advanced)
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 10: Sorting1 Sorting Based on Chapter 10 of Koffmann and Wolfgang.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting 2 Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Sorting اعداد: ابوزيد ابراهيم حامد سعد صبرة حميده الشاذلي عبدالاه السيد محمد احمد.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Sorting. 2 Chapter Outline How to use standard sorting methods in the Java API How to implement these sorting algorithms: Selection sort Bubble sort.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Prof. U V THETE Dept. of Computer Science YMA
Sorting Mr. Jacobs.
Sorting Chapter 10.
Sorting Chapter 8.
10.6 Shell Sort: A Better Insertion
Description Given a linear collection of items x1, x2, x3,….,xn
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
10.3 Bubble Sort Chapter 10 - Sorting.
Sorting means The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) Sorting.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
8/04/2009 Many thanks to David Sun for some of the included slides!
C++ Plus Data Structures
Sorting Chapter 8 CS 225.
Sorting Chapter 8.
CSE 373 Data Structures and Algorithms
Sorting Chapter 10.
Chapter 10 Sorting Algorithms
Sorting Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
10.3 Bubble Sort Chapter 10 - Sorting.
Presentation transcript:

Chapter 10: Sorting1 Sorting

Chapter 10: Sorting2 Chapter Outline How to use standard sorting functions in How to implement these sorting algorithms: Selection sort Bubble sort Insertion sort Shell sort Merge sort Heapsort Quicksort

Chapter 10: Sorting3 Chapter Outline (2) Understand the performance of these algorithms Which to use for small arrays Which to use for medium arrays Which to use for large arrays

Chapter 10: Sorting4 C++ Standard Library Sort Functions The C++ standard library (in ) provides the following functions: template void sort(RI first, RI last); template void sort(RI first, RI last, Compare comp); The template parameter RI is a random-access iterator. The data to be sorted is in the range first.. last, where last is one past the last data value. In practice you would tend to use these In this class, you will implement some yourself

Chapter 10: Sorting5 Using the sort functions Assume that array items contains 16 integer values. sort(items, items + 16); will sort the whole array. sort(items, items + 8); will sort the first half. sort(items, items + 16, greater ()); will sort in descending order. Assume that v is a vector and d is a deque. sort(v.begin(), v.end()); // sorts vector v sort(d.begin(), d.end()); // sorts deque d

Chapter 10: Sorting6 Using the sort functions Define comparison for Person class: struct Compare_Person { bool operator()(const Person& p1, const Person& p2) { return (p1.family_name < p2.family_name) || ((p1.family_name == p2.family_name) && (p1.given_name < p2.given_name)); }; To sort the vector people_list sort(people_list.begin(), people_list.end(), Compare_Person());

Chapter 10: Sorting7 Conventions of Presentation Write algorithms for a range defined by random- access iterators. For convenience, examples show integers

Chapter 10: Sorting8 Selection Sort A relatively easy to understand algorithm Sorts an array in passes Each pass selects the next smallest element At the end of the pass, places it where it belongs Efficiency is O(n 2 ), hence called a quadratic sort Performs: O(n 2 ) comparisons O(n) exchanges (swaps)

Chapter 10: Sorting9 Selection Sort Algorithm 1.for fill = 0 to n-2 do // steps 2-6 form a pass 2. set pos_min to fill 3. for next = fill+1 to n-1 do 4. if item at next < item at pos_min 5. set pos_min to next 6. Exchange item at pos_min with one at fill

Chapter 10: Sorting10 Selection Sort Example scan 0-4, smallest 20 swap 35 and scan 1-4, smallest 30 swap 65 and scan 2-4, smallest 35 swap 65 and scan 3-4, smallest 60 swap 60 and done

Chapter 10: Sorting11 Selection Sort Code template void selection_sort(RI first, RI last) { for (RI fill = first; fill != last - 1; ++fill) { // Invariant: table[first] through table[fill - 1] is sorted RI pos_min = fill; for (RI next = fill + 1; next != last; ++next) { // Invariant: pos_min references the smallest item in // table[fill] through table[last - 1] if (*next < *pos_min) { pos_min = next; } // Assert pos_min references the minimum value in table[fill] // through table[last - 1]. // If the next smallest item is not a fill, // exchange *fill and *pos_min. if (fill != pos_min) std::iter_swap(pos_min, fill); }

Chapter 10: Sorting12 Selection Sort using Compare template void selection_sort(RI first, RI last, Compare comp) { for (RI fill = first; fill != last - 1; ++fill) { // Invariant table[first] through table[fill - 1] is sorted RI pos_min = fill; for (RI next = fill + 1; next != last; ++next) { // Invariant: pos_min references the smallest item in // table[fill] through table[last - 1]. if (comp(*next, *pos_min)) { pos_min = next; } // Assert pos_min references the minimum value in table[fill] // through table[last - 1]. // If the next smallest item is not a fill, // exchange *fill and *pos_min. if (fill != pos_min) std::iter_swap(pos_min, fill); }

Chapter 10: Sorting13 Bubble Sort Compares adjacent array elements Exchanges their values if they are out of order Smaller values bubble up to the top of the array Larger values sink to the bottom

Chapter 10: Sorting14 Bubble Sort Example

Chapter 10: Sorting15 Bubble Sort Algorithm 1.do 2. for each pair of adjacent array elements 3. if values are out of order 4. Exchange the values 5.while the array is not sorted

Chapter 10: Sorting16 Bubble Sort Algorithm, Refined 1.do 2. Initialize exchanges to false 3. for each pair of adjacent array elements 4. if values are out of order 5. Exchange the values 6. Set exchanges to true 7.while exchanges

Chapter 10: Sorting17 Analysis of Bubble Sort Excellent performance in some cases But very poor performance in others! Works best when array is nearly sorted to begin with Worst case number of comparisons: O(n 2 ) Worst case number of exchanges: O(n 2 ) Best case occurs when the array is already sorted: O(n) comparisons O(1) exchanges (none actually)

Chapter 10: Sorting18 Bubble Sort Code template void bubble_sort(RI first, RI last) { int pass = 1; bool exchanges; do { // Invariant: Elements after position last - pass // are in place. exchanges = false; // No exchanges yet. // Compare each pair of adjacent elements. for (RI first_of_pair = first; first_of_pair != last - pass; ++first_of_pair) { RI second_of_pair = first_of_pair + 1; if (*second_of_pair < *first_of_pair) { // Exchange pair. std::iter_swap(first_of_pair, second_of_pair); exchanges = true; // Set flag. } pass++; } while (exchanges); // Assert: Array is sorted. }

Chapter 10: Sorting19 Insertion Sort Based on technique of card players to arrange a hand Player keeps cards picked up so far in sorted order When the player picks up a new card Makes room for the new card Then inserts it in its proper place

Chapter 10: Sorting20 Insertion Sort Algorithm For each element from 2nd (next_pos = 1) to last: Insert element at next_pos where it belongs Increases sorted subarray size by 1 To make room: Hold next_pos value in a variable Shuffle elements to the right until gap at right place

Chapter 10: Sorting21 Insertion Sort Example

Chapter 10: Sorting22 Insertion Sort Code template void insertion_sort(RI first, RI last) { for (RI next_pos = first + 1; next_pos != last; ++next_pos) { // Invariant: elements at position first through next_pos - 1 are sorted. // Insert element at position next_pos in the sorted subarray. insert(first, next_pos); } template void insert(RI first, RI next_pos) { while (next_pos != first && *next_pos < *(next_pos - 1)) { std::iter_swap(next_pos, next_pos - 1); // Exchange pair of values --next_pos; // Check next smaller element }

Chapter 10: Sorting23 More Efficient Version template void insertion_sort2(RandomAccessIterator first, RandomAccessIterator last) { for (RandomAccessIterator next_pos = first+1; next_pos != last; ++next_pos) { // Invariant: elements at position first thru next_pos - 1 are sorted. // Insert element at position next_pos // in the sorted subarray. insert2(first, next_pos); } template void insert2(RI first, RI next_pos) { typename std::iterator_traits ::value_type next_val = *next_pos; // next_val is element to insert. while (next_pos != first && next_val < *(next_pos - 1)) { *next_pos = *(next_pos - 1); --next_pos; // Check next smaller element. } *next_pos = next_val; // Store next_val where it belongs. }

Chapter 10: Sorting24 Analysis of Insertion Sort Maximum number of comparisons: O(n 2 ) In the best case, number of comparisons: O(n) # shifts for an insertion = # comparisons - 1 When new value smallest so far, # comparisons A shift in insertion sort moves only one item Bubble or selection sort exchange: 3 assignments

Chapter 10: Sorting25 Comparison of Quadratic Sorts None good for large arrays!

Chapter 10: Sorting26 Shell Sort: A Better Insertion Sort Shell sort is a variant of insertion sort It is named after Donald Shell Average performance: O(n 3/2 ) or better Divide and conquer approach to insertion sort Sort many smaller subarrays using insertion sort Sort progressively larger arrays Finally sort the entire array These arrays are elements separated by a gap Start with large gap Decrease the gap on each “pass”

Chapter 10: Sorting27 Illustration of Shell Sort

Chapter 10: Sorting28 Analysis of Shell Sort Intuition: Reduces work by moving elements farther earlier Its general analysis is an open research problem Performance depends on sequence of gap values For sequence 2 k, performance is O(n 2 ) Hibbard’s sequence (2 k -1), performance is O(n 3/2 ) We start with n/2 and repeatedly divide by 2.2 Empirical results show this is O(n 5/4 ) or O(n 7/6 ) No theoretical basis (proof) that this holds

Chapter 10: Sorting29 Shell Sort Algorithm 1.Set gap to n/2 2.while gap > 0 3. for each element from gap to end, by gap 4. Insert element in its gap-separated sub-array 5. if gap is 2, set it to 1 6. otherwise set it to gap / 2.2

Chapter 10: Sorting30 Shell Sort Algorithm: Inner Loop 3.1set next_pos to position of element to insert 3.2set next_val to value of that element 3.3while next_pos > gap and element at next_pos-gap is > next_val 3.4Shift element at next_pos-gap to next_pos 3.5Decrement next_pos by gap 3.6Insert next_val at next_pos

Chapter 10: Sorting31 Shell Sort Code template void shell_sort(RI first, RI last) { // Set initial gap between adjacent elements. int gap = (last - first) / 2; while (gap > 0) { for (RI next_pos = first + gap; next_pos != last; ++next_pos) { // Insert element at next_pos in its subarray. shell_insert(first, next_pos, gap); } // End for. // Reset gap for next pass. if (gap == 2) { gap = 1; } else { gap = int(gap / 2.2); }

Chapter 10: Sorting32 Shell Sort Code (2) template void shell_insert(RI first, RI next_pos, int gap) { typename std::iterator_traits ::value_type next_val = *next_pos; // Shift all values > next_val in subarray down by gap. while ((next_pos > first + gap - 1) // First element not shifted. && (next_val < *(next_pos - gap))) { *next_pos = *(next_pos - gap); // Shift down. next_pos -= gap; // Check next position in subarray. } *next_pos = next_val; }

Chapter 10: Sorting33 Merge Sort A merge is a common data processing operation: Performed on two sequences of data Items in both sequences use same compare Both sequences in ordered of this compare Goal: Combine the two sorted sequences in one larger sorted sequence Merge sort merges longer and longer sequences

Chapter 10: Sorting34 Merge Algorithm (Two Sequences) Merging two sequences: 1.Access the first item from both sequences 2.While neither sequence is finished 1.Compare the current items of both 2.Copy smaller current item to the output 3.Access next item from that input sequence 3.Copy any remaining from first sequence to output 4.Copy any remaining from second to output

Chapter 10: Sorting35 Picture of Merge

Chapter 10: Sorting36 Analysis of Merge Two input sequences, total length n elements Must move each element to the output Merge time is O(n) Must store both input and output sequences An array cannot be merged in place Additional space needed: O(n)

Chapter 10: Sorting37 Merge Sort Algorithm Overview: Split array into two halves Sort the left half (recursively) Sort the right half (recursively) Merge the two sorted halves

Chapter 10: Sorting38 Merge Sort Algorithm (2) Detailed algorithm: if tSize  1, return (no sorting required) set hSize to tSize / 2 Allocate LTab of size hSize Allocate RTab of size tSize – hSize Copy elements 0.. hSize – 1 to LTab Copy elements hSize.. tSize – 1 to RTab Sort LTab recursively Sort RTab recursively Merge LTab and RTab into a

Chapter 10: Sorting39 Merge Sort Example

Chapter 10: Sorting40 Merge Sort Analysis Splitting/copying n elements to subarrays: O(n) Merging back into original array: O(n) Recursive calls: 2, each of size n/2 Their total non-recursive work: O(n) Next level: 4 calls, each of size n/4 Non-recursive work again O(n) Size sequence: n, n/2, n/4,..., 1 Number of levels = log n Total work: O(n log n)

Chapter 10: Sorting41 Merge Sort Code template void merge_sort(RI first, RI last) { if (last - first > 1) { // Split table into two new half tables. typedef typename std::iterator_traits ::value_type value_type; RI middle = first + (last - first) / 2; std::vector left_table(first, middle); std::vector right_table(middle, last); // Sort the halves. merge_sort(left_table.begin(), left_table.end()); merge_sort(right_table.begin(), right_table.end()); // Merge the halves back into the original table. merge(left_table.begin(), left_table.end(), right_table.begin(), right_table.end(), first); }

Chapter 10: Sorting42 Merge Sort Code (2) template void merge(RI left, RI end_left, RI right, RI end_right, RI out) { // While there is data in both input sequences while (left != end_left && right != end_right) { // Find the smaller and // insert it into the output sequence. if (*left < *right) { *out++ = *left++; } else { *out++ = *right++; }

Chapter 10: Sorting43 Heapsort Merge sort time is O(n log n) But requires (temporarily) n extra storage items Heapsort Works in place: no additional storage Offers same O(n log n) performance Idea (not quite in-place): Insert each element into a priority queue Repeatedly remove from priority queue to array Array slots go from 0 to n-1

Chapter 10: Sorting44 Heapsort Picture

Chapter 10: Sorting45 Heapsort Picture (2)

Chapter 10: Sorting46 Algorithm for In-Place Heapsort Build heap starting from unsorted array While the heap is not empty Remove the first item from the heap: Swap it with the last item Restore the heap property

Chapter 10: Sorting47 Heapsort Code template void heap_sort(RI first, RI last) { build_heap(first, last); shrink_heap(first, last); } template void build_heap(RI first, RI last) { int n = 1; // Invariant: table[first] through table[first + n - 1] is a heap. while (n < (last - first)) { ++n; // Add a new item to the heap and reheap. RI child = first + n - 1; RI parent = first + (child - first - 1) / 2; // Find parent. while (parent >= first && *parent < *child) { std::iter_swap(parent, child); // Exchange elements. child = parent; parent = first + (child first) / 2; }

Chapter 10: Sorting48 Heapsort Code (2) template void shrink_heap(RI first, RI last) { RI n = last; while (n != first) { --n; std::iter_swap(first, n); RI parent = first; while (true) { RI left_child = first + 2 * (parent - first) + 1; if (left_child >= n) break; // No more children. RI right_child = left_child + 1; // Find the larger of the two children. RI max_child = left_child; if (right_child < n // There is a right child. && *left_child < *right_child) max_child = right_child; // If the parent is smaller than the larger child, if (*parent < *max_child) { std::iter_swap(parent, max_child); parent = max_child; } else break; // Heap property is restored. Exit the loop. }

Chapter 10: Sorting49 Heapsort Analysis Insertion cost is log i for heap of size i Total insertion cost = log(n)+log(n-1)+...+log(1) This is O(n log n) Removal cost is also log i for heap of size i Total removal cost = O(n log n) Total cost is O(n log n)

Chapter 10: Sorting50 Quicksort Developed in 1962 by C. A. R. Hoare Given a pivot value: Rearranges array into two parts: Left part  pivot value Right part > pivot value Average case for Quicksort is O(n log n) Worst case is O(n 2 )

Chapter 10: Sorting51 Quicksort Example

Chapter 10: Sorting52 Algorithm for Quicksort first and last are end points of region to sort if first < last Partition using pivot, which ends in piv_index Apply Quicksort recursively to left subarray Apply Quicksort recursively to right subarray Performance: O(n log n) provide piv_index not always too close to the end Performance O(n 2 ) when piv_index always near end

Chapter 10: Sorting53 Quicksort Code template void quick_sort(RI first, RI last) { if (last - first > 1) { // There is data to be sorted. // Partition the table. RI pivot = partition(first, last); // Sort the left half. quick_sort(first, pivot); // Sort the right half. quick_sort(pivot + 1, last); }

Chapter 10: Sorting54 Algorithm for Partitioning 1.Set pivot value to a[fst] 2.Set up to fst and down to lst 3.do 4. Increment up until a[up] > pivot or up = lst 5. Decrement down until a[down] <= pivot or down = fst 6. if up < down, swap a[up] and a[down] 7.while up is to the left of down 8.swap a[fst] and a[down] 9.return down as pivIndex

Chapter 10: Sorting55 Trace of Algorithm for Partitioning

Chapter 10: Sorting56 Partitioning Code template RI partition(RI first, RI last) { // Start up and down at either end of the sequence. // The first table element is the pivot value. RI up = first + 1; RI down = last - 1; do { while ((up != last - 1) && !(*first < *up)) { ++up; } while (*first < *down) { --down; } if (up < down) { // if up is to the left of down, std::iter_swap(up, down); } } while (up < down); // Repeat while up is left of down. std::iter_swap(first, down); return down; }

Chapter 10: Sorting57 Revised Partitioning Algorithm Quicksort is O(n 2 ) when each split gives 1 empty array This happens when the array is already sorted Solution approach: pick better pivot values Use three “marker” elements: first, middle, last Let pivot be one whose value is between the others

Chapter 10: Sorting58 Testing Sortiing Algorithms Need to use a variety of test cases Small and large arrays Arrays in random order Arrays that are already sorted (and reverse order) Arrays with duplicate values Compare performance on each type of array

Chapter 10: Sorting59 Variety of partitioning algorithms have been published One that partitions an array into three segments was introduced by Edsger W. Dijkstra Problem: partition a disordered three-color flag into three contiguous segments Segments represent the pivot value The Dutch National Flag Problem Dutch National FlagScrambled Dutch National Flag

Chapter 10: Sorting60 The Dutch National Flag Problem

Chapter 10: Sorting61 Summary of Performance