SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
Sunawar Khan
Islamia University of Bahawalpur
Bahawalnagar Campus
Oct 2, 2018
Analysis o f Algorithm
‫خان‬ ‫سنور‬ Algorithm Analysis
Partitioning and Sorting
Basic Idea of Quick Sort
Learning Goals
Analysis of Best and Average Case
Example
Contents
‫خان‬ ‫سنور‬ Algorithm Analysis
Learning Goals
•After Learning This
• Be able to explain how quicksort works and what its complexity is
(worst-case, best-case, average case).
• Be able to compare quicksort with heapsort and mergesort.
• Be able to explain the advantages and disadvantages of quicksort,
and its applications.
‫خان‬ ‫سنور‬ Algorithm Analysis
Introduction
• The basic version of quick sort algorithm was invented by C. A. R. Hoare in
1960 and formally introduced quick sort in 1962.
• It is used on the principle of divide-and-conquer.
• Quick sort is an algorithm of choice in many situations because it is not
difficult to implement, it is a good "general purpose" sort.
• It consumes relatively fewer resources during execution.
‫خان‬ ‫سنور‬ Algorithm Analysis
Quicksort by Hoare (1962)
• Select a pivot (partitioning element)
• Rearrange the list so that all the elements in the positions before the pivot are smaller
than or equal to the pivot and those after the pivot are larger than or equal to the pivot
• Exchange the pivot with the last element in the first (i.e., ≤) sublist – the pivot is now
in its final position
• Sort the two sublists recursively
• Apply quicksort to sort the list 7 2 9 10 5 4
p
A[i]≤p A[i]p
‫خان‬ ‫سنور‬ Algorithm Analysis
Features
• Similar to mergesort - divide-and-conquer recursive algorithm
• One of the fastest sorting algorithms
• Average running time O(NlogN)
• Worst-case running time O(N 2)
‫خان‬ ‫سنور‬ Algorithm Analysis
Good Points
• It is in-place since it uses only a small auxiliary stack.
• It requires only n log(n) time to sort n items.
• It has an extremely short inner loop
‫خان‬ ‫سنور‬ Algorithm Analysis
Bad Points
• It is recursive. Especially if recursion is not available, the implementation
is extremely complicated.
• It requires quadratic (i.e., n2) time in the worst-case.
• It is fragile i.e., a simple mistake in the implementation can go unnoticed
and cause it to perform badly.
‫خان‬ ‫سنور‬ Algorithm Analysis
Description of Quick Sort
• Quick sort works by partitioning a given array A[p . . r] into two non-
empty sub array A[p . . q] and A[q+1 . . r] such that every key in A[p . . q] is
less than or equal to every key in A[q+1 . . r].
• Then the two subarrays are sorted by recursive calls to Quick sort.
• The exact position of the partition depends on the given array and
index q is computed as a part of the partitioning procedure.
‫خان‬ ‫سنور‬ Algorithm Analysis
Quick Sort Design
• Follows the divide-and-conquer paradigm.
• Divide: Partition (separate) the array A[p..r] into two (possibly empty)
subarrays A[p..q–1] and A[q+1..r].
• Each element in A[p..q–1] < A[q].
• A[q] < each element in A[q+1..r].
• Index q is computed as part of the partitioning procedure.
• Conquer: Sort the two subarrays by recursive calls to quicksort.
• Combine: The subarrays are sorted in place – no work is needed to
combine them.
• How do the divide and combine steps of quicksort compare with those of
merge sort?
‫خان‬ ‫سنور‬ Algorithm Analysis
Quick Sort:
• Like mergesort, Quicksort is also based on the divide-and-conquer
paradigm.
• But it uses this technique in a somewhat opposite manner,
as all the hard work is done before the recursive calls.
• It works as follows:
1. First, it partitions an array into two parts,
2. Then, it sorts the parts independently,
3. Finally, it combines the sorted subsequences by
a simple concatenation.
‫خان‬ ‫سنور‬ Algorithm Analysis
Quick Sort (Cont.…)
The quick-sort algorithm consists of the following three steps:
1. Divide: Partition the list.
• To partition the list, we first choose some element from the list for which we hope
about half the elements will come before and half after. Call this element the pivot.
• Then we partition the elements so that all those with values less than the pivot
come in one sublist and all those with greater values come in another.
2. Recursion: Recursively sort the sublists separately.
3. Conquer: Put the sorted sublists together.
‫خان‬ ‫سنور‬ Algorithm Analysis
Quick Sort Procedure
If p < r then
q PARTITION (A, p, r)
Recursive call to QUICKSORT(A, p, q)
Recursive call to QUICKSORT(A, q + r, r)
‫خان‬ ‫سنور‬ Algorithm Analysis
Partition – Choosing the pivot
• First, we have to select a pivot element among the elements of the given
array, and we put this pivot into the first location of the array before
partitioning.
• Which array item should be selected as pivot?
• Somehow we have to select a pivot, and we hope that we will get a good
partitioning.
• If the items in the array arranged randomly, we choose a pivot randomly.
• We can choose the first or last element as a pivot (it may not give a good
partitioning).
• We can use different techniques to select the pivot.
‫خان‬ ‫سنور‬ Algorithm Analysis
Partitioning The Array
PARTITION (A, p, r)
x ← A[p]
i ← p-1
j ← r+1
while TRUE do
Repeat j ← j-1
until A[j] ≤ x
Repeat i ← i+1
until A[i] ≥ x
if i < j
then exchange A[i] ↔ A[j]
else return j
5
A[p..r]
A[p..q – 1] A[q+1..r]
 5  5
