Chapter # 6 Sorting Algorithms
Chapter # 6 Sorting Algorithms
Chapter # 6 Sorting Algorithms
Analysis of Algorithm
Chapter 6
Sorting Algorithms
(Advanced Sorting Algorithms)
1
6/11/2019
Sorting by Comparison
These are based on comparison of keys
The method has general applicability
Examples are selection sort, quick sort
Sorting by Counting
These depend on the characteristics of individual data items
The sort method has limited application
Examples include radix sort, bucket sort,
Analysis of Algorithm
Sorting by Comparison
The Sorting by Comparison method sorts input
By comparing pairs of keys in the input.
Elementary Algorithms
Are inefficient but easy to implement
Their running times are θ(n2)
Common algorithms are
Insertion sort, Exchange sort, Selection sort
Advanced Algorithms
Advanced algorithms are efficient
Implementations are usually based on recursive function calls
Their running times are θ(n lg n)
The typical advanced algorithms include
Merge sort, Quick sort, Heap sort
Analysis of Algorithm
2
6/11/2019
Sorting Algorithms
Algorithm Design Approach Sort in Place Complexity
Analysis of Algorithm
3
6/11/2019
Merge Sort
4
6/11/2019
Analysis of Algorithm
Merge Sort
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
p q r
MERGE-SORT(A, p, r)
if p < r ►Check for base case
then q ← (p + r)/2 ►Divide
MERGE-SORT(A, p, q) ►Conquer 1st half (Recursively sort)
MERGE-SORT(A, q + 1, r) ►Conquer 2nd half (Recursively sort)
MERGE(A, p, q, r) ►Combine the 2 halves
5
6/11/2019
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Merge-Sort(A, 1, 8)
MS(A, 5, 8)
MS(A, 5, 6)
MS(A, 5, 5) ►2
MS(A, 6, 6) ►6
M(A, 5, 5, 6) ► 2, 6
MS(A, 7, 8)
MS(A, 7, 7) ►1
MS(A, 8, 8) ►4
M(A, 7, 7, 8) ► 1, 4
M(A, 5, 6, 8) ► 1, 2, 4, 6
M(A, 1, 4, 8) ► 1, 2, 3, 4, 5, 6, 7, 8 ► Sorted
Analysis of Algorithm
6
6/11/2019
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Analysis of Algorithm
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Analysis of Algorithm
7
6/11/2019
p q r
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Merge Sort - Pseudocode
n1 n2
p q
L 2 4 5 7
q +1 r
R 1 2 3 6
Analysis of Algorithm
T(n)=2T(n/2)+θ(n)
Analysis of Algorithm
8
6/11/2019
Analysis of Algorithm
9
6/11/2019
Analysis of Algorithm
Analysis of Algorithm
10
6/11/2019
Merge Sort
Worst, Average and Best-Case Scenario
Worst case running time of merge sort is
θ(nlgn)
Best case ?
Analysis of Algorithm
Advantage
Guaranteed to run in (nlgn)
Disadvantage
Requires extra space N
Analysis of Algorithm
11
6/11/2019
Sort Challenge 1
Problem
Sort a file of huge records with tiny keys
Solution
Insertion sort or bubble sort?
NO, too many exchanges
Selection sort?
YES, it takes linear time for exchanges
Analysis of Algorithm
12
6/11/2019
Sort Challenge 2
Problem
Sort a huge randomly-ordered file of small records
Solution
Selection sort?
NO, always takes quadratic time
Bubble sort?
NO, quadratic time for randomly-ordered keys
Insertion d?
NO, quadratic time for randomly-ordered keys
Merge sort?
YES, it is designed for this problem
Analysis of Algorithm
13
6/11/2019
Sort Challenge 3
Problem
Sort a file that is already almost in order
Applications
Re-sort a huge database after a few changes
Double check that someone else sorted a file
Which sorting method to use?
A. Merge sort, guaranteed to run in time nlgn
B. Selection sort
C. Bubble sort
D. A custom algorithm for almost in-order files
E. Insertion sort
Analysis of Algorithm
Solution
Selection sort?
NO, always takes quadratic time
Bubble sort?
NO, bad for some definitions of “almost in order”
Example:
BCDEFGHIJKLMNOPQRSTUVWXYZA
Insertion sort?
YES, takes linear time for most definitions of “almost in
order”
Merge sort or custom method?
Probably not: insertion sort simpler and faster
Analysis of Algorithm
14
6/11/2019
Quick Sort
Quick Sort
Design Approach
Divide and Conquer
Procedure take place in two phases
Given array
Phase #1 (Divide)
An array element is chosen as Pivot
Analysis of Algorithm
15
6/11/2019
Phase #2 (Conquer)
The left and right sub-arrays are sorted recursively
Analysis of Algorithm
Analysis of Algorithm
16
6/11/2019
Analysis of Algorithm
Analysis of Algorithm
17
6/11/2019
Analysis of Algorithm
Analysis of Algorithm
18
6/11/2019
As shown
Next slide
Analysis of Algorithm
Analysis of Algorithm
19
6/11/2019
As shown
Next slide
Analysis of Algorithm
Analysis of Algorithm
20
6/11/2019
21
6/11/2019
As shown
Next slide
Analysis of Algorithm
Analysis of Algorithm
22
6/11/2019
Analysis of Algorithm
Analysis of Algorithm
23
6/11/2019
Analysis of Algorithm
Analysis of Algorithm
24
6/11/2019
Pivot Selection
Pivot influence performance of quick sort algorithm
There are four choices
Left-most element
The 1st element is chosen as pivot
There is chance of bad partitioning
Right-most element
The last element is chosen as pivot
There is chance of bad partitioning
Analysis of Algorithm
Pivot Selection
Medium element
The middle element is chosen as pivot
Random element
An element is selected randomly
The random selection reduces/ minimize the chances of bad
partitioning
Analysis of Algorithm
25
6/11/2019
Analysis of Algorithm
Heap Sort
26
6/11/2019
For MS
Heap sort
Count sort
Radix sort
Bucket sort
Analysis of Algorithm
27