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

Chapter9 Part1

The document discusses different sorting algorithms: insertion sort, selection sort, bubble sort, and comb sort. It analyzes the time complexity of each algorithm in terms of comparisons and movements/swaps in best, average, and worst cases. Comb sort is presented as an improvement on bubble sort by using larger gaps between elements at the start that gradually shrink to optimize performance.

Uploaded by

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

Chapter9 Part1

The document discusses different sorting algorithms: insertion sort, selection sort, bubble sort, and comb sort. It analyzes the time complexity of each algorithm in terms of comparisons and movements/swaps in best, average, and worst cases. Comb sort is presented as an improvement on bubble sort by using larger gaps between elements at the start that gradually shrink to optimize performance.

Uploaded by

Artemis Zeusborn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

COS 212

Chapter 9 (Sorting):
Elementary Algorithms
Sorting is important
 Why do we sort data?
 Because it makes searching so much easier!
 Suppose we’re looking for a student number…

 15367822 15990400 14367789 15562890 15782200 15378830

 14367789 15367822 15378830 15562890 15782200 15990400

 For a human, sorted data is easier to comprehend and process


 A computer, having no consciousness, does not care… But sorted data means
we can find an item much more efficiently: consider binary search
 Some algorithms rely on data being sorted
 Some data structures rely on sorted data
 Sorting is important, and we need good algorithms for it!
What makes a good sorting algorithm?
 Fast execution!
 I.e., if time is defined as t = f(n), we want time to increase as slowly
as possible as n, number of items, increases
 Big-O complexity can be used to estimate time efficiency of sorting
 What actions constitute sorting?
 Comparisons
 Movements

7 3 5
What makes a good sorting algorithm?
 The number of actions will depend on the order of elements
 Consider:

Every element
needs to move 7 3 5 Worst case

Only 5 and 3 need


to move 5 3 7 Average case

No elements need
to move 3 5 7 Best case

 We want an algorithm that performs the best in all cases


Main idea: take an element from the
Insertion Sort unsorted part of the array, insert it into the
insertionSort(data[ ]) sorted part
for i = 1 to data.length-1 // initialize i to 1 (2nd element)
tmp = data[i]; // store i’th element in tmp
j = i – 1; // set j to the element preceding i
while j >= 0 && data[j] > tmp // find correct position
data[j + 1] = data[j]; // move j’th up by 1 position
j--; // go to an earlier element, 1 pos. back
data[j + 1] = tmp; // end of loop: pos. found!

5 1 3 4
2 2 2 2 1 1 1 1 1
5 5 5 2 2 2 2 2 2
1 1 5 5 5 5 3 3 3
3 3 3 3 3 5 5 5 4
4 4 4 4 4 4 4 5 5
Insertion Sort:
Efficiency
n = data.length

insertionSort(data[ ]) How many times will


for i = 1 to n the outer loop (n – 1)
tmp = data[i]; execute?
j = i – 1;
while j >= 0 && data[j] > tmp How many times
will the inner loop
data[j + 1] = data[j];
execute?
j--;
data[j + 1] = tmp;
Depends on the case!

Best case Worst case


Average case
(ordered) (reversed)

0 1, 2, … (n – 2), (n – 1) ½ of the
worst case?
Insertion Sort: Efficiency
insertionSort(data[ ])
for i = 1 to n-1
tmp = data[i];
j = i – 1;
while j >= 0 && data[j] > tmp
data[j + 1] = data[j]; j--;
data[j + 1] = tmp;

best case avg case worst case

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

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

Is average case better than the worst case?


Selection Sort Main idea: find the smallest element, put it
in the correct position
selectionSort(data[ ])
for i = 0 to data.length-1
select the smallest element among
data[i], …, data[data.length-1];
swap it with data[i];

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
Selection Sort: Efficiency
How many times will
selectionSort(data[ ]) the outer loop (n – 1)
for i = 0 to n-1 execute?
select the smallest element among How many times
data[i], …, data[n-1]; will the inner loop
swap it with data[i]; execute?

Do any movements happen in the (n – 1) , (n – 2), … 2, 1


1
inner loop?
2
5 What about the outer loop?

3
best case avg case worst case
4 (largest first, the rest
(ordered) (random) is ordered)

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

movements 0 O(n) O(n)


