Wednesday, April 11, 2018 Announcements… For Today…

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
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.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
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.
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.
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.
CHAPTER 11 Sorting.
Sorting Chapter 10.
Quicksort.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
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.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Algorithm Efficiency and Sorting
Warmup What is an abstract class?
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
QuickSort QuickSort is often called Partition Sort.
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Teach A level Computing: Algorithms and Data Structures
Monday, March 19, 2018 Announcements… For Today… For Next Time…
Sorting Chapter 8.
Lab 08 - BST.
10.6 Shell Sort: A Better Insertion
Lab 10 - Quicksort.
Description Given a linear collection of items x1, x2, x3,….,xn
How can this be simplified?
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
10.3 Bubble Sort Chapter 10 - Sorting.
Lab 04 – Linked List.
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
24 Searching and Sorting.
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
Chapter 4.
EE 312 Software Design and Implementation I
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Algorithm Efficiency and Sorting
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Sorting Chapter 10.
Data Structures & Algorithms
Algorithm Efficiency and Sorting
CSC 380: Design and Analysis of Algorithms
CS203 Lecture 15.
Algorithm Efficiency and Sorting
Divide and Conquer Merge sort and quick sort Binary search
Lab 03 – Linked List.
Advanced Sorting Methods: Shellsort
Presentation transcript:

Wednesday, April 11, 2018 Announcements… For Today… Sorting Announcements… Lab 09 – Pokemon Reviews Lab 10 – Quicksort due Monday, April 16 Direct emails to cs235ta@cs.byu.edu (w/netid) Year end deliverables: Lab 11 – AVL (Bonus) Final Exam (15%) Comprehensive Testing Center For Today… Quicksort 10.9 11.1

Attendance Quiz #38 Sorting

Lab 10 - Quicksort

Lab 10 - Quicksort Sorting "Quicksort is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. It is also known as partition-exchange sort. In the worst case, Quicksort makes O(n2) comparisons, though this behavior is rare. Quicksort is typically faster in practice than other O(n log n) algorithms. Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort can be implemented as an in-place partitioning algorithm."

Quicksort Commands Recursion COMMAND DESCRIPTION OUTPUT QuickSort <capacity> Dynamically allocate a QuickSort array of size capacity. Set current number of elements (Size) to 0. OK Error AddToArray <data1> <data2>... Add data element(s) to QuickSort array. Duplicates are allowed. Dynamically increase array size as needed (double array capacity for each increase.) OK Error Capacity Return the size of the QuickSort array. size Clear Delete all nodes from the QuickSort array. OK Size Return the number of elements currently in the QuickSort array. elements Sort <left> <right> QuickSort the elements in the QuickSort array from index <left> to index <right> (where <right> is one past last element) using median and partition functions. OK Error SortAll QuickSort all the elements in the QuickSort array using median and partition functions. OK Error MedianOfThree <left> <right> 1) Calculate the middle index (middle = (left + right)/2), then 2) bubble-sort the values at the left, middle, and right indices. (<right> is one past last element.) Pivot Index -1 error Partition <left> <right> <pivot> Using the revised pivot algorithm, partition the QuickSort array (<left>, <right> and <pivot> indexes) around the pivot value. Values smaller than the pivot should be placed to the left of the pivot while values larger than the pivot should be placed to the right of the pivot. (<right> is one past last element.) Pivot Index -1 error PrintArray Print the contents of the QuickSort array as comma separated values (using a friend insertion (<<) operator and toString() function.) Array values. Empty Stats (Bonus) Output the number of comparisons and exchanges used by the SortAll command. Comparisons,Exchanges

