Beruflich Dokumente
Kultur Dokumente
Sorting
Bringing Order to the World
Lecture Outline
Selection Sort
Bubble Sort
Insertion Sort
Merge Sort
Quick Sort
Properties of Sorting
Applications of Sorting
Uniqueness testing
Deleting duplicates
Prioritizing events
Frequency counting
Set intersection/union
Efficient searching
Selection Sort
10
14
37
13
29
10
14
13
37
x Unsorted items
13
10
14
29
37
13
10
14
29
37
10
13
14
29
37
x current iteration
x Sorted items
Sorted!
We can also find the smallest and put it the front instead
http://visualgo.net/sorting?create=29,10,14,37,13&mode=Selection
[ CS1020E AY1617S1 Lecture 10 ]
Step 1:
Search for
maximum
element
Step 2:
Swap
maximum
element
with the last
item i
Bubble Sort
Analogy
11
x
x
[ CS1020E AY1617S1 Lecture 10 ]
Sorted Item
Pair of items
under comparison
12
29
10
14
37
Step 1:
Compare
adjacent
pairs of
numbers
Step 2:
Swap if the
items are out
of order
13
http://visualgo.net/sorting?create=29,10,14,37,13&mode=Bubble
[ CS1020E AY1617S1 Lecture 10 ]
13
14
11
25
39
Idea
15
If the flag
remains true
after the inner
loop sorted!
16
Worst-case
Best-case
17
Insertion Sort
10
2nd card: 5
5
10
3rd card: K
10
19
Iteration 1
40
13
13
40
20
20
Sorted
Unsorted
Unsorted
To be inserted
Iteration 2
13
20
40
Iteration 3
13
20
40
http://visualgo.net/sorting?create=40,13,20,8&mode=Insertion
[ CS1020E AY1617S1 Lecture 10 ]
20
next is the
item to be
inserted
a[j+1] = next;
}
}
Insert next to
the correct
location
29
10
14
37
13
http://visualgo.net/sorting?create=29,10,14,37,13&mode=Insertion
[ CS1020E AY1617S1 Lecture 10 ]
21
22
Merge Sort
Question
24
Divide-and-Conquer Method
Divide step
Conquer step
25
Divide step
Conquer step
26
Divide into
two halves
Recursively
sort the
halves
Merge them
Question
27
Note
Function to merge
a[lowmid] and
a[mid+1high] into
a[lowhigh]
28
38 16 27 39 12 27
38 16 27
38 16
38
39 12 27
27
16
16 38
39 12
39
27
12
12 39
Divide Phase
Recursive call to
mergeSort()
Conquer Phase
Merge steps
16 27 38
12 27 39
12 16 27 27 38 39
http://visualgo.net/sorting?create=38,16,27,39,12,27&mode=Merge
[ CS1020E AY1617S1 Lecture 10 ]
29
a[0..2]
a[3..5]
2 4 5
3 7 8
2 4 5
3 7 8
2 4 5
3 7 8
2 3
2 4 5
3 7 8
2 3 4
24 5
3 7 8
2 3 4 5
2 4 5
3 7 8
2 3 4 5 7 8
Merged result in a
temporary array
Unmerged
items
Merged items
30
Normal Merging
Where both
halves have
unmerged items
31
Merged result
are copied
back into a[]
Remaining
items are
copied into
b[]
delete [] b;
}
Remember to free
allocated memory
Question
32
33
Level 1:
mergeSort n/2 items
Level 2:
mergeSort n/22 items
n/2
n/22
n/22
n/22
. . .
Level 2:
22 calls to mergeSort
n/22
Level 1:
2 calls to mergeSort
n/2
Level 0:
1 call to mergeSort
n/(2k) = 1 n = 2k k = lg n
[ CS1020E AY1617S1 Lecture 10 ]
34
Level (lg n): 2lg(n) 1(= n/2) calls to merge() with n/2lg(n) (= 1)
item in each half, O(n) time
35
Pros
Cons
36
Quick Sort
Divide step
Conquer step
Do nothing!
38
27 38 12 39 27 19
16
Pivot
12 16
27
39 27 38
Pivot
Recursively sort
the two parts
12 16 27 27 38 39
39
40
<p
m
S1
p
k
S2
Unknown
41
p
i
?
k
j
Unknown
42
If a[k]=y p,
S2
x
<p
p
i
S1
Increment k
S2
x
<p
m
?
k
43
If a[k]=y < p
p
p
p
<p
<p
p
i
<p
k
x
i
Increment k
i
Swap x and y
<p
i
Increment m
S2
y
m
y
m
?
k
j
44
p is the pivot
S1 and S2 empty
initially
45
http://visualgo.net/sorting?create=27,38,12,39,27,16&mode=Quick
[ CS1020E AY1617S1 Lecture 10 ]
46
n = high low
Complexity is O(n)
47
S2 = a[m+1...j]
S1 = a[i+1...m]
empty when m = i
48
n-1
1
Total no.
of levels
=n
n-2
50
It is known that
51
Radix Sort
In each iteration
53
54
55
Question
56
Basic Idea
57
58
Properties of Sorting
In-Place Sorting
Questions
60
Stable Sorting
61
Non-Stable Sort
Selection Sort
1285 5a 4746 602 5b (8356)
1285 5a 5b 602 (4746 8356)
602 5a 5b (1285 4746 8356)
5b 5a (602 1285 4746 8356)
Quick Sort
62
Best
Case
In-place?
Stable?
Selection
Sort
O(n2)
O(n2)
Yes
No
Insertion
Sort
O(n2)
O(n)
Yes
Yes
Bubble Sort
O(n2)
O(n2)
Yes
Yes
Bubble Sort 2
O(n2)
O(n)
Yes
Yes
Merge Sort
O(n lg n)
O(n lg n)
No
Yes
Quick Sort
O(n2)
O(n lg n)
Yes
No
Radix sort
O(dn)
O(dn)
No
yes
63
Summary
Iterative Sorting
Selection Sort
Bubble Sort
Insertion Sort
Recursive Sorting
Merge Sort
Quick Sort
Radix Sort
In-Place
Stable
64