Quick Sort - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Quick Sort

Description:

Divide and Conquer cuts the problem in two parts each time, but uses the result of both parts: ... Another divide-and-conquer algorithm: ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 21
Provided by: BillL161
Category:
Tags: conquer | divide | quick | sort

less

Transcript and Presenter's Notes

Title: Quick Sort


1
Quick Sort
2
Outline
  • Prerequisites
  • Insertion Sort
  • Merge Sort
  • Objectives
  • Understand Quick Sort
  • Reference
  • HTDP Section 25.2

3
Background
  • Sorting takes an unordered collection and makes
    it an ordered one.
  • Quick Sort is the fastest known sorting algorithm.

1 2 3 4 5
6
12
101
5
35
42
77
1 2 3 4 5
6
4
Divide and Conquer
  • Divide and Conquer cuts the problem in two parts
    each time, but uses the result of both parts
  • cut the problem in two until the problem is
    trivial
  • solve for both parts
  • combine the solutions

5
Quick Sort
  • Another divide-and-conquer algorithm
  • Divide the unsorted list into 2 parts until one
    of the parts is empty
  • Arbitrarily choose the first item as the "pivot
    value"
  • One part contains everything smaller than the
    pivot value
  • The other part contains everything larger than
    the same pivot value
  • Append the sub-problem solutions

6
67
45
23
14
6
33
36
42
7
67
45
23
14
6
33
36
42
67
6
14
36
45
33
23
42
8
67
45
23
14
6
33
36
42
67
6
14
36
45
33
23
42
6
14
33
67
45
42
23
9
67
45
23
14
6
33
36
42
67
6
14
36
45
33
23
42
6
14
33
67
45
42
23
6
14
10
67
45
23
14
6
33
36
42
67
6
14
36
45
33
23
42
6
14
33
67
45
42
23
6
14
6
14
11
67
45
23
14
6
33
36
42
67
6
14
36
45
33
23
42
6
14
33
67
45
42
23
6
14
6
14
6
14
33
23
67
45
42
12
67
45
23
14
6
33
36
42
67
6
14
36
45
33
23
42
6
14
33
23
67
45
42
6
14
6
14
6
14
33
23
67
45
42
6
14
33
23
67
45
42
36
13
67
45
23
14
6
33
36
42
6
14
33
23
67
45
42
36
14
Summary
  • Divide the unsorted collection into two
  • Until one of the sub-lists is empty
  • Then append the sub-problem solutions

15
Comparing Quick Sort to Merge Sort
  • On a list that is not previously sorted, Quick
    Sort runs 2-3 times faster than Merge Sort
  • However, if the data are already sorted, Quick
    Sort is as slow as Insertion Sort
  • Moral of the story
  • If you want speed, use Quick Sort
  • But make sure the data are in random order
  • It might be worth the time to check the data
    first to see if it is sorted.

16
quick-sort.scm
17
Code Used
  • pick-list (listof number) ck number -gt
    (listof number)
  • (define (pick-list lst ck n)
  • (if (empty? lst)
  • empty
  • (if (ck (first lst) n)
  • (cons (first lst) (pick-list (rest lst)
    ck n))
  • (pick-list (rest lst) ck n))))
  • quick-sort unsorted list -gt sorted list
  • (define (quick-sort lst)
  • (if (empty? lst)
  • empty
  • (append
  • (quick-sort (pick-list lst lt (first lst)))
  • (list (first lst))
  • (quick-sort (pick-list lst gt (first
    lst))))))

18
Questions?
19
Summary
  • You should now know
  • Quick Sort

20
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com