Quicksort Example QuickSort 2 AddToArray 1 6 5 4 2 8 PrintArray Sorting QuickSort 2 AddToArray 1 6 5 4 2 8 PrintArray Capacity Size Sort 0 3 AddToArray 6 -5 -1 -3 -2 -4 MedianOfThree 0 12 Partition 0 12 4 SortAll Stats Clear QuickSort 2 OK AddToArray 1,6,5,4,2,8 OK PrintArray 1,6,5,4,2,8 Capacity 8 Size 6 Sort 0,3 OK PrintArray 1,5,6,4,2,8 AddToArray 6,-5,-1,-3,-2,-4 OK PrintArray 1,5,6,4,2,8,6,-5,-1,-3,-2,-4 MedianOfThree 0,12 = 6 PrintArray -4,5,6,4,2,8,1,-5,-1,-3,-2,6 Partition 0,12,4 = 6 PrintArray 1,-2,-3,-1,-4,-5,2,8,4,6,5,6 Size 12 SortAll OK PrintArray -5,-4,-3,-2,-1,1,2,4,5,6,6,8 Stats 53,22 Clear OK PrintArray Empty

Steps to Success Step 1 - Begin with a main function. Sorting Step 1 - Begin with a main function. As with most labs this semester, you will need to write your own main function. The main needs to be able to parse a given input file. Step 2 - Begin the QuickSort class. The templated QuickSort class is derived from the abstract template interface class QSInterface. Your QuickSort class should contain a dynamically-allocated array. Before you focus too much on the actual sorting algorithm, make sure the logistics of the class work correctly. Focus on createArray(), addToArray(), clear(), getSize(), and toString() member functions. Step 3 - Write your medianOfThree() function in the QuickSort class. The Median of Three function should take a left index and right index as parameters. It should then calculate the index in the middle of the left and right indexes (rounding down if necessary). Sort the left, middle, and right numbers from smallest to largest in the array. Finally, return the index of the middle value. This will be your pivot value in the sortAll() function. Note that medianOfThree() is not used in your partition() function. The pivot value will be supplied by the input files.

Steps to Success Sorting Step 4 - Write your partition() function in the QuickSort class. The partition() function should begin by swapping the leftmost element from the array with whatever value is contained at the pivot index. Now, follow the revised pivot algorithm presented in the textbook (pg. 611) to arrange the array such that all elements lower than the given pivot are at its left and all elements higher are at its right. The partition() function should return the location of the pivot index. Step 5 - Write the sortAll() function, using your medianOfThree() and partition() functions. Note that this function will be recursive. Most people use sortAll() as a recursive starter function that then calls another function to do the sorting. To sort, you should first call your medianOfThree() function and sort the first, middle, and last elements. This function returns the pivot index. Now, call your partition() function, using the number returned from medianOfThree() as the pivot index. This function will return a pivot index, or the index where your array is split. Finally, you will recursively call your sort() function on two halves of your array, with one half from the left until the pivot, and the other half from the pivot to the right.

Requirements Trees Points Requirement (40 Points) 5 argv[1] and argv[2] used for input / output streams respectively. No execution user interaction (i.e. system("pause"); or getchr();). Your template QuickSort class contains a dynamically-allocated element array and implements the abstract interface QSInterface class. The QuickSort command deletes the current array (if any) and then dynamically allocates a new element array. The Capacity and Clear commands execute as described above. No STL container is used anywhere in your QuickSort class. (lab10_in_01.txt). Items are added to the QuickSort array using the AddToArray command. Duplicate/multiple items can be added with one command. The QuickSort array dynamically grows (by doubling) as the number of items added exceeds the current array capacity. The PrintArray command outputs a comma-separated string representation of the array using an insertion (<<) friend operator and the toString() method. The Size command outputs the number of elements in the QuickSort array. (lab10_in_02.txt). Your MedianOfThree command (medianOfThree() function) properly sorts the first, middle, and last elements (as described above.) (lab10_in_03.txt). Your Partition command (partition() function) arranges the array such that all elements less than the given pivot are to the left and all elements greater than the given pivot are to the right (using the revised pivot algorithm.) (lab10_in_04.txt). The SortAll command (sortAll() function) utilizes your medianOfThree() and partition() functions to recursively sort the entire array. (lab10_in_05.txt). The Sort command (sort() function) utilizes your medianOfThree() and partition() functions to recursively sort a QuickSort subarray. (lab10_in_06.txt). BONUS: The Stats command outputs the number of comparisons and exchanges used by the sort commands (Sort and SortAll.) (lab10_in_07.txt). VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported.