Partition
5
Partitioning procedure rearranges the subarrays in-place.
‫خان‬ ‫سنور‬ Algorithm Analysis
How to Place
• Partition selects the first key, A[p] as a pivot key about which the array
will partitioned:
• Keys ≤ A[p] will be moved towards the left .
• Keys ≥ A[p] will be moved towards the right.
• The running time of the partition procedure is Θ(n) where n = r – p + 1
which is the number of keys in the array.
‫خان‬ ‫سنور‬ Algorithm Analysis
Example
p r
initially: 2 5 8 3 9 4 1 7 10 6 note: pivot (x) = 6
i j
next iteration: 2 5 8 3 9 4 1 7 10 6
i j
next iteration: 2 5 8 3 9 4 1 7 10 6
i j
next iteration: 2 5 8 3 9 4 1 7 10 6
i j
next iteration: 2 5 3 8 9 4 1 7 10 6
i j
Partition(A, p, r)
x, i := A[r], p – 1;
for j := p to r – 1 do
if A[j]  x then
i := i + 1;
A[i]  A[j]
A[i + 1]  A[r];
return i + 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Example (Continued)
next iteration: 2 5 3 8 9 4 1 7 10 6
i j
next iteration: 2 5 3 8 9 4 1 7 10 6
i j
next iteration: 2 5 3 4 9 8 1 7 10 6
i j
next iteration: 2 5 3 4 1 8 9 7 10 6
i j
next iteration: 2 5 3 4 1 8 9 7 10 6
i j
next iteration: 2 5 3 4 1 8 9 7 10 6
i j
after final swap: 2 5 3 4 1 6 9 7 10 8
i j
Partition(A, p, r)
x, i := A[r], p – 1;
for j := p to r – 1 do
if A[j]  x then
i := i + 1;
A[i]  A[j]
A[i + 1]  A[r];
return i + 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Partitioning
• Select the last element A[r] in the subarray A[p..r] as the pivot – the
element around which to partition.
• As the procedure executes, the array is partitioned into four (possibly
empty) regions.
1. A[p..i ] — All entries in this region are < pivot.
2. A[i+1..j – 1] — All entries in this region are > pivot.
3. A[r] = pivot.
4. A[j..r – 1] — Not known how they compare to pivot.
• The above hold before each iteration of the for loop, and constitute a
loop invariant. (4 is not part of the loopi.)
‫خان‬ ‫سنور‬ Algorithm Analysis
Correctness of Partition
• Use loop invariant.
• Initialization:
• Before first iteration
• A[p..i] and A[i+1..j – 1] are empty – Conds. 1 and 2 are satisfied
(trivially).
• r is the index of the pivot
• Cond. 3 is satisfied.
• Maintenance:
• Case 1: A[j] > x
• Increment j only.
• Loop Invariant is maintained.
Partition(A, p, r)
x, i := A[r], p – 1;
for j := p to r – 1 do
if A[j]  x then
i := i + 1;
A[i]  A[j]
A[i + 1]  A[r];
return i + 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Correctness of Partition
>x x
p i j r
 x > x
x
p i j r
 x > x
Case 1:
‫خان‬ ‫سنور‬ Algorithm Analysis
Correctness of Partition
• Case 2: A[j]  x
• Increment i
• Swap A[i] and A[j]
• Condition 1 is maintained.
• Increment j
• Condition 2 is maintained.
• A[r] is unaltered.
• Condition 3 is maintained.
 x x
p i j r
 x > x
 x > x
