Quick Sort
- a recursive divide and conquer algorithm
• In most cases Quick Sort is the best
comparison sorting algorithm
• Widely used as the sort funtion of
many programming languages
• It’s efficiency on average is O(n log n)
The O(n log n) Sweet
Spot
• Doesn’t require more memory (in place)
Why is it the best?
Quick Sort
• Pick a Pivot - (random or median)
• Partition elements into 2 sections:
› Elements < to Pivot (left)
› Elements > Pivot (right)
• Recursively Quick Sort the two halves
down until sorted - (down to 1 element)
STEPS
Quick Sort
• Properly Picking the Pivot - random or median
The Key to Quick Sort’s Efficiency
• Quick Sort works best on UNSORTED lists or arrays
Best Case Worst Case
Pick Pivot Randomly Pick Pivot by Median
UNSORTED SORTED/SEMI-SORTED
O(n log n) O(n^2)
AVERAGE RARE/AVOIDABLE
8 7613 3 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
When researching Quicksort you’ll find a few different
popular methods used but they’re all basically the same
idea. Here we’re going to look at one of the most
popular, Hoare’s Quicksort, which puts the Pivot on one
end and works down the list or array.
8 7613 3 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o All 4 of these bullet points that you see here are the
basic rules, or checks, that we’ll be using to sort. This is
the basic logic defined by the underlying code.
Left Right
#’s < P (7) #’s >/= P (7)
8 7613 3 4 9 P (Pivot) = 7
Left Right
#’s < P (7) #’s >/= P (7)
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o So first we pick our Pivot (P), in this example at random.
We’ve decided to go with the last element in the
sequence, 7, and we’ll do this with each recursion going
down as needed until the sorting is complete.
8 7613 3 4 9 P (Pivot) = 7
Left Right
#’s < P (7) #’s >/= P (7)
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o This is our Partition divider, every element on the left will
be less than our pivot and everything on the right will be
greater ( or equal to ) our pivot.
8 7613 3 4 9 P (Pivot) = 7
Left Right
#’s < P (7) #’s >/= P (7)
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
So we start from the left and compare each element to
our pivot. Again we just check if its less than or greater
and we move our partition divider and swap elements
accordingly. Here 13 is greater than our pivot 7 so it is in
the correct position, to the right of our divider.
8 7613 3 4 9 P (Pivot) = 7
Left Right
#’s < P (7) #’s >/= P (7)
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 3 is less than or pivot so we swap it with the first known
element greater than our pivot, here the 13. And move
our partition divider over one as well so that the lesser 3
is in the correct position.
8 7613
3
4 9 P (Pivot) = 7
Left Right
#’s < P (7) #’s >/= P (7)
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 3 is less than or pivot so we swap it with the first known
element greater than our pivot, here the 13. And move
our partition divider over one as well so that the lesser 3
is in the correct position.
8 7613
3
4 9 P (Pivot) = 7
Left Right
#’s < P (7) #’s >/= P (7)
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 3 is less than or pivot so we swap it with the first known
element greater than our pivot, here the 13. And move
our partition divider over one as well so that the lesser 3
is in the correct position.
8 76133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 3 is less than or pivot so we swap it with the first known
element greater than our pivot, here the 13. And move
our partition divider over one as well so that the lesser 3
is in the correct position.
Left Right
#’s < P (7) #’s >/= P (7)
8 76133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
8 is greater than our pivot so its in the right place.
Left Right
#’s < P (7) #’s >/= P (7)
8 76133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
4 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 13.
8 76133
4
9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 4 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 13.
Left Right
#’s < P (7) #’s >/= P (7)
8 76133
4
9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 4 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 13.
Left Right
#’s < P (7) #’s >/= P (7)
8 76133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 4 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 13.
Left Right
#’s < P (7) #’s >/= P (7)
8 76133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 4 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 13.
Left Right
#’s < P (7) #’s >/= P (7)
8 76133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 6 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 8.
Left Right
#’s < P (7) #’s >/= P (7)
8 7
6
133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 6 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 8.
Left Right
#’s < P (7) #’s >/= P (7)
8 7
6
133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 6 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 8.
Left Right
#’s < P (7) #’s >/= P (7)
8 76 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 6 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 8.
Left Right
#’s < P (7) #’s >/= P (7)
8 76 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o 6 will obviously require another swap and shift. Again,
with the first known element greater than our pivot,
here the 8.
Left Right
#’s < P (7) #’s >/= P (7)
8 76 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
9 is greater than our pivot so we move on.
Now at the end we swap our pivot with the first element
greater than it, if there is one. In this case there is and its
13 so we swap to complete the first pass through.
8 76 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
Now at the end we swap our pivot with the first element
greater than it, if there is one. In this case there is and its
13 so we swap to complete the first pass through.
8
7
6 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
Now at the end we swap our pivot with the first element
greater than it, if there is one. In this case there is and its
13 so we swap to complete the first pass through.
8
7
6 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
Now at the end we swap our pivot with the first element
greater than it, if there is one. In this case there is and its
13 so we swap to complete the first pass through.
876 133 4 9 P (Pivot) = 7
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Left Right
#’s < P (7) #’s >/= P (7)
876 133 4 9
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o We now have two partitions with one with all the
elements less than our first pivot and another with all
those greater.
Left Right
#’s < P (7) #’s >/= P (7)
876 133 4 9
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o Now the same operation will happen to each new
segement. This happens until you get down to one
element.
Left Right
#’s < P (7) #’s >/= P (7)
876 133 4 9
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
Our pivots get picked accordingly and our partition
dividers are set.
Left Right
#’s < P (13) #’s >/= P (13)
Left Right
#’s < P (6) #’s >/= P (6)
876 133 4 9
L < P </= R
• If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put
• Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P
• Finish by also Swapping the Pivot with the 1st Number >/=
(put the = sign on same side as your pivot)
Hoare’s Quicksort Example:
• o
However you’ll notice that both our remaining
segements just happened to already be sorted. So
though the code will work all the way through and down
until there is only one element left, no numbers will
change order from here on so this ends this example.
Left Right
#’s < P (13) #’s >/= P (13)
Left Right
#’s < P (6) #’s >/= P (6)
Just remember this cycle happens over and over same
way until the list or array is completly sorted. It happens
to each new segement that is created. A pivot is picked
and the elements are divided into those less than on the
left and those greater than on the right.
until
< >
Here is a good example of Quicksort online that I refenced for this example, it also
has a snippet of the corresponding code for this example -
http://www.csanimated.com/animation.php?t=Quicksort