Bubble Sort Main idea: swap neighbour elements if they
are out of order
bubbleSort(data[ ])
for i = 0 to data.length-2
for j = data.length-1 down to i+1
if elements in positions j and j-1 are out of order,
swap them;

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
(n – 1)
Bubble Sort How many times will
bubbleSort(data[ ]) the outer loop
for i = 0 to n-2 execute?
for j = n-1 down to i+1 How many times
if elements in positions j and j-1 will the inner loop
are out of order, swap them; execute?

(n – 1) , (n – 2), … 2, 1

Does the number of movements differ


from the number of comparisons?

best case avg case worst case


(ordered) (random) (reversed)

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

movements 0 O(n2) O(n2)


Who’s the fastest of them all?

insertion best avg worst selection best avg worst

comp. O(n) O(n2) O(n2) comp. O(n2) O(n2) O(n2)


move O(n) O(n2) O(n2) move 0 O(n) O(n)

bubble best avg worst


comp. O(n2) O(n2) O(n2)
move 0 O(n2) O(n2)
Why is bubble sort so slow?
 Because every element that is not in its right place is painstakingly
swapped with other elements one-by-one

 Big elements move to their right places quite fast


 But small elements take forever to get to
the start…
 Can we speed up the “turtles”?
 Idea: what if, instead of comparing
adjacent elements, we compare every
third element?
 Comb sort: start with sparse comparisons,
decrease the gap over time
Comb Sort
combSort(data[ ])
gap = n // this will be our shrinking gap Shrinking the gap
swapped = false // stop when no swaps happen step
while gap > 1 or swapped
swapped = false
if gap > 1 then
gap = floor(gap/1.3) // shrink! Bubble sort step
i = 0
while i + gap < n
if data[i] > data[i + gap]
swap data[i] with data[i + gap]
i += 1

How many times will How many times


the outer loop will the inner loop
execute? execute?

log n? (n - n/1.3), (n - n/1.32), … , n


Comb Sort
gap = 10
33 98 74 13 55 20 77 45 64 83 gap/1.3 = 7

33 98 74 13 55 20 77 45 64 83
33 98 74 13 55 20 77 45 64 83
33 64 74 13 55 20 77 45 98 83
33 64 74 13 55 20 77 45 98 83
gap/1.3 = 5
33 64 74 13 55 20 77 45 98 83
20 64 74 13 55 33 77 45 98 83
20 64 74 13 55 33 77 45 98 83
20 64 74 13 55 33 77 45 98 83
20 64 45 13 55 33 77 74 98 83
20 64 45 13 55 33 77 74 98 83
20 64 45 13 55 33 77 74 98 83
Comb Sort
20 64 45 13 55 33 77 74 98 83
13 64 45 20 55 33 77 74 98 83 gap/1.3 = 3
13 64 45 20 55 33 77 74 98 83
13 55 45 20 64 33 77 74 98 83
13 55 45 20 64 33 77 74 98 83
13 55 33 20 64 45 77 74 98 83
13 55 33 20 64 45 77 74 98 83
13 55 33 20 64 45 77 74 98 83
13 55 33 20 64 45 77 74 98 83
13 55 33 20 64 45 77 74 98 83

13 55 33 20 64 45 77 74 98 83
13 55 33 20 64 45 77 74 98 83
13 20 33 55 64 45 77 74 98 83
gap/1.3 = 2 13 20 33 55 64 45 77 74 98 83
13 20 33 55 64 45 77 74 98 83
13 20 33 45 64 55 77 74 98 83
13 20 33 45 64 55 77 74 98 83
13 20 33 45 64 55 77 74 98 83
13 20 33 45 64 55 77 74 98 83
13 20 33 45 64 55 77 74 98 83
Comb Sort

gap/1.3 = 1 Efficiency?

13 20 33 45 64 55 77 74 98 83 Best: O(n lg n)


13 20 33 45 64 55 77 74 98 83
13 20 33 45 64 55 77 74 98 83
13 20 33 45 64 55 77 74 98 83 Worst: O(n2)
13 20 33 45 64 55 77 74 98 83
13 20 33 45 55 64 77 74 98 83
13 20 33 45 55 64 77 74 98 83
13 20 33 45 55 64 77 74 98 83
13 20 33 45 55 64 74 77 98 83
13 20 33 45 55 64 74 77 98 83
13 20 33 45 55 64 74 77 83 98
13 20 33 45 55 64 74 77 83 98

You might also like