Quick Sort - PowerPoint PPT Presentation

About This Presentation
Title:

Quick Sort

Description:

Developed in 1962 by C. A. R. Hoare. Recursive. Divide-and-conquer. The fastest algorithm known ... Quicksort rearranges an array into two parts so that all the ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 13
Provided by: quaziabid
Category:
Tags: hoare | quick | sort

less

Transcript and Presenter's Notes

Title: Quick Sort


1
Quick Sort
  • csc326 Information Structures
  • Spring 2009

2
Quicksort
  • Developed in 1962 by C. A. R. Hoare
  • Recursive
  • Divide-and-conquer
  • The fastest algorithm known
  • Average running time O(N log N)
  • Worst-case running time O(N2) but very unlikely
    to happen

3
Quicksort
  • Quicksort rearranges an array into two parts so
    that all the elements in the left subarray are
    less than or equal to a specified value, called
    the pivot
  • Quicksort ensures that the elements in the right
    subarray are larger than the pivot
  • Advantage
  • No extra memory is needed
  • Fast running time (in average)
  • Disadvantage
  • Unstable in running time
  • Finding pivot element is a big issue!

4
Quicksort Algorithm
  • To sort aleft...right
  • 1. if left lt right
  • 1.1. Partition aleft...right such that
  • all aleft...p-1 are less than ap, and
  • all ap1...right are gt ap
  • 1.2. Quicksort aleft...p-1
  • 1.3. Quicksort ap1...right
  • 2. Terminate

5
Partitioning
  • A key step in the Quicksort algorithm is
    partitioning the array
  • We choose some (any) number p in the array to use
    as a pivot
  • We partition the array into three parts

6
Partitioning
  • Choose an array value (say, the first) to use as
    the pivot
  • Starting from the left end, find the first
    element that is greater than or equal to the
    pivot
  • Searching backward from the right end, find the
    first element that is less than the pivot
  • Interchange (swap) these two elements
  • Repeat, searching from where we left off, until
    done

7
Partitioning
  • To partition aleft...right
  • 1. Set pivot aleft, l left 1, r right
  • 2. while l lt r, do
  • 2.1. while l lt right al lt pivot , set l l
    1
  • 2.2. while r gt left ar gt pivot , set r r -
    1
  • 2.3. if l lt r, swap al and ar
  • 3. Set aleft ar, ar pivot
  • 4. Terminate

8
Example of Partitioning
  • choose pivot 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
  • search 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
  • swap 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6
  • search 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6
  • swap 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6
  • search 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6
  • swap 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6
  • search 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (left gt
    right)
  • swap with pivot 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6

9
Selecting the Pivot
  • First element - a bad choice
  • In general, don't select the pivot near the
    beginning or the end of the set
  • Middle element is a good choice
  • Median-of-three - the median of the first,
    middle, and last elements

10
(No Transcript)
11
Quicksort partition
int partition(int a, int left, int right)
int p aleft, l left 1, r right
while (l lt r) while (l lt right
al lt p) l while (r gt left ar gt
p) r-- if (l lt r) int
temp al al ar ar
temp aleft ar ar
p return r
12
Quicksort (cont.)
  • void quicksort(int array, int left,int right)
  • if (left lt right)
  • int p partition(array, left, right)
    quicksort(array, left, p - 1)
    quicksort(array, p 1, right)
Write a Comment
User Comments (0)
About PowerShow.com