Quicksort Presentation

  • 1.
    Quick Sort - arecursive divide and conquer algorithm
  • 2.
    • In mostcases Quick Sort is the best comparison sorting algorithm • Widely used as the sort funtion of many programming languages
  • 3.
    • It’s efficiencyon average is O(n log n) The O(n log n) Sweet Spot • Doesn’t require more memory (in place) Why is it the best?
  • 4.
    Quick Sort • Picka Pivot - (random or median) • Partition elements into 2 sections: › Elements < to Pivot (left) › Elements > Pivot (right) • Recursively Quick Sort the two halves down until sorted - (down to 1 element) STEPS
  • 5.
    Quick Sort • ProperlyPicking the Pivot - random or median The Key to Quick Sort’s Efficiency • Quick Sort works best on UNSORTED lists or arrays Best Case Worst Case Pick Pivot Randomly Pick Pivot by Median UNSORTED SORTED/SEMI-SORTED O(n log n) O(n^2) AVERAGE RARE/AVOIDABLE
  • 6.
    8 7613 34 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7) When researching Quicksort you’ll find a few different popular methods used but they’re all basically the same idea. Here we’re going to look at one of the most popular, Hoare’s Quicksort, which puts the Pivot on one end and works down the list or array.
  • 7.
    8 7613 34 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o All 4 of these bullet points that you see here are the basic rules, or checks, that we’ll be using to sort. This is the basic logic defined by the underlying code. Left Right #’s < P (7) #’s >/= P (7)
  • 8.
    8 7613 34 9 P (Pivot) = 7 Left Right #’s < P (7) #’s >/= P (7) L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o So first we pick our Pivot (P), in this example at random. We’ve decided to go with the last element in the sequence, 7, and we’ll do this with each recursion going down as needed until the sorting is complete.
  • 9.
    8 7613 34 9 P (Pivot) = 7 Left Right #’s < P (7) #’s >/= P (7) L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o This is our Partition divider, every element on the left will be less than our pivot and everything on the right will be greater ( or equal to ) our pivot.
  • 10.
    8 7613 34 9 P (Pivot) = 7 Left Right #’s < P (7) #’s >/= P (7) L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o So we start from the left and compare each element to our pivot. Again we just check if its less than or greater and we move our partition divider and swap elements accordingly. Here 13 is greater than our pivot 7 so it is in the correct position, to the right of our divider.
  • 11.
    8 7613 34 9 P (Pivot) = 7 Left Right #’s < P (7) #’s >/= P (7) L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 3 is less than or pivot so we swap it with the first known element greater than our pivot, here the 13. And move our partition divider over one as well so that the lesser 3 is in the correct position.
  • 12.
    8 7613 3 4 9P (Pivot) = 7 Left Right #’s < P (7) #’s >/= P (7) L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 3 is less than or pivot so we swap it with the first known element greater than our pivot, here the 13. And move our partition divider over one as well so that the lesser 3 is in the correct position.
  • 13.
    8 7613 3 4 9P (Pivot) = 7 Left Right #’s < P (7) #’s >/= P (7) L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 3 is less than or pivot so we swap it with the first known element greater than our pivot, here the 13. And move our partition divider over one as well so that the lesser 3 is in the correct position.
  • 14.
    8 76133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 3 is less than or pivot so we swap it with the first known element greater than our pivot, here the 13. And move our partition divider over one as well so that the lesser 3 is in the correct position. Left Right #’s < P (7) #’s >/= P (7)
  • 15.
    8 76133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 8 is greater than our pivot so its in the right place. Left Right #’s < P (7) #’s >/= P (7)
  • 16.
    8 76133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7) 4 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 13.
  • 17.
    8 76133 4 9 P(Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 4 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 13. Left Right #’s < P (7) #’s >/= P (7)
  • 18.
    8 76133 4 9 P(Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 4 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 13. Left Right #’s < P (7) #’s >/= P (7)
  • 19.
    8 76133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 4 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 13. Left Right #’s < P (7) #’s >/= P (7)
  • 20.
    8 76133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 4 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 13. Left Right #’s < P (7) #’s >/= P (7)
  • 21.
    8 76133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 6 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 8. Left Right #’s < P (7) #’s >/= P (7)
  • 22.
    8 7 6 133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 6 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 8. Left Right #’s < P (7) #’s >/= P (7)
  • 23.
    8 7 6 133 49 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 6 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 8. Left Right #’s < P (7) #’s >/= P (7)
  • 24.
    8 76 1334 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 6 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 8. Left Right #’s < P (7) #’s >/= P (7)
  • 25.
    8 76 1334 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o 6 will obviously require another swap and shift. Again, with the first known element greater than our pivot, here the 8. Left Right #’s < P (7) #’s >/= P (7)
  • 26.
    8 76 1334 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7) 9 is greater than our pivot so we move on.
  • 27.
    Now at theend we swap our pivot with the first element greater than it, if there is one. In this case there is and its 13 so we swap to complete the first pass through. 8 76 133 4 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7)
  • 28.
    Now at theend we swap our pivot with the first element greater than it, if there is one. In this case there is and its 13 so we swap to complete the first pass through. 8 7 6 133 4 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7)
  • 29.
    Now at theend we swap our pivot with the first element greater than it, if there is one. In this case there is and its 13 so we swap to complete the first pass through. 8 7 6 133 4 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7)
  • 30.
    Now at theend we swap our pivot with the first element greater than it, if there is one. In this case there is and its 13 so we swap to complete the first pass through. 876 133 4 9 P (Pivot) = 7 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Left Right #’s < P (7) #’s >/= P (7)
  • 31.
    876 133 49 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o We now have two partitions with one with all the elements less than our first pivot and another with all those greater. Left Right #’s < P (7) #’s >/= P (7)
  • 32.
    876 133 49 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Now the same operation will happen to each new segement. This happens until you get down to one element. Left Right #’s < P (7) #’s >/= P (7)
  • 33.
    876 133 49 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o Our pivots get picked accordingly and our partition dividers are set. Left Right #’s < P (13) #’s >/= P (13) Left Right #’s < P (6) #’s >/= P (6)
  • 34.
    876 133 49 L < P </= R • If the Number (#) is Greater Than/ Equal to (>/=) the Pivot, it Stays Put • Else, if its Less Than (<) the Pivot, Swap it with 1st Element >/= than P • Finish by also Swapping the Pivot with the 1st Number >/= (put the = sign on same side as your pivot) Hoare’s Quicksort Example: • o However you’ll notice that both our remaining segements just happened to already be sorted. So though the code will work all the way through and down until there is only one element left, no numbers will change order from here on so this ends this example. Left Right #’s < P (13) #’s >/= P (13) Left Right #’s < P (6) #’s >/= P (6)
  • 35.
    Just remember thiscycle happens over and over same way until the list or array is completly sorted. It happens to each new segement that is created. A pivot is picked and the elements are divided into those less than on the left and those greater than on the right. until < >
  • 36.
    Here is agood example of Quicksort online that I refenced for this example, it also has a snippet of the corresponding code for this example - http://www.csanimated.com/animation.php?t=Quicksort