Quicksort Presentation

36
Quick Sort - a recursive divide and conquer algorithm

Transcript of Quicksort Presentation

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:

• o8 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:

• oOur 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:

• oHowever 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