Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last 10 minutes to talk to you individually – if want me to look at code get it ready.

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


Download ppt "CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last."

Similar presentations


Ads by Google