Chapter 4 - Sorting Algorithm
Chapter 4 - Sorting Algorithm
Th c
Goals
At the end of this chapter you should be able to:
Explain interchange sort and its analysis
Explain insertion sort and its analysis
Explain selection sort and its analysis
Explain bubble sort and its analysis
Explain heapsort and its analysis
Explain merge sort and its analysis
Explain quicksort and its analysis
Demo
Analysis and compare the algorithms
11/26/2014
Chapter Outline
Introduction to Sorting
Sorting Algorithms
Interchange sort
Insertion sort
Selection sort
Bubble sort
Heapsort
Merge sort
Quicksort
Lab
11/26/2014
Introduction to Sorting
The Sorting Problem?
Input:
A sequence of n numbers a1, a2, . . . , an
Output:
A permutation (reordering) a1, a2, . . . , an of the input
sequence such that a1 a2 an
11/26/2014
Introduction to Sorting
What is Sorting?
Sorting: an operation that segregates items into groups according
to specified criterion
EX:
A={3162134590}
A={0112334569}
11/26/2014
Introduction to Sorting
Why Study Sorting Algorithms?
There are a variety of situations that we can encounter
11/26/2014
Introduction to Sorting
Examples:
11/26/2014
Introduction to Sorting
Types of Sorting Algorithms
Interchange sort
Insertion sort
Selection sort
Bubble sort
Heap sort
Merge sort
Quick sort
11/26/2014
11/26/2014
11/26/2014
10
j
2
12
1
15
11/26/2014
11
j
3
12
2
15
11/26/2014
12
j
4
12
4
15
11/26/2014
13
j
5
12
5
15
11/26/2014
14
12
15
11/26/2014
15
11/26/2014
16
11/26/2014
17
11/26/2014
18
19
11/26/2014
20
12
15
11/26/2014
21
pos
2
12
2
15
x
11/26/2014
22
pos
3
12
8
15
x
11/26/2014
23
pos
4
58
12
15
x
11/26/2014
24
pos
5
2
1
12
15
x
11/26/2014
25
pos
6
8
6
12
15
x
11/26/2014
26
pos
7
5
4
12
8
15
x
11/26/2014
27
pos
8
12
15
x
11/26/2014
28
pos
5
11/26/2014
12
15
29
11/26/2014
30
11/26/2014
31
32
11/26/2014
33
11/26/2014
34
Find MinPos(1, 8)
min
1
12
15
11/26/2014
35
Find MinPos(2, 8)
1
min
2
12
15
11/26/2014
36
Find MinPos(3, 8)
1
min
3
12
15
11/26/2014
37
Find MinPos(4, 8)
1
min
4
12
15
11/26/2014
38
Find MinPos(5, 8)
1
min
5
12
15
11/26/2014
39
Swap(ai, amin)
7
15
min
6
12
11/26/2014
40
Find MinPos(7, 8)
1
min
7
12
8
15
11/26/2014
41
42
11/26/2014
43
11/26/2014
44
45
11/26/2014
46
j
8
12
1
15
11/26/2014
47
j
8
12
2
15
11/26/2014
48
j
8
12
4
15
11/26/2014
49
j
8
12
5
15
11/26/2014
50
j
8
12
6
15
11/26/2014
51
j
8
12
8
15
11/26/2014
52
j
8
12
15
11/26/2014
53
11/26/2014
54
O(n)
11/26/2014
55
O(n2)
Average-case:
O(n2)
56
57
25
15
11/26/2014
21
12
13
18
58
12 5
53
44
25
15
21
13
18
12
10 11
59
11/26/2014
60
12
15
11/26/2014
61
curr
4
12
joint
6
joint
8
15
5
left
11/26/2014
62
curr
2
12
joint
4
joint
5
joint
8
15
left
11/26/2014
63
curr
1
joint
2
joint
3
12
15
left
11/26/2014
64
15
12
11/26/2014
65
15
12
2
right
Swap(a1, aright)
11/26/2014
66
curr
1
joint
2
joint
3
12
15
right
Shift(a, 1, right)
11/26/2014
67
12
15
right
Swap(a1, aright)
11/26/2014
68
curr
1
joint
2
joint
3
joint
6
12
15
right
Shift(a, 1, right)
11/26/2014
69
12
15
right
Swap(a1, aright)
11/26/2014
70
12
15
12
15
11/26/2014
71
72
11/26/2014
73
11/26/2014
74
11/26/2014
75
11/26/2014
76
77
12
15
11/26/2014
78
12
15
11/26/2014
79
12
15
11/26/2014
80
12
15
11/26/2014
81
12
15
11/26/2014
82
12
15
11/26/2014
83
12
15
11/26/2014
84
12
15
11/26/2014
85
12
15
11/26/2014
86
12
15
11/26/2014
87
12
15
STOP
Only one subarray
11/26/2014
88
12
15
11/26/2014
89
90
91
11/26/2014
92
11/26/2014
93
k-1
......
k-1
......
2k-1
......
Best-case:
All the elements in the first array are smaller (or larger) than all the
elements in the second array.
The number of moves: 2k + 2k
The number of key comparisons: k
Worst-case:
The number of moves: 2k + 2k
The number of key comparisons: 2k-1
11/26/2014
94
2m-1
2m-1
20
11/26/2014
.................
20
level m
95
96
11/26/2014
97
Quicksort Idea
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.
11/26/2014
98
99
Quicksort Algorithms
Step 1: If left>=right: STOP
Step 2: Choose any element a[k] is pivot (l k r):
x = a [k]; i = l; j = r;
Step 3: Detect and correct pair of elements a[i], a[j] is the
wrong position:
o Step 3a: while (a [i] <x) i ++;
o Step 3b: while (a [j]> x) j--;
o Step 3c: If i <j Swap(a [i], a [j]);
100
Quicksort Example
Partition
X
i
1
j
8
12
15
right
left
11/26/2014
STOP
STOP
101
Quicksort Example
Partition
X
i
2
j
6
12
15
right
left
11/26/2014
STOP
STOP
102
Quicksort Example
j
3
i
5
12
15
right
left
11/26/2014
103
Quicksort Example
Partition
X
1
i
5
j
8
12
15
right
left
11/26/2014
STOP
STOP
104
Quicksort Example
j
5
6
left
11/26/2014
i
6
12
15
right
105
Quicksort Example
12
15
11/26/2014
106
107
Quicksort Analysis
Worst Case: (assume that we are selecting the mid element as
pivot)
The pivot divides the list of size n into two sublists of sizes 0 and n-1.
The number of key comparisons
= n-1 + n-2 + ... + 1
= n2/2 n/2
O(n2)
The number of swaps =
= n-1 + n-1 + n-2 + ... + 1
swaps outside of the for loop
= n2/2 + n/2 - 1
O(n2)
108
11/26/2014
109
Question
11/26/2014
110
111
11/26/2014
112
11/26/2014
113
Thank you !
11/26/2014
114