Quicksort - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Quicksort

Description:

invented by British computer scientist C.A.R. Hoare in 1960. more specifically: choose one element in the list to be the pivot (= partition element) ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 25
Provided by: dermotshin
Category:
Tags: hoare | quicksort

less

Transcript and Presenter's Notes

Title: Quicksort


1
Quicksort
  • quick sort orders a list of values by
    partitioning the list around one element called a
    pivot, then sorting each partition
  • invented by British computer scientist C.A.R.
    Hoare in 1960
  • more specifically
  • choose one element in the list to be the pivot (
    partition element)
  • organize the elements so that all elements less
    than the pivot are to its left and all greater
    are to its right
  • apply the quick sort algorithm (recursively) to
    both partitions
  • Divide and conquer strategy

2
Choosing a pivot
  • first element
  • bad if input is sorted or in reverse sorted order
  • bad if input is nearly sorted
  • variation particular element (e.g. middle
    element)
  • random element
  • median of three elements
  • choose the median of the left, right, and center
    elements

3
Quicksort
4
Quicksort
L
R
Mid (LR)/2
5
Quicksort
Median of Three if(ArrL gt ArrMid)
Swap(Arr,L,Mid)
L
R
Mid (LR)/2
6
Quicksort
Median of Three if(ArrMid gt ArrR)
Swap(Arr,Mid,R)
L
R
Mid (LR)/2
7
Quicksort
Median of Three if(ArrMid gt ArrR)
Swap(Arr,Mid,R) // Largest of three in R
L
R
Mid (LR)/2
8
Quicksort
Median of Three if(ArrMid gt ArrL)
Swap(Arr,Mid,L)
L
R
Mid (LR)/2
9
Quicksort
Median of Three if(ArrMid gt ArrL)
Swap(Arr,Mid,L) // Median of three in L //
Smallest of three in Mid
L
R
Mid (LR)/2
10
Quicksort
Partition - Smallest to left of Median, Largest
to right // initialise i L 1 // ignore
median j R 1 // know R is bigger so ignore
i
j
11
Quicksort
Partition - Smallest to left of Median, Largest
to right // find item bigger than
median while(Arri lt ArrL) i // find item
bigger than median while(Arrj gt ArrL) j--
i
j
L
12
Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
i
j
L
13
Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
i
j
L
14
Quicksort
Partition - Smallest to left of Median, Largest
to right // find item bigger than
median while(Arri lt ArrL) i // find item
bigger than median while(Arrj gt ArrL) j--
i
j
L
15
Quicksort
Partition - Smallest to left of Median, Largest
to right // find item bigger than
median while(Arri lt ArrL) i // find item
bigger than median while(Arrj gt ArrL) j--
i
j
L
16
Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
i
j
L
17
Quicksort
Partition - Smallest to left of Median, Largest
to right // if we havent finished swap these
two if(i lt j) swap(Arr,i,j) i // move on
to next j-- // move back to previous
j
i
L
18
Quicksort
Partition - Smallest to left of Median, Largest
to right // Put Median in FINAL
position swap(Arr,L,j)
j
i
L
19
Quicksort
Partition - Smallest to left of Median, Largest
to right // Put Median in FINAL
position swap(Arr,L,j)
Right Partition
Left Partition
20
Special cases
  • What happens when the array contains many
    duplicate elements?
  • What happens when the array is already sorted (or
    nearly sorted) to begin with?
  • Small arrays
  • Quicksort is slower than insertion sort when is N
    is small (say, N ? 20).
  • Optimization Make A ? 20 the base case and use
    insertion sort algorithm on arrays of that size
    or smaller.

21
MedianOfThree Algorithm
  • public static void MedianOfThree(int List, int
    Left, int Right)
  • int Mid (LeftRight) / 2
  • if(ListLeft gt ListMid)
  • swap(List, Left, Mid)
  • if(ListMid gt ListRight)
  • swap(List, Mid, Right)
  • if(ListLeft lt ListMid)
  • swap(List, Left, Mid)
  • swap(List, Left1, Mid)

22
Quicksort Algorithm
  • private static void swap(int List, int i, int
    j)
  • int temp Listi
  • Listi Listj
  • Listj temp

23
Partition Algorithm
  • private static int partition(int List, int
    Left, int Right)
  • MedianOfThree(List, Left, Right)
  • int i Left 1
  • int j Right 1
  • while (i lt j)
  • while (Listi lt ListLeft) i //
    find big item on way up
  • while (Listj gt ListLeft) j-- // find
    small item on way down if (i lt j)
    // If haven't crossed then
    swap
  • swap(List, i, j)
  • swap(List, Left, j) // swap with partition
    element
  • return j

24
Quicksort Algorithm
  • public static void quicksort(int List, int
    Left, int Right)
  • int ListSize Right - Left 1
  • if (ListSize gt QuickSortLowBoundary)
  • int p partition(List, Left, Right)
  • quicksort(List, Left, p-1)
  • quicksort(List, p1, Right)
  • else if(ListSize gt 1)
  • SelectionSort(List, Left, Right)
Write a Comment
User Comments (0)
About PowerShow.com