Quick Sort - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Quick Sort

Description:

Quick Sort Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide-and-conquer, massively recursive sort. – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 25
Provided by: Charles484
Category:
Tags: quick | sort

less

Transcript and Presenter's Notes

Title: Quick Sort


1
Quick Sort

2
Quicksort
  • Quicksort is a well-known sorting algorithm
    developed by C. A. R. Hoare. The quick sort is an
    in-place, divide-and-conquer, massively recursive
    sort. It's essentially a faster in-place version
    of the merge sort. The quick sort algorithm is
    simple in theory, but very difficult to put into
    code (computer scientists tied themselves into
    knots for years trying to write a practical
    implementation of the algorithm, and it still has
    that effect on university students).

3
Quick Sort
  • The recursive algorithm consists of four steps
    (which closely resemble the merge sort)
  • If there are one or less elements in the array to
    be sorted, return immediately.
  • Pick an element in the array to serve as a
    "pivot" point. (Usually the right-most element in
    the array is used.)
  • Split the array into two parts - one with
    elements larger than the pivot and the other with
    elements smaller than the pivot.
  • Recursively repeat the algorithm for both halves
    of the original array.

4
Quick Sort
  • function quicksort(q)
  • var list less, pivotList, greater
  • if length(q) 1return q
  • select a pivot value pivot from q
  • for each x in q except the pivot element
  • if x lt pivot then add x to less
  • if x pivot then add x to greater
  • add pivot to pivotList
  • return concatenate(quicksort(less), pivotList,
    quicksort(greater))

5
Quick Sort Visualization
6
Quick Sort Visualization
7
Quick Sort Visualization
85
96
63
8
Quick Sort Visualization
85
96
63
9
Quick Sort Visualization
85
96
63
45

31
96
10
Quick Sort Visualization
85
96
63
45

31
96
11
Quick Sort Visualization
85
96
63
45

31
96

24
17

85
63
12
Quick Sort Visualization
85
96
63
45

31
96
13
Quick Sort Visualization
63
96
85
14
Quick Sort Visualization
15
Efficiency
n 8
Assuming the divide steps and the conquer steps
take time proportional to n, then the runtime of
each level is proportional to n. With logn 1
levels, the runtime is O(nlogn).
16
Efficiency
  • Best Case Situation
  • Assuming that the list breaks into two equal
    halves, we have two lists of size N/2 to sort. In
    order for each half to be partitioned,
    (N/2)(N/2) N comparisons are made. Also
    assuming that each of these list breaks into two
    equal sized sublists, we can assume that there
    will be at the most log(N) splits. This will
    result in a best time estimate of O(Nlog(N)) for
    Quicksort.

17
Efficiency
  • Worst Case Situation
  • In the worst case the list does not divide
    equally and is larger on one side than the other.
    In this case the splitting may go on N-1 times.
    This gives a worst-case time estimate of O(N²).
    Average Time
  • The average time for Quicksort is estimated to
    be O(Nlog(N)) comparisons.

18
Efficiency
  • The disadvantage of the simple version
    previously stated is that it requires extra
    storage space. The additional memory allocations
    required can also drastically impact speed and
    cache performance in practical implementations.
    There is a more complicated version which uses an
    in-place partition algorithm.

19
In-Place Quick Sort
  • Sorting in place
  • Instead of transferring elements out of a
    sequence and then back in, we just re-arrange
    them.
  • Uses a constant amount of memory
  • Efficient space usage
  • Algorithm inPlaceQuickSort
  • Runs efficiently when the sequence is implemented
    with an array

20
In-Place Quick Sort Visualization
21
In-Place Quick Sort Visualization
Pivot p element at the right bound index l
leftBound index r rightBound 1
r
l
p
while l lt r
22
In-Place Quick Sort Visualization
Pivot p element at the right bound index l
leftBound index r rightBound 1
r
l
p
while l lt r
r
l
p
r
l
p
23
In-Place Quick Sort Visualization
Pivot p element at the right bound index l
leftBound index r rightBound 1
r
l
p
while l lt r
l while its value lt p AND l lt r r-- while
its value gt p AND r gt l
r
l
p
swap l and r (as long as l lt r)
r
l
p
l while its value lt p AND l lt r r-- while
its value gt p AND r gt l
r
l
p
when l gt r swap l and p
r
l
p
sort(left leftBound, right l 1) sort(left
l 1, right rightBound)
r
l
p
r
l
p
24
Randomized Quick Sort
  • Variation of the quick sort algorithm
  • Instead of selecting the last element as the
    pivot, select an element at random
  • Expected runtime efficiency will always be
    O(nlogn)
Write a Comment
User Comments (0)
About PowerShow.com