0% found this document useful (0 votes)
52 views18 pages

Quick Sort

Quicksort is a sorting algorithm that selects a pivot element and partitions the array into elements less than and greater than the pivot, recursively sorting the subarrays. Its average time complexity is O(n log n), but in the worst case, such as when the array is already sorted, it can degrade to O(n^2). The choice of pivot is crucial for performance, with techniques like the 'median of three' improving efficiency.

Uploaded by

iamnityasinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views18 pages

Quick Sort

Quicksort is a sorting algorithm that selects a pivot element and partitions the array into elements less than and greater than the pivot, recursively sorting the subarrays. Its average time complexity is O(n log n), but in the worst case, such as when the array is already sorted, it can degrade to O(n^2). The choice of pivot is crucial for performance, with techniques like the 'median of three' improving efficiency.

Uploaded by

iamnityasinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

QUICKSORT

BASIC IDEA
Pick one element in the array, which
will be the pivot.
Make one pass through the array,
called a partition step, re-arranging
the entries so that:
• entries smaller than the pivot are to
the left of the pivot.
• entries larger than the pivot are to
the right
BASIC IDEA
Recursively apply quicksort to the part
of the array that is to the left of the
pivot, and to the part on its right.

No merge step (like merge sort), at the


end all the elements are in the proper
order
TWO KEY STEPS

How to pick a pivot?

How to partition?
PARTITIONING (QUICKSORT )

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:

numbers p numbers greater than


less than p or equal to p 5
EXAMPLE OF PARTITIONING

choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
search: 436924312189356
swap: 433924312189656
search: 433924312189656
swap: 433124312989656
search: 433124312989656
swap: 433122314989656
search: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (L > R)
swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
6
ANALYSIS OF QUICKSORT—BEST
CASE

Suppose each partition operation


divides the array almost exactly in two
half.

Then the depth of the recursion in


log2n (like merge sort)
⚫ Because that’s how many times we can
halve n

7
PARTITIONING AT VARIOUS LEVELS

8
BEST CASE II
We cut the array size in half each time
So the depth of the recursion in log2n
At each level of the recursion, all the
partitions at that level do work that is
linear in n
O(log2n) * O(n) = O(n log2n)
Hence in the average case, quicksort has
time complexity O(n log2n)
What about the worst case?
9
ANALYSIS OF QUICKSORT—WORST
CASE

In the worst case, partitioning always


divides the size n array into these three
parts:
⚫ A length one part, containing the pivot
itself
⚫ A length zero part, and
⚫ A length n-1 part, containing
everything else
We don’t recur on the zero-length part
Recurring on the length n-1 part requires10
(in the worst case) recurring to depth n-1
WORST CASE PARTITIONING

11
WORST CASE FOR QUICKSORT
In the worst case, recursion may be n levels
deep (for an array of size n)
But the partitioning work done at each level
is still n
O(n) * O(n) = O(n2)
So worst case for Quicksort is O(n2)
When does this happen?
⚫ When the array is sorted to begin with!

12
TYPICAL CASE FOR QUICKSORT

If the array is sorted to begin with,


Quicksort is terrible: O(n2)
However, Quicksort is usually O(n log2n)
The constants are so good that Quicksort
is generally the fastest algorithm known
Most real-world sorting is done by
Quicksort

13
CHOOSING THE PIVOT

Some fixed element: e.g. the first, the last,


the one in the middle.
Bad choice –
If the array is already sorted, this
results in O(n2) behavior.
It’s no better if we pick the last element.

Randomly chosen (by random generator)


- still a bad choice
CHOOSING THE PIVOT

The median of the array


(if the array has N numbers, the median is
the [N/2] largest number).

This is difficult to compute - increases the


complexity.
CHOOSING THE PIVOT

The median-of-three choice:


take the first, the last and the middle
element.

Choose the median of these three


elements.
FINAL COMMENTS
Quicksort is the fastest known sorting
algorithm
For optimum efficiency, the pivot
must be chosen carefully
“Median of three” is a good technique
for choosing the pivot
However, no matter what you do,
there will be some cases where
Quicksort runs in O(n2) time
17
COMPARISON WITH MERGE
SORT

Merge sort guarantees O(NlogN) time


Merge sort requires additional
memory with size N
Usually Merge sort is not used for main
memory sorting, only for external
memory sorting

You might also like