CS 280 Data Structures - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS 280 Data Structures

Description:

Recursion is used to solve the smaller sub-problems. Quicksort ... Using the first element to separate the halves, create the following: ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 14
Provided by: johnpe2
Learn more at: http://wiki.western.edu
Category:

less

Transcript and Presenter's Notes

Title: CS 280 Data Structures


1
CS 280Data Structures
  • Professor John Peterson

2
Project
  • Not a work day but Ill answer questions as long
    as they keep coming! Ill 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
  • Whats interesting about the sorts weve 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 arent that many!
  • With the O(n2) sorts, the basic idea is similar
    to mathematical induction
  • Base case step from size n to n1
  • 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. Thats 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
  • Well 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 were using recursion, we need to worry
    about a base case, one in which theres nothing
    to do.
  • When is qs(s, low, high) trivial to compute?
  • high lt low

11
Partitioning
  • The goal of partitioning is to separate the range
    into two chunks, one below and one above the
    pivot.
  • Lets 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 gt 0)
  • int p partition(s, low, high)
  • qs(s, low, p-1) // sort low side
  • qs(s, p1, high) // sort high side

13
Partitioning
  • There are lots of ways to do partitioning well
    choose one of the more efficient ones.
  • Well arbitrarily select the last element in the
    range to be the pivot. How could we choose
    something different?
  • Well use two pointers (array indices) to
    indicate the lower and upper bound of the
    unpartitioned area.
  • Initially, lo low and hi high 1
Write a Comment
User Comments (0)
About PowerShow.com