x
p i j r
‫خان‬ ‫سنور‬ Algorithm Analysis
Correctness of Partition
• Termination:
• When the loop terminates, j = r, so all elements in A are partitioned into one of the
three cases:
• A[p..i]  pivot
• A[i+1..j – 1] > pivot
• A[r] = pivot
• The last two lines swap A[i+1] and A[r].
• Pivot moves from the end of the array to between the two subarrays.
• Thus, procedure partition correctly performs the divide step.
‫خان‬ ‫سنور‬ Algorithm Analysis
Complexity of Partition
• PartitionTime(n) is given by the number of iterations in the for loop.
• (n) : n = r – p + 1.
Partition(A, p, r)
x, i := A[r], p – 1;
for j := p to r – 1 do
if A[j]  x then
i := i + 1;
A[i]  A[j]
A[i + 1]  A[r];
return i + 1
‫خان‬ ‫سنور‬ Algorithm Analysis
Quicksort Overview
To sort a[left...right]:
1. if left < right:
1.1. Partition a[left...right] such that:
all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate
‫خان‬ ‫سنور‬ Algorithm Analysis
Partitioning in 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:
p
numbers less
than p
numbers greater than or
equal to p
p
‫خان‬ ‫سنور‬ Algorithm Analysis
Alternative 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
‫خان‬ ‫سنور‬ Algorithm Analysis
Alternative Partitioning
To partition a[left...right]:
1. Set pivot = a[left], l = left + 1, r = right;
2. while l < r, do
2.1. while l < right & a[l] < pivot , set l = l + 1
2.2. while r > left & a[r] >= pivot , set r = r - 1
2.3. if l < r, swap a[l] and a[r]
3. Set a[left] = a[r], a[r] = pivot
4. Terminate
‫خان‬ ‫سنور‬ Algorithm Analysis
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
• swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
‫خان‬ ‫سنور‬ Algorithm Analysis
Analysis of quicksort—best case
• Suppose each partition operation divides the array almost
exactly in half
• Then the depth of the recursion in log2n
• Because that’s how many times we can halve n
• We note that
• Each partition is linear over its subarray
• All the partitions at one level cover the array
‫خان‬ ‫سنور‬ Algorithm Analysis
Partitioning at various levels
‫خان‬ ‫سنور‬ Algorithm Analysis
Best Case Analysis
• The best thing that could happen in Quick sort would be that each
partitioning stage divides the array exactly in half. In other words, the best
to be a median of the keys in A[p . . r] every time procedure 'Partition' is
called. The procedure 'Partition' always split the array to be sorted into
two equal sized arrays.
• If the procedure 'Partition' produces two regions of size n/2. the
recurrence relation is then
T(n) = T(n/2) + T(n/2) + Θ(n)
= 2T(n/2) + Θ(n)
• From case 2 of Master theorem
T(n) = Θ(n lg n)
‫خان‬ ‫سنور‬ Algorithm Analysis
Best Case Analysis
• 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 best case, quicksort has time complexity O(n log2n)
• What about the worst case?
‫خان‬ ‫سنور‬ Algorithm Analysis
Worst Case Partitioning
• The worst-case occurs if given array A[1 . . n] is already sorted. The
PARTITION (A, p, r) call always return p so successive calls to partition
will split arrays of length n, n-1, n-2, . . . , 2 and running time proportional
to n + (n-1) + (n-2) + . . . + 2 = [(n+2)(n-1)]/2 = Θ (n2). The worst-case
also occurs if A[1 . . n] starts out in reverse order.
‫خان‬ ‫سنور‬ Algorithm Analysis
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 requires (in the worst case) recurring to
depth n-1
‫خان‬ ‫سنور‬ Algorithm Analysis
Worst case partitioning
‫خان‬ ‫سنور‬ Algorithm Analysis
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?
• There are many arrangements that could make this happen
• Here are two common cases:
• When the array is already sorted
• When the array is inversely sorted (sorted in the opposite order)
‫خان‬ ‫سنور‬ Algorithm Analysis
Typical case for quicksort
• If the array is sorted to begin with, Quicksort is terrible: O(n2)
• It is possible to construct other bad cases
• However, Quicksort is usually O(n log2n)
• The constants are so good that Quicksort is generally the faster
algorithm.
• Most real-world sorting is done by Quicksort
‫خان‬ ‫سنور‬ Algorithm Analysis
Picking a better pivot
• Before, we picked the first element of the subarray to use as a pivot
• If the array is already sorted, this results in O(n2) behavior
• It’s no better if we pick the last element
• We could do an optimal quicksort (guaranteed O(n log n)) if we always
picked a pivot value that exactly cuts the array in half
• Such a value is called a median: half of the values in the array are larger, half are
smaller
• The easiest way to find the median is to sort the array and pick the value in the
middle (!)
‫خان‬ ‫سنور‬ Algorithm Analysis
Median of three
• Obviously, it doesn’t make sense to sort the array in order to find the
median to use as a pivot.
• Instead, compare just three elements of our (sub)array—the first, the last,
and the middle
• Take the median (middle value) of these three as the pivot
• It’s possible (but not easy) to construct cases which will make this technique O(n2)
‫خان‬ ‫سنور‬ Algorithm Analysis
Quicksort for Small Arrays
• For very small arrays (N<= 20), quicksort does not perform as well as
insertion sort
• A good cutoff range is N=10
• Switching to insertion sort for small arrays can save about 15% in the
running time
‫خان‬ ‫سنور‬ Algorithm Analysis
Mergesort vs Quicksort
• Both run in O(n lgn)
• Mergesort – always.
• Quicksort – on average
• Compared with Quicksort, Mergesort has less number of comparisons but
larger number of moving elements
• In Java, an element comparison is expensive but moving elements is cheap.
Therefore, Mergesort is used in the standard Java library for generic
sorting
‫خان‬ ‫سنور‬ Algorithm Analysis
Mergesort vs Quicksort
In C++, copying objects can be expensive while comparing objects often is
relatively cheap. Therefore, quicksort is the sorting routine commonly
used in C++ libraries
Note these last two rules are not really language specific, but rather how the
language is typically used.
‫خان‬ ‫سنور‬ Algorithm Analysis
Summary of Quicksort
• Best case: split in the middle — Θ( n log n)
• Worst case: sorted array! — Θ( n2)
• Average case: random arrays — Θ( n log n)
• Considered as the method of choice for internal sorting for large files (n ≥
10000)
• Improvements:
• better pivot selection: median of three partitioning avoids worst case in sorted files
• switch to insertion sort on small subfiles
• elimination of recursion
these combine to 20-25% improvement
‫خان‬ ‫سنور‬ Algorithm Analysis
Conclusion
•Quick sort is an in place sorting algorithm whose
worst-case running time is Θ(n2) and expected
running time is Θ(n lg n) where constants hidden in
Θ(n lg n) are small.
‫خان‬ ‫سنور‬ Algorithm AnalysisQuick-Sort 46
Summary of Sorting Algorithms
Algorithm Time Notes
selection-sort O(n2)
in-place
slow (good for small inputs)
insertion-sort O(n2)
in-place
slow (good for small inputs)
quick-sort
O(n log n)
expected
in-place, randomized
fastest (good for large inputs)
heap-sort O(n log n)
in-place
fast (good for large inputs)
merge-sort O(n log n)
sequential data access
fast (good for huge inputs)
‫خان‬ ‫سنور‬ Algorithm Analysis
Exam Like Question
• Briefly describe the basic idea of quicksort.
• What is the complexity of quicksort?
• Analyze the worst-case complexity solving the recurrence relation.
• Analyze the best-case complexity solving the recurrence relation.
• Compare quicksort with mergesort and heapsort.
• What are the advantages and disadvantages of quicksort?
• Which applications are not suitable for quicksort and why?
‫خان‬ ‫سنور‬ Algorithm Analysis
Characteristics
• Advantage
• One of the fastest algorithms on average.
• Does not need additional memory (the sorting takes place in the array -
this is called in-place processing). Compare with mergesort: mergesort
needs additional memory for merging.
• Disadvantage
• The worst-case complexity is O(N2)
‫خان‬ ‫سنور‬ Algorithm Analysis
Applications
• Commercial applications use Quicksort - generally it runs fast, no
additional memory,
this compensates for the rare occasions when it runs with O(N2)
• Never use in applications which require guaranteed response time:
• Life-critical (medical monitoring, life support in aircraft and space craft)
• Mission-critical (monitoring and control in industrial and research plants
handling dangerous materials, control for aircraft, defense, etc)
• unless you assume the worst-case response time.
‫خان‬ ‫سنور‬ Algorithm Analysis
Comparison
• Comparison with heapsort:
• Both algorithms have O(NlogN) complexity
• Quicksort runs faster, (does not support a heap tree)
• The speed of quick sort is not guaranteed
• Comparison with mergesort:
• Mergesort guarantees O(NlogN) time, however it requires additional memory with
size N.
• Quicksort does not require additional memory, however the speed is not
quaranteed
• Usually mergesort is not used for main memory sorting, only for external memory
sorting.
• So far, our best sorting algorithm has O(nlog n) performance: can we do any better?
• In general, the answer is no.
‫خان‬ ‫سنور‬ Algorithm Analysis
Acknowledgment
• Introduction to Algorithms. T.H. Cormen, C.E. Leiserson and R.L. Rivest.
2001. 2001.

