0% found this document useful (0 votes)
33 views

Chapter9: Sorting: CSD201 - Data Structures and Algorithms (In C++)

This document discusses sorting algorithms including insertion sort, selection sort, bubble sort, merge sort, and quicksort. It provides pseudocode for the algorithms and analyzes their computational complexity. Insertion sort, selection sort, and bubble sort have O(n^2) time complexity in the average and worst cases. Merge sort uses a divide and conquer approach and has O(nlogn) time complexity. Quicksort also uses divide and conquer and one pass partitioning with O(nlogn) average time complexity.

Uploaded by

Adeyeye Gbenga
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Chapter9: Sorting: CSD201 - Data Structures and Algorithms (In C++)

This document discusses sorting algorithms including insertion sort, selection sort, bubble sort, merge sort, and quicksort. It provides pseudocode for the algorithms and analyzes their computational complexity. Insertion sort, selection sort, and bubble sort have O(n^2) time complexity in the average and worst cases. Merge sort uses a divide and conquer approach and has O(nlogn) time complexity. Quicksort also uses divide and conquer and one pass partitioning with O(nlogn) average time complexity.

Uploaded by

Adeyeye Gbenga
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CSD201 – Data Structures and Algorithms (In

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

best case avg case worst case


(ordered) (random) (reversed)
n2 + n – 2 n(n – 1)
comparisons n–1
4 2
n2 + 5n – 6 n(n–1)
movements 2(n – 1) + 2(n–1)
4 2

best case avg case n(n – 1)worst case


1 + 2 + … + (n – 2) + (n – 1) =
2
comparisons O(n) O(n )
2
O(n2)
movements O(n) O(n2) O(n2)

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

best case avg case worst case


(largest first, the rest
(ordered) (random) is ordered)

n(n – 1) n(n – 1) n(n – 1)


comparisons
2 2 2

movements 0 c(n–1) 3(n–1)

best case avg case worst case


comparisons O(n2) O(n2) O(n2)
movements 0 O(n) O(n)

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

best case avg case worst case


(ordered) (random) (reversed)
n(n – 1) n(n – 1) n(n – 1)
comparisons
2 2 2
3n(n – 1) 3n(n – 1)
movements 0
4 2

best case avg case worst case

comparisons O(n2) O(n2) O(n2)


movements 0 O(n2) O(n2)

http://www.fpt.edu.vn/ 11
Basic sorting algorithms: comparison

insertion best case avg case worst case


comparisons O(n) O(n2) O(n2)
movements O(n) O(n2) O(n2)

selection best case avg case worst case


comparisons O(n2) O(n2) O(n2)
movements 0 O(n) O(n)

bubble best case avg case worst case


comparisons O(n2) O(n2) O(n2)
movements 0 O(n2) O(n2)

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

best case avg case worst case


comparisons n/2 cn – 1 n–1
movements n n n

http://www.fpt.edu.vn/ 14
Mergesort: algorithm

mergesort(data[], first, last)


if first < last
partitioning mid = (first + last) / 2;
mergesort(data[], first, mid);
mergesort(data[], mid+1, last);
merging merge(data[], first, last);

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

include element either in


data1[] = {el: el ≤ pivot};
or in data2[] = {el: el ≥ pivot};
quicksort(data1[]);
quicksort(data2[]);
no merging

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

while (pivot.compareTo(data[upper]) < 0)


upper--;
if (lower < upper)
swap(data,lower++,upper--);
else lower++;
}
swap(data,upper,first);
if (first < upper-1)
quicksort(data,first,upper-1);
if (upper+1 < last)
quicksort(data,upper+1,last);
}
}

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

54 2 5 2 6 3 7 8 9 pivot to final cell


partitioning

2 4 2 3 pivot to the first cell 6 5 7 8


4 2 2 3 pivot to final cell
partitioning 5 6 7 8

3 2 2 pivot to the first cell 6 7 8

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

n + n + … + n = nlgn (n–1) + (n–2) + (n–3) + … + 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

Thank you for your listening!

http://www.fpt.edu.vn/ 28

You might also like