10.9, pgs. 604-614 10.9 Quicksort Algorithm for Quicksort Code for Quicksort Algorithm for Partitioning Code for partition A Revised Partition Algorithm Code for Revised partition Function 10.9, pgs. 604-614

A Revised Partition Algorithm Sorting Quicksort is O(n2) when each split yields one empty subarray, which is the case when the array is presorted. The worst possible performance occurs for a sorted array, which is not very desirable. A better solution is to pick the pivot value in a way that is less likely to lead to a bad split. Use three references: first, middle, last. Select the median of the these items as the pivot. Revised Partition Algorithm 1. Sort table[first], table[middle], and table[last-1]. 2. Move median value to first position (pivot value) by exchanging table[first] and table[middle]. 3. Initialize up to first + 1 and down to last - 1. …

Finding a Pivot Sorting 1 2 3 4 5 6 7 8 9 10 50 25 80 35 60 90 70 40 12 85 Up Pivot Up Down Down 50 25 12 35 60 90 70 40 80 85 Pivot Up Down 50 25 12 35 40 90 70 60 80 85 Pivot Down Up 40 25 12 35 50 90 70 60 80 85 Pivot

A Revised Partition Algorithm Sorting

middle = (last – first) / 2 Median of Three Sorting first last middle 44 75 23 43 55 12 64 77 33 middle = (last – first) / 2 = (8 – 0) / 2 = 4

Median of Three first middle last 33 75 23 43 44 12 64 77 55 Sorting first middle last 33 75 23 43 44 12 64 77 55 Sort these values

Exchange middle and first Revised Partitioning Sorting first middle 44 75 23 43 33 12 64 77 55 Exchange middle and first

Run the partition algorithm using the first element as the pivot Revised Partitioning Sorting up down 44 75 23 43 33 12 64 77 55 Run the partition algorithm using the first element as the pivot

Revised Partitioning up down 44 75 23 43 33 12 64 77 55 Sorting up down 44 75 23 43 33 12 64 77 55 up moves right until > 44 down move left until <= 44

continue until down <= up Revised Partitioning Sorting up down 44 12 23 43 33 75 64 77 55 then exchange and continue until down <= up

Revised Partitioning down up 44 12 23 43 33 75 64 77 55 Sorting down up 44 12 23 43 33 75 64 77 55 When down and up cross…

Exchange first with down Revised Partitioning Sorting down up 33 12 23 43 44 75 64 77 55 Exchange first with down down becomes pivot

Code for Revised partition Sorting /** Partition the table so that left values are less than pivot and right are greater then or equal to pivot. @return The position of the pivot value (originally at first) */ template<typename RI> RI partition(RI first, RI last) { /* Put the median of table[first], table[middle], table[last - 1] into table[first], and use this value as the pivot. */ bubble_sort3(first, last); // Swap first element with middle element. std::iter_swap(first, first + (last - first) / 2); // Start up and down at either end of the sequence with first as pivot RI up = first + 1; RI down = last - 1; do /* Invariant: All items in table[first] through table[up - 1] <= table[first] All items in table[down + 1] through table[last - 1] > table[first] */ while ((up != last - 1) && !(*first < *up)) ++up; // Assert: up equals last - 1 or table[up] > table[first]. while (*first < *down) --down; // Assert: down equals first or table[down] <= table[first]. // if up is to the left of down, exchange table[up] and table[down]. if (up < down) std::iter_swap(up, down); } while (up < down); // Repeat while up is left of down. // Exchange table[first] and table[down] thus putting the pivot value where it belongs. std::iter_swap(first, down); return down; // Return position of pivot. }

How many exchanges were required? How many comparisons? Sort the following integer array in ascending order using revised Quicksort. Show each step. 1 2 3 4 5 6 7 8 44 75 23 43 55 12 64 77 33 How many exchanges were required? How many comparisons?