Más contenido relacionado

La actualidad más candente

Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and boundAbhishek Singh
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentationirdginfo
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 

La actualidad más candente (20)

Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Time complexity
Time complexityTime complexity
Time complexity
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Parsing
ParsingParsing
Parsing
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 

Similar a Quick sort

Similar a Quick sort (20)

quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
3.8 quicksort
3.8 quicksort3.8 quicksort
3.8 quicksort
 
Linear timesorting
Linear timesortingLinear timesorting
Linear timesorting
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Sorting
SortingSorting
Sorting
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Quicksort
QuicksortQuicksort
Quicksort
 

Más de International Islamic University (20)

Hash tables
Hash tablesHash tables
Hash tables
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graph 1
Graph 1Graph 1
Graph 1
 
Graph 2
Graph 2Graph 2
Graph 2
 
Graph 3
Graph 3Graph 3
Graph 3
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Facial Expression Recognitino
Facial Expression RecognitinoFacial Expression Recognitino
Facial Expression Recognitino
 
Lecture#4
Lecture#4Lecture#4
Lecture#4
 
Lecture#3
Lecture#3 Lecture#3
Lecture#3
 
Lecture#2
Lecture#2 Lecture#2
Lecture#2
 
Case study
Case studyCase study
Case study
 
Arrays
ArraysArrays
Arrays
 
