Sorting
Sorting takes an unordered collection and makes it an ordered
one.
2
512354277 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
What is Bubble Sort
Idea:
•Repeatedly pass through the array
•Swaps adjacent elements that are out of order
Easier to implement, but slower…
1329648
j
n1 2 3
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest valueto the end using pair-wise comparisons and
swapping
4
512354277 101
1 2 3 4 5 6
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
512354277 101
1 2 3 4 5 6
Swap
42 77
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest value to the end using pair-wise comparisons and
swapping
6
512357742 101
1 2 3 4 5 6
Swap
35 77
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest value to the end using pair-wise comparisons and swapping
7
512773542 101
1 2 3 4 5 6
Swap
12 77
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest value to the end using pair-wise comparisons and
swapping
8
577123542 101
1 2 3 4 5 6
No need to swap
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest value to the end using pair-wise comparisons and swapping
9
577123542 101
1 2 3 4 5 6
Swap
5 101
"Bubbling Up" the Largest Element
Traverse a collection of elements
•Move from the front to the end
•“Bubble” the largest value to the end using pair-wise comparisons and swapping
10
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
Items of Interest
Notice that only the largest value is correctly placed
All other values are still out of order
So, we need to repeat this process
11
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
Repeat “Bubble Up” How Many Times?
If we have N elements…
And if each time we bubble an element, we place it in its correct
location…
Then we repeat the “bubble up” process N –1 times.
This guarantees we’ll correctly place all N elements.
12
Reducing the Number of Comparisons
On the N
th
“bubble up”, we only need to do MAX -N comparisons.
For example:
•This is the 4
th
“bubble up”
•MAX is 6
•Thus, we have 2 comparisonsto do
15
4253512 77
1 2 3 4 5 6
101
16
#include <iostream>
usingnamespace std;
// perform bubble sort
voidbubbleSort(intarray[], intsize) {
// loop to access each array element
for(intstep = 0; step < size; ++step) {
// loop to compare array elements
for (inti= 0; i< size -step; ++i) {
// compare two adjacent elements
// change > to < to sort in descending order
if (array[i] > array[i+ 1]) {
// swapping elements if elements
// are not in the intended order
inttemp = array[i];
array[i] = array[i+ 1];
array[i+ 1] = temp;
}
}
}
}
Already Sorted Array?
What if the array was already sorted?
What if only a few elements were out of place and after a couple
of “bubble ups,” the collection was sorted?
We want to be able to detect this and “stop early”!
18
4235125 77
1 2 3 4 5 6
101
An Animated Example
19
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
An Animated Example
20
674523 14 6 3398 42
to_do
index
7
1
N 8 did_swap false
1 2 3 4 5 6 7 8
An Animated Example
21
674523 14 6 3398 42
to_do
index
7
1
N 8
Swap
did_swap false
1 2 3 4 5 6 7 8
An Animated Example
22
674598 14 6 3323 42
to_do
index
7
1
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
23
674598 14 6 3323 42
to_do
index
7
2
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
24
674598 14 6 3323 42
to_do
index
7
2
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
25
679845 14 6 3323 42
to_do
index
7
2
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
26
679845 14 6 3323 42
to_do
index
7
3
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
27
679845 14 6 3323 42
to_do
index
7
3
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
28
671445 98 6 3323 42
to_do
index
7
3
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
29
671445 98 6 3323 42
to_do
index
7
4
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
30
671445 98 6 3323 42
to_do
index
7
4
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
31
671445 6 98 3323 42
to_do
index
7
4
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
32
671445 6 98 3323 42
to_do
index
7
5
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
33
671445 6 98 3323 42
to_do
index
7
5
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
34
981445 6 67 3323 42
to_do
index
7
5
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
35
981445 6 67 3323 42
to_do
index
7
6
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
36
981445 6 67 3323 42
to_do
index
7
6
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
37
331445 6 67 9823 42
to_do
index
7
6
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
38
331445 6 67 9823 42
to_do
index
7
7
N 8 did_swap true
1 2 3 4 5 6 7 8
An Animated Example
39
331445 6 67 9823 42
to_do
index
7
7
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
An Animated Example
40
331445 6 67 4223 98
to_do
index
7
7
N 8
Swap
did_swap true
1 2 3 4 5 6 7 8
After First Pass of Outer Loop
41
331445 6 67 4223 98
to_do
index
7
8
N 8
Finished first “Bubble Up”
did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
42
331445 6 67 4223 98
to_do
index
6
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Second “Bubble Up”
43
331445 6 67 4223 98
to_do
index
6
1
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
44
331445 6 67 4223 98
to_do
index
6
2
N 8 did_swap false
1 2 3 4 5 6 7 8
The Second “Bubble Up”
45
331445 6 67 4223 98
to_do
index
6
2
N 8 did_swap false
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
46
334514 6 67 4223 98
to_do
index
6
2
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
47
334514 6 67 4223 98
to_do
index
6
3
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
48
334514 6 67 4223 98
to_do
index
6
3
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
49
33614 4567 4223 98
to_do
index
6
3
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
50
33614 4567 4223 98
to_do
index
6
4
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
51
33614 4567 4223 98
to_do
index
6
4
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
52
33614 4567 4223 98
to_do
index
6
5
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
53
33614 4567 4223 98
to_do
index
6
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
54
67614 4533 4223 98
to_do
index
6
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
55
67614 4533 4223 98
to_do
index
6
6
N 8 did_swap true
1 2 3 4 5 6 7 8
The Second “Bubble Up”
56
67614 4533 4223 98
to_do
index
6
6
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Second “Bubble Up”
57
42614 4533 6723 98
to_do
index
6
6
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop
58
42614 4533 6723 98
to_do
index
6
7
N 8 did_swap true
Finished second “Bubble Up”
1 2 3 4 5 6 7 8
The Third “Bubble Up”
59
42614 4533 6723 98
to_do
index
5
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Third “Bubble Up”
60
42614 4533 6723 98
to_do
index
5
1
N 8 did_swap false
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
61
42623 4533 6714 98
to_do
index
5
1
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
62
42623 4533 6714 98
to_do
index
5
2
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
63
42623 4533 6714 98
to_do
index
5
2
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
64
42236 4533 6714 98
to_do
index
5
2
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
65
42236 4533 6714 98
to_do
index
5
3
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
66
42236 4533 6714 98
to_do
index
5
3
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
67
42236 4533 6714 98
to_do
index
5
4
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
68
42236 4533 6714 98
to_do
index
5
4
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
69
42236 3345 6714 98
to_do
index
5
4
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
70
42236 3345 6714 98
to_do
index
5
5
N 8 did_swap true
1 2 3 4 5 6 7 8
The Third “Bubble Up”
71
42236 3345 6714 98
to_do
index
5
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
72
45236 3342 6714 98
to_do
index
5
5
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Third “Bubble Up”
73
45236 3342 6714 98
to_do
index
5
6
N 8 did_swap true
Finished third “Bubble Up”
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
74
45236 3342 6714 98
to_do
index
4
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
75
45236 3342 6714 98
to_do
index
4
1
N 8 did_swap false
Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
76
452314 3342 676 98
to_do
index
4
1
N 8 did_swap true
Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
77
452314 3342 676 98
to_do
index
4
2
N 8 did_swap true
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
78
452314 3342 676 98
to_do
index
4
2
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
79
452314 3342 676 98
to_do
index
4
3
N 8 did_swap true
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
80
452314 3342 676 98
to_do
index
4
3
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
81
452314 3342 676 98
to_do
index
4
4
N 8 did_swap true
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
82
452314 3342 676 98
to_do
index
4
4
N 8 did_swap true
No Swap
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
83
452314 3342 676 98
to_do
index
4
5
N 8 did_swap true
Finished fourth “Bubble Up”
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
84
452314 3342 676 98
to_do
index
3
1
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
85
452314 3342 676 98
to_do
index
3
1
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
86
452314 3342 676 98
to_do
index
3
2
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
87
452314 3342 676 98
to_do
index
3
2
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
88
452314 3342 676 98
to_do
index
3
3
N 8 did_swap false
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
89
452314 3342 676 98
to_do
index
3
3
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop
90
452314 3342 676 98
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
1 2 3 4 5 6 7 8
Finished “Early”
91
452314 3342 676 98
to_do
index
3
4
N 8 did_swap false
We didn’t do any swapping, so all of the other
elements must be correctly placed.
We can “skip” the last two passes of the outer loop.
1 2 3 4 5 6 7 8
Using a Boolean “Flag”
We can use a boolean variable to determine if any swapping occurred
during the “bubble up.”
If no swapping occurred, then we know that the array is already
sorted!
This boolean “flag” needs to be reset after each “bubble up.”
92
93
#include <iostream>
usingnamespace std;
// perform bubble sort
voidbubbleSort(intarray[], intsize) {
// check if swapping occurs
intflag= 0;
// loop to access each array element
for(intstep = 0; step < size; ++step) {
// loop to compare array elements
for (inti= 0; i< size -step; ++i) {
// compare two adjacent elements
// change > to < to sort in descending order
if (array[i] > array[i+ 1]) {
// swapping elements if elements
// are not in the intended order
inttemp = array[i];
array[i] = array[i+ 1];
array[i+ 1] = temp;
flag= 1;
}
}
// no swapping means the array is already sorted
// so no need of further comparison
if(flag== 0)
break;
}
}
Selection Sort
Idea:
•Find the smallest element in the array
•Exchange it with the element in the first position
•Find the second smallest element and exchange it with the element in the
second position
•Continue until the array is sorted
Disadvantage:
•Running time depends only slightly on the amount of order in the file
95
Example
96
1329648
8329641
8349621
8649321
8964321
8694321
9864321
9864321
Selection Sort
Alg.:SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n -1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
97
1329648
Assignment # 1
•Perform selection sort on user defined data and implement the code
in C++.
•Deadline: 4
th
March, 2024
98