Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Project Questions? http://wiki.western.edu/mcis/index.php /CIS280/f07/project5http://wiki.western.edu/mcis/index.php /CIS280/f07/project5

3 Yet Another Sort of Sort What’s interesting about the sorts we’ve seen so far? Insertion sort? Selection sort? Bubble sort? Heapsort? Why would you choose any of these in a real program?

4 Algorithmic Paradigms One of the key ideas in this class is to understand basic algorithm designs. There aren’t that many! With the O(n^2) sorts, the basic idea is similar to mathematical induction: Base case + step from size n to n+1 Each of these takes n steps and the work needed to take a step is O(n).

5 Divide and Conquer One of the big ideas is to decompose a problem “more evenly” – that is, instead of taking a problem of size n and solving the n-1 case then adding one more number, cut the problem into equal sized chunks. This is called “divide and conquer” and this the basis for many fast algorithms. Questions: How to split the problem up How to glue solutions together Recursion is used to solve the smaller sub- problems.

6 Quicksort To divide: pick a piece of data and divide the array into two pieces: one less than the selected element, the other greater than or equal to it. To unite divided array chunks: NOTHING! That is, everything ends up in the right place after the recursions. That’s EASY!

7 Example Start with the following array: {5, 3, 7, 2, 4, 8, 1, 9, 6} Using the first element to separate the halves, create the following: {3, 2, 1, 4, 5, 7, 8, 9, 6} The ordering of the low and high part might change. The 5 is called the pivot. Note that the 5 is “locked” in place.

8 Representations How do we represent the cutting of the array (the Sortable) in half without creating new objects?

9 Representations We’ll use a triple: the original sortable and upper and lower bounds (integers). Thus we never need to deal with sub- arrays directly. We need to “prime the pump” as follows: quicksort(s) = qs(s, 0, s.size()-1) The “qs” method is the real sort algorithm – this just initializes the range.

10 Base Case Since we’re using recursion, we need to worry about a “base case”, one in which there’s nothing to do. When is qs(s, low, high) trivial to compute? high <= low

11 Partitioning The goal of partitioning is to separate the range into two chunks, one below and one above the pivot. Let’s postulate another method: partition. This will take a range and return the location of the pivot in the partitioned array. int partition(Sortable s, int low, int high)

12 Quicksort void qs(Sortable s, int low, int high) { if (high – low > 0) { int p = partition(s, low, high); qs(s, low, p-1); // sort low side qs(s, p+1, high); // sort high side }}

13 Partitioning There are lots of ways to do partitioning – we’ll choose one of the more efficient ones. We’ll arbitrarily select the last element in the range to be the pivot. How could we choose something different? We’ll use two pointers (array indices) to indicate the lower and upper bound of the unpartitioned area. Initially, lo = low and hi = high – 1

14 Example { 3, 8, 4, 2, 5, 7, 6 } lo hi Strategy: slide lo up and hi down until something out of place appears. If they haven’t crossed, swap the elements at lo and hi, push them one closer to the middle, and repeat.

15 Example { 3, 8, 4, 2, 5, 7, 6 } lo hi After the slide, lo points to something that is too big and hi points to something that is too low (compared to pivot)

16 Example { 3, 5, 4, 2, 8, 7, 6 } lo hi After the swap

17 Example { 3, 5, 4, 2, 8, 7, 6 } lo hi Then push them one closer …

18 Example { 3, 5, 4, 2, 8, 7, 6 } hi lo And slide again … This time they have crossed so we’re done (almost). To finish, swap lo and the pivot, then return “lo”. { 3, 5, 4, 2, 6, 7, 8 }

19 As Code … int partition(Sortable s, int low, int high) { int lo = low; int hi = high – 1; while (lo <= hi) { while (lo < high && s.gtr(high, lo)) lo++; while (hi >= low && !s.gtr(high, hi)) hi--; if (lo <= hi) { // in case they crossed … s.swap(lo, hi); lo++;hi--;}} s.swap(lo, high); return lo; }

20 Examples Time to work examples on the board.


Download ppt "CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php."

Similar presentations


Ads by Google