Pcb
PcbPcb
Pcb
 
Data transmission
Data transmissionData transmission
Data transmission
 
Basic organization of computer
Basic organization of computerBasic organization of computer
Basic organization of computer
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Fundamentals of-algorithm
Fundamentals of-algorithmFundamentals of-algorithm
Fundamentals of-algorithm
 
Fundamentals of-algorithm
Fundamentals of-algorithmFundamentals of-algorithm
Fundamentals of-algorithm
 

Último

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Quick sort

  • 1. Sunawar Khan Islamia University of Bahawalpur Bahawalnagar Campus Oct 2, 2018 Analysis o f Algorithm
  • 2. ‫خان‬ ‫سنور‬ Algorithm Analysis Partitioning and Sorting Basic Idea of Quick Sort Learning Goals Analysis of Best and Average Case Example Contents
  • 3. ‫خان‬ ‫سنور‬ Algorithm Analysis Learning Goals •After Learning This • Be able to explain how quicksort works and what its complexity is (worst-case, best-case, average case). • Be able to compare quicksort with heapsort and mergesort. • Be able to explain the advantages and disadvantages of quicksort, and its applications.
  • 4. ‫خان‬ ‫سنور‬ Algorithm Analysis Introduction • The basic version of quick sort algorithm was invented by C. A. R. Hoare in 1960 and formally introduced quick sort in 1962. • It is used on the principle of divide-and-conquer. • Quick sort is an algorithm of choice in many situations because it is not difficult to implement, it is a good "general purpose" sort. • It consumes relatively fewer resources during execution.
  • 5. ‫خان‬ ‫سنور‬ Algorithm Analysis Quicksort by Hoare (1962) • Select a pivot (partitioning element) • Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than or equal to the pivot • Exchange the pivot with the last element in the first (i.e., ≤) sublist – the pivot is now in its final position • Sort the two sublists recursively • Apply quicksort to sort the list 7 2 9 10 5 4 p A[i]≤p A[i]p
  • 6. ‫خان‬ ‫سنور‬ Algorithm Analysis Features • Similar to mergesort - divide-and-conquer recursive algorithm • One of the fastest sorting algorithms • Average running time O(NlogN) • Worst-case running time O(N 2)
  • 7. ‫خان‬ ‫سنور‬ Algorithm Analysis Good Points • It is in-place since it uses only a small auxiliary stack. • It requires only n log(n) time to sort n items. • It has an extremely short inner loop
  • 8. ‫خان‬ ‫سنور‬ Algorithm Analysis Bad Points • It is recursive. Especially if recursion is not available, the implementation is extremely complicated. • It requires quadratic (i.e., n2) time in the worst-case. • It is fragile i.e., a simple mistake in the implementation can go unnoticed and cause it to perform badly.
  • 9. ‫خان‬ ‫سنور‬ Algorithm Analysis Description of Quick Sort • Quick sort works by partitioning a given array A[p . . r] into two non- empty sub array A[p . . q] and A[q+1 . . r] such that every key in A[p . . q] is less than or equal to every key in A[q+1 . . r]. • Then the two subarrays are sorted by recursive calls to Quick sort. • The exact position of the partition depends on the given array and index q is computed as a part of the partitioning procedure.
  • 10. ‫خان‬ ‫سنور‬ Algorithm Analysis Quick Sort Design • Follows the divide-and-conquer paradigm. • Divide: Partition (separate) the array A[p..r] into two (possibly empty) subarrays A[p..q–1] and A[q+1..r]. • Each element in A[p..q–1] < A[q]. • A[q] < each element in A[q+1..r]. • Index q is computed as part of the partitioning procedure. • Conquer: Sort the two subarrays by recursive calls to quicksort. • Combine: The subarrays are sorted in place – no work is needed to combine them. • How do the divide and combine steps of quicksort compare with those of merge sort?
  • 11. ‫خان‬ ‫سنور‬ Algorithm Analysis Quick Sort: • Like mergesort, Quicksort is also based on the divide-and-conquer paradigm. • But it uses this technique in a somewhat opposite manner, as all the hard work is done before the recursive calls. • It works as follows: 1. First, it partitions an array into two parts, 2. Then, it sorts the parts independently, 3. Finally, it combines the sorted subsequences by a simple concatenation.
  • 12. ‫خان‬ ‫سنور‬ Algorithm Analysis Quick Sort (Cont.…) The quick-sort algorithm consists of the following three steps: 1. Divide: Partition the list. • To partition the list, we first choose some element from the list for which we hope about half the elements will come before and half after. Call this element the pivot. • Then we partition the elements so that all those with values less than the pivot come in one sublist and all those with greater values come in another. 2. Recursion: Recursively sort the sublists separately. 3. Conquer: Put the sorted sublists together.
  • 13. ‫خان‬ ‫سنور‬ Algorithm Analysis Quick Sort Procedure If p < r then q PARTITION (A, p, r) Recursive call to QUICKSORT(A, p, q) Recursive call to QUICKSORT(A, q + r, r)
  • 14. ‫خان‬ ‫سنور‬ Algorithm Analysis Partition – Choosing the pivot • First, we have to select a pivot element among the elements of the given array, and we put this pivot into the first location of the array before partitioning. • Which array item should be selected as pivot? • Somehow we have to select a pivot, and we hope that we will get a good partitioning. • If the items in the array arranged randomly, we choose a pivot randomly. • We can choose the first or last element as a pivot (it may not give a good partitioning). • We can use different techniques to select the pivot.
  • 15. ‫خان‬ ‫سنور‬ Algorithm Analysis Partitioning The Array PARTITION (A, p, r) x ← A[p] i ← p-1 j ← r+1 while TRUE do Repeat j ← j-1 until A[j] ≤ x Repeat i ← i+1 until A[i] ≥ x if i < j then exchange A[i] ↔ A[j] else return j 5 A[p..r] A[p..q – 1] A[q+1..r]  5  5 Partition 5 Partitioning procedure rearranges the subarrays in-place.
  • 16. ‫خان‬ ‫سنور‬ Algorithm Analysis How to Place • Partition selects the first key, A[p] as a pivot key about which the array will partitioned: • Keys ≤ A[p] will be moved towards the left . • Keys ≥ A[p] will be moved towards the right. • The running time of the partition procedure is Θ(n) where n = r – p + 1 which is the number of keys in the array.
  • 17. ‫خان‬ ‫سنور‬ Algorithm Analysis Example p r initially: 2 5 8 3 9 4 1 7 10 6 note: pivot (x) = 6 i j next iteration: 2 5 8 3 9 4 1 7 10 6 i j next iteration: 2 5 8 3 9 4 1 7 10 6 i j next iteration: 2 5 8 3 9 4 1 7 10 6 i j next iteration: 2 5 3 8 9 4 1 7 10 6 i j Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] A[i + 1]  A[r]; return i + 1
  • 18. ‫خان‬ ‫سنور‬ Algorithm Analysis Example (Continued) next iteration: 2 5 3 8 9 4 1 7 10 6 i j next iteration: 2 5 3 8 9 4 1 7 10 6 i j next iteration: 2 5 3 4 9 8 1 7 10 6 i j next iteration: 2 5 3 4 1 8 9 7 10 6 i j next iteration: 2 5 3 4 1 8 9 7 10 6 i j next iteration: 2 5 3 4 1 8 9 7 10 6 i j after final swap: 2 5 3 4 1 6 9 7 10 8 i j Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] A[i + 1]  A[r]; return i + 1
  • 19. ‫خان‬ ‫سنور‬ Algorithm Analysis Partitioning • Select the last element A[r] in the subarray A[p..r] as the pivot – the element around which to partition. • As the procedure executes, the array is partitioned into four (possibly empty) regions. 1. A[p..i ] — All entries in this region are < pivot. 2. A[i+1..j – 1] — All entries in this region are > pivot. 3. A[r] = pivot. 4. A[j..r – 1] — Not known how they compare to pivot. • The above hold before each iteration of the for loop, and constitute a loop invariant. (4 is not part of the loopi.)
  • 20. ‫خان‬ ‫سنور‬ Algorithm Analysis Correctness of Partition • Use loop invariant. • Initialization: • Before first iteration • A[p..i] and A[i+1..j – 1] are empty – Conds. 1 and 2 are satisfied (trivially). • r is the index of the pivot • Cond. 3 is satisfied. • Maintenance: • Case 1: A[j] > x • Increment j only. • Loop Invariant is maintained. Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] A[i + 1]  A[r]; return i + 1
  • 21. ‫خان‬ ‫سنور‬ Algorithm Analysis Correctness of Partition >x x p i j r  x > x x p i j r  x > x Case 1:
  • 22. ‫خان‬ ‫سنور‬ Algorithm Analysis Correctness of Partition • Case 2: A[j]  x • Increment i • Swap A[i] and A[j] • Condition 1 is maintained. • Increment j • Condition 2 is maintained. • A[r] is unaltered. • Condition 3 is maintained.  x x p i j r  x > x  x > x x p i j r
  • 23. ‫خان‬ ‫سنور‬ Algorithm Analysis Correctness of Partition • Termination: • When the loop terminates, j = r, so all elements in A are partitioned into one of the three cases: • A[p..i]  pivot • A[i+1..j – 1] > pivot • A[r] = pivot • The last two lines swap A[i+1] and A[r]. • Pivot moves from the end of the array to between the two subarrays. • Thus, procedure partition correctly performs the divide step.
  • 24. ‫خان‬ ‫سنور‬ Algorithm Analysis Complexity of Partition • PartitionTime(n) is given by the number of iterations in the for loop. • (n) : n = r – p + 1. Partition(A, p, r) x, i := A[r], p – 1; for j := p to r – 1 do if A[j]  x then i := i + 1; A[i]  A[j] A[i + 1]  A[r]; return i + 1
  • 25. ‫خان‬ ‫سنور‬ Algorithm Analysis Quicksort Overview To sort a[left...right]: 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and all a[p+1...right] are >= a[p] 1.2. Quicksort a[left...p-1] 1.3. Quicksort a[p+1...right] 2. Terminate
  • 26. ‫خان‬ ‫سنور‬ Algorithm Analysis Partitioning in 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: p numbers less than p numbers greater than or equal to p p
  • 27. ‫خان‬ ‫سنور‬ Algorithm Analysis Alternative 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
  • 28. ‫خان‬ ‫سنور‬ Algorithm Analysis Alternative Partitioning To partition a[left...right]: 1. Set pivot = a[left], l = left + 1, r = right; 2. while l < r, do 2.1. while l < right & a[l] < pivot , set l = l + 1 2.2. while r > left & a[r] >= pivot , set r = r - 1 2.3. if l < r, swap a[l] and a[r] 3. Set a[left] = a[r], a[r] = pivot 4. Terminate
  • 29. ‫خان‬ ‫سنور‬ Algorithm Analysis 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 • swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
  • 30. ‫خان‬ ‫سنور‬ Algorithm Analysis Analysis of quicksort—best case • Suppose each partition operation divides the array almost exactly in half • Then the depth of the recursion in log2n • Because that’s how many times we can halve n • We note that • Each partition is linear over its subarray • All the partitions at one level cover the array
  • 31. ‫خان‬ ‫سنور‬ Algorithm Analysis Partitioning at various levels
  • 32. ‫خان‬ ‫سنور‬ Algorithm Analysis Best Case Analysis • The best thing that could happen in Quick sort would be that each partitioning stage divides the array exactly in half. In other words, the best to be a median of the keys in A[p . . r] every time procedure 'Partition' is called. The procedure 'Partition' always split the array to be sorted into two equal sized arrays. • If the procedure 'Partition' produces two regions of size n/2. the recurrence relation is then T(n) = T(n/2) + T(n/2) + Θ(n) = 2T(n/2) + Θ(n) • From case 2 of Master theorem T(n) = Θ(n lg n)
  • 33. ‫خان‬ ‫سنور‬ Algorithm Analysis Best Case Analysis • 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 best case, quicksort has time complexity O(n log2n) • What about the worst case?
  • 34. ‫خان‬ ‫سنور‬ Algorithm Analysis Worst Case Partitioning • The worst-case occurs if given array A[1 . . n] is already sorted. The PARTITION (A, p, r) call always return p so successive calls to partition will split arrays of length n, n-1, n-2, . . . , 2 and running time proportional to n + (n-1) + (n-2) + . . . + 2 = [(n+2)(n-1)]/2 = Θ (n2). The worst-case also occurs if A[1 . . n] starts out in reverse order.
  • 35. ‫خان‬ ‫سنور‬ Algorithm Analysis 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 requires (in the worst case) recurring to depth n-1
  • 36. ‫خان‬ ‫سنور‬ Algorithm Analysis Worst case partitioning
  • 37. ‫خان‬ ‫سنور‬ Algorithm Analysis 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? • There are many arrangements that could make this happen • Here are two common cases: • When the array is already sorted • When the array is inversely sorted (sorted in the opposite order)
  • 38. ‫خان‬ ‫سنور‬ Algorithm Analysis Typical case for quicksort • If the array is sorted to begin with, Quicksort is terrible: O(n2) • It is possible to construct other bad cases • However, Quicksort is usually O(n log2n) • The constants are so good that Quicksort is generally the faster algorithm. • Most real-world sorting is done by Quicksort
  • 39. ‫خان‬ ‫سنور‬ Algorithm Analysis Picking a better pivot • Before, we picked the first element of the subarray to use as a pivot • If the array is already sorted, this results in O(n2) behavior • It’s no better if we pick the last element • We could do an optimal quicksort (guaranteed O(n log n)) if we always picked a pivot value that exactly cuts the array in half • Such a value is called a median: half of the values in the array are larger, half are smaller • The easiest way to find the median is to sort the array and pick the value in the middle (!)
  • 40. ‫خان‬ ‫سنور‬ Algorithm Analysis Median of three • Obviously, it doesn’t make sense to sort the array in order to find the median to use as a pivot. • Instead, compare just three elements of our (sub)array—the first, the last, and the middle • Take the median (middle value) of these three as the pivot • It’s possible (but not easy) to construct cases which will make this technique O(n2)
  • 41. ‫خان‬ ‫سنور‬ Algorithm Analysis Quicksort for Small Arrays • For very small arrays (N<= 20), quicksort does not perform as well as insertion sort • A good cutoff range is N=10 • Switching to insertion sort for small arrays can save about 15% in the running time
  • 42. ‫خان‬ ‫سنور‬ Algorithm Analysis Mergesort vs Quicksort • Both run in O(n lgn) • Mergesort – always. • Quicksort – on average • Compared with Quicksort, Mergesort has less number of comparisons but larger number of moving elements • In Java, an element comparison is expensive but moving elements is cheap. Therefore, Mergesort is used in the standard Java library for generic sorting
  • 43. ‫خان‬ ‫سنور‬ Algorithm Analysis Mergesort vs Quicksort In C++, copying objects can be expensive while comparing objects often is relatively cheap. Therefore, quicksort is the sorting routine commonly used in C++ libraries Note these last two rules are not really language specific, but rather how the language is typically used.
  • 44. ‫خان‬ ‫سنور‬ Algorithm Analysis Summary of Quicksort • Best case: split in the middle — Θ( n log n) • Worst case: sorted array! — Θ( n2) • Average case: random arrays — Θ( n log n) • Considered as the method of choice for internal sorting for large files (n ≥ 10000) • Improvements: • better pivot selection: median of three partitioning avoids worst case in sorted files • switch to insertion sort on small subfiles • elimination of recursion these combine to 20-25% improvement
  • 45. ‫خان‬ ‫سنور‬ Algorithm Analysis Conclusion •Quick sort is an in place sorting algorithm whose worst-case running time is Θ(n2) and expected running time is Θ(n lg n) where constants hidden in Θ(n lg n) are small.
  • 46. ‫خان‬ ‫سنور‬ Algorithm AnalysisQuick-Sort 46 Summary of Sorting Algorithms Algorithm Time Notes selection-sort O(n2) in-place slow (good for small inputs) insertion-sort O(n2) in-place slow (good for small inputs) quick-sort O(n log n) expected in-place, randomized fastest (good for large inputs) heap-sort O(n log n) in-place fast (good for large inputs) merge-sort O(n log n) sequential data access fast (good for huge inputs)
  • 47. ‫خان‬ ‫سنور‬ Algorithm Analysis Exam Like Question • Briefly describe the basic idea of quicksort. • What is the complexity of quicksort? • Analyze the worst-case complexity solving the recurrence relation. • Analyze the best-case complexity solving the recurrence relation. • Compare quicksort with mergesort and heapsort. • What are the advantages and disadvantages of quicksort? • Which applications are not suitable for quicksort and why?
  • 48. ‫خان‬ ‫سنور‬ Algorithm Analysis Characteristics • Advantage • One of the fastest algorithms on average. • Does not need additional memory (the sorting takes place in the array - this is called in-place processing). Compare with mergesort: mergesort needs additional memory for merging. • Disadvantage • The worst-case complexity is O(N2)
  • 49. ‫خان‬ ‫سنور‬ Algorithm Analysis Applications • Commercial applications use Quicksort - generally it runs fast, no additional memory, this compensates for the rare occasions when it runs with O(N2) • Never use in applications which require guaranteed response time: • Life-critical (medical monitoring, life support in aircraft and space craft) • Mission-critical (monitoring and control in industrial and research plants handling dangerous materials, control for aircraft, defense, etc) • unless you assume the worst-case response time.
  • 50. ‫خان‬ ‫سنور‬ Algorithm Analysis Comparison • Comparison with heapsort: • Both algorithms have O(NlogN) complexity • Quicksort runs faster, (does not support a heap tree) • The speed of quick sort is not guaranteed • Comparison with mergesort: • Mergesort guarantees O(NlogN) time, however it requires additional memory with size N. • Quicksort does not require additional memory, however the speed is not quaranteed • Usually mergesort is not used for main memory sorting, only for external memory sorting. • So far, our best sorting algorithm has O(nlog n) performance: can we do any better? • In general, the answer is no.
  • 51. ‫خان‬ ‫سنور‬ Algorithm Analysis Acknowledgment • Introduction to Algorithms. T.H. Cormen, C.E. Leiserson and R.L. Rivest. 2001. 2001.