Computer Science 101 A Survey of Computer Science - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Computer Science 101 A Survey of Computer Science

Description:

Computer Science 101 A Survey of Computer Science QuickSort Divide and Conquer Divide and Conquer Divide and Conquer is a common strategy used in computer science. – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 24
Provided by: TomWh6
Learn more at: https://home.wlu.edu
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 101 A Survey of Computer Science


1
Computer Science 101A Survey of Computer Science
  • QuickSort

2
Divide and Conquer
3
Divide and Conquer
  • Divide and Conquer is a common strategy used in
    computer science.
  • The idea is that for a given problem, we try to
    break it into smaller problems (perhaps of the
    same type and then solve the smaller problems)
  • Of course, we must consider how to solve the
    smaller problems.

4
Sorting -Quicksort
  • Strategy - Divide and Conquer
  • Partition list with small elements in first part
    and large elements in second part
  • Sort the first part.
  • Sort the second part.

5
Quicksort (cont.)
  • Question - How do we sort the sections?Answer -
    Apply Quicksort to them.
  • Recursive algorithm - one which makes use of
    itself to solve smaller problems of the same type.

6
Quicksort (cont.)
  • Question - Will this recursive process ever
    stop?
  • Answer - Yes, when the problem is small enough
    , we no longer use recursion. Such cases are
    called anchor cases.

7
Recursion Example
  • The factorial function could be defined this
    way n! 1 if n1 n ((n-1)!)
    otherwise
  • Example 4! 4 x 3! 4 x 3 x 2! 4 x 3 x 2
    x 1! 4 x 3 x 2 x 1

8
Quicksort - Partitioning
  • To partition, we choose a pivot element from
    the list.
  • The elements which are less than or equal to the
    pivot go into the first section.
  • The elements larger than the pivot go into the
    second section.

9
Quicksort - The Pivot
  • Ideal would be to choose the median as the pivot,
    but this would take too long.
  • Some programs just choose the first element.
  • Our choice - choose the median of the first three
    elements.

10
Quicksort Partition
  • Variables
  • N(I),N(I1), , N(K) - list to partition
  • P - position of the pivot element
  • L - Right hand marker for the first section
  • U - Left hand marker for the second section

11
Quicksort Partition Algorithm
  • Exchange the median of the first 3 elements
    with the first elementSet P to first position of
    listSet L to second position of listSet U to
    last position of listWhile L U do While N(L)
    ? N(P) do Set L to L1 end-of-loop While N(U)
    gt N(P) do Set U to U-1 end-of-loop If L lt U
    then Exchange N(L) and N(U)end-of-loopExchange
    N(P) and N(U)

12
QuickSort - The Algorithm
  • If the list to sort has more than 1 element
    then If the list has exactly two elements then
    If the elements are out of order
    then Exchange them else Perform the
    Partition Algorithm on list Apply QuickSort
    to the first section Apply QuickSort to the
    second section
  • Note Anchor cases are when the list has 1 or 2
    elements recursion is used for 3 or more.

13
Quicksort Example
14
Quicksort Example (Cont.)
15
Quicksort Example (Cont.)
16
Quicksort Example (Cont.)
17
Quicksort Example (Cont.)
18
Quicksort Example (Cont.)
19
Quicksort Example (Cont.)
20
Quicksort Example (Cont.)
21
Quicksort Example (Cont.)
22
Student(?) sorts exam papers
23
If your way's so good, then prove it!
Write a Comment
User Comments (0)
About PowerShow.com