Chapter9: Sorting: CSD201 - Data Structures and Algorithms (In C++)
Chapter9: Sorting: CSD201 - Data Structures and Algorithms (In C++)
C++)
Chapter9: Sorting
http://www.fpt.edu.vn/ 1
Chapter topics
Sorting algorithms:
• Elementary
• Advanced
http://www.fpt.edu.vn/ 2
Insertion sort: algorithm
insertionsort(data[])
for i = 1 to data.length-1
tmp = data[i];
move all elements data[j] greater than tmp by one position;
place tmp in its proper position;
http://www.fpt.edu.vn/ 3
Insertion sort: example
1 3 4
2 2 2 2 2 1 1 1 1 1 1 1
5 5 5 5 2 2 2 2 2 2 2 2
1 1 5 5 5 5 3 3 3 3
1 5
3 3 3 3 5
3 3 3 5 5 5 4
4 4 4 4 4 4 5
4 4 4 4 5
http://www.fpt.edu.vn/ 4
Insertion sort: computational complexity
http://www.fpt.edu.vn/ 5
Selection sort: algorithm
selectionsort(data[])
for i = 0 to data.length-2
select the smallest elements among
data[i], …, data[data.length-1];
swap it with data[i];
http://www.fpt.edu.vn/ 6
Selection sort: example
2 2 1 1 1 1 1
5 5 5 2 2 2 2
1 1 2 5 3 3 3
3 3
3 3 5 4 4
4 4 4
4 4 5 5
http://www.fpt.edu.vn/ 7
Selection sort: computational complexity
http://www.fpt.edu.vn/ 8
Bubble sort: algorithm
bubblesort(data[])
for i = 0 to data.length-2
for j = data.length-1 downto i+1
if elements in positions j and j-1 are out of order
swap them;
http://www.fpt.edu.vn/ 9
Bubble sort: example
2 2 2 1 1 1 1 1
5 5 1 2 2 2 2 2
5
1 1 5 3 3 3 3
3 3
3 3 5 4 4 4
4 4 4
4 4 5 5 5
http://www.fpt.edu.vn/ 10
Bubble sort: computational complexity
http://www.fpt.edu.vn/ 11
Basic sorting algorithms: comparison
http://www.fpt.edu.vn/ 12
Divide-and-conquer sorting
DCsorting(data[])
quicksort partition data[] into data1[] and data2[];
DCsorting(data1[]);
DCsorting(data2[]);
mergesort merge data1[] and data2[] into data[];
http://www.fpt.edu.vn/ 13
Merging ordered arrays into one array
2 5 6 8 1 5 7 9
1 2 5 5 6 7 8 9 n elements
http://www.fpt.edu.vn/ 14
Mergesort: algorithm
http://www.fpt.edu.vn/ 15
Mergesort: example
2 4 9 5 5 7 3 6 2
2 4 9 5 5 7 3 6 2
2 4 9 5 5 7 3 6 2
2 4 9 5 5 7 3 6 2
2 4
2 4
2 4 9 5 5 3 7 2 6
2 4 5 5 9 2 3 6 7
2 2 3 4 5 5 6 7 9
Mergesort: computational complexity
0 if n = 1
C(n) =
2C(n/2) + n – 1 otherwise
C(n) = 2C(n/2) + n – 1
= 2(2C(n/4) + n/2 – 1) + n – 1 = 4C(n/4) + 2n – 3
= 4(2C(n/8) + n/4 – 1) + 2n – 3 = 8C(n/8) + 3n – 7
...
assume that n == 22C(n/2 ) + in – (2i – 1)
ii i
= in – 2i + 1
= nlgn – n + 1 = O(nlgn)
http://www.fpt.edu.vn/ 17
Quicksort: algorithm
quicksort(data[])
if data.length > 1
choose pivot;
while there are elements left in data
partitioning
http://www.fpt.edu.vn/ 18
Quicksort: implementation
private <T extends Comparable<? super T>>
void quicksort(T[] data, int first, int last) {
int lower = first + 1, upper = last;
swap(data,first,(first+last)/2);
T pivot = data[first];
while (lower <= upper) {
while (pivot.compareTo(data[lower]) > 0)
lower++;
partitioning
http://www.fpt.edu.vn/ 19
Quicksort: implementation, one pass
a p first step
p ≤p b c ? d e ≥p intermediate
step
b < p, e > p
p ≤p b c ? d e ≥p
b < p, e ≤ p
p ≤p b c ? d e ≥p
b ≥ p, e > p
p ≤p b c ? d e ≥p
b ≥ p, e ≤ p
p ≤p e c ? d b ≥p
p ≤p f ≥p last step
Quicksort: example
2 4 9 5 5 6 3 7 8 2 largest
pivot toto
the
the
first
lastcell
cell
3 2
2 2 partitioning
pivot to final cell 7 6 8
2 3 6 8
2 2 3 4 5 5 6 7 8 9
Quicksort: computational complexity
best case: partitioning into two worst case: one array after
even-size arrays partitioning is empty
Number of comparisons Number of comparisons:
(approximations):
n n–1
n/2 n/2 n–2
n/4 n/4 n/4 n/4 n–3
……………………………….. …………………………..
1 1 ………………. 1 1 1
http://www.fpt.edu.vn/ 22
Heap sort
heapsort(data[])
transform data into a heap;
for i = data.length-1 downto 2
swap the root with the element in position i;
restore the heap property for the tree data[0], …, data[i-1];
http://www.fpt.edu.vn/ 23
Heap sort: example, transforming data into a heap
4
6 3 3 6 4
7 5 4 7 5 4 7 5 3
4 6 3 7 5 4 4 6 3 7 5 4 4 6 4 7 5 3
last nonleaf
4 7 7
7 4 4 4 6 4
6 5 3 6 5 3 4 5 3
4 7 4 6 5 3 7 4 4 6 5 3 7 6 4 4 5 3
http://www.fpt.edu.vn/ 24
Heap sort: example, sorting
7 3 6 6
6 4 6 4 3 4 5 4
4 5 3 4 5 7 4 5 4 3
7 6 4 4 5 3 3 6 4 4 5 7 6 3 4 4 5 7 6 5 4 4 3 7
3 5 5 3
5 4 3 4 4 4 4 4
4 6 4 3 5
3 5 4 4 6 7 5 3 4 4 6 7 5 4 4 3 6 7 3 4 4 5 6 7
http://www.fpt.edu.vn/ 25
Heap sort: example, sorting (cont.)
3 4 4 4 3
4 4 3 4 3 4 3 4
5
3 4 4 5 6 7 4 3 4 5 6 7 4 3 4 5 6 7 4 3 4 5 6 7 3 4 4 5 6 7
http://www.fpt.edu.vn/ 26
Sorting methods: comparison of runtimes
http://www.fpt.edu.vn/ 27
Q&A
http://www.fpt.edu.vn/ 28