2. Sorting
Sorting takes an unordered collection and makes it an ordered
one.
2
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
3. What is Bubble Sort
Idea:
• Repeatedly pass through the array
• Swaps adjacent elements that are out of order
Easier to implement, but slower…
1
3
2
9
6
4
8
j
n
1 2 3
4. "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
4
5
12
35
42
77 101
1 2 3 4 5 6
5. "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
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 77
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
6
5
12
35
77
42 101
1 2 3 4 5 6
Swap
35 77
7. "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
5
12
77
35
42 101
1 2 3 4 5 6
Swap
12 77
8. "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
5
77
12
35
42 101
1 2 3 4 5 6
No need to swap
9. "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
5
77
12
35
42 101
1 2 3 4 5 6
Swap
5 101
10. "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
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
11. 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
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
12. 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
15. Reducing the Number of Comparisons
On the Nth “bubble up”, we only need to do MAX - N comparisons.
For example:
• This is the 4th “bubble up”
• MAX is 6
• Thus, we have 2 comparisons to do
15
42
5
35
12 77
1 2 3 4 5 6
101
16. 16
#include <iostream>
using namespace std;
// perform bubble sort
void bubbleSort(int array[], int size) {
// loop to access each array element
for (int step = 0; step < size; ++step) {
// loop to compare array elements
for (int i = 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
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
17. 17
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "n";
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
// find array's length
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout << "Sorted Array in Ascending Order:n";
printArray(data, size);
return 0;
}
18. 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
42
35
12
5 77
1 2 3 4 5 6
101
84. The Fourth “Bubble Up”
84
45
23
14 33 42 67
6 98
to_do
index
3
1
N 8 did_swap false
1 2 3 4 5 6 7 8
85. The Fifth “Bubble Up”
85
45
23
14 33 42 67
6 98
to_do
index
3
1
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
86. The Fifth “Bubble Up”
86
45
23
14 33 42 67
6 98
to_do
index
3
2
N 8 did_swap false
1 2 3 4 5 6 7 8
87. The Fifth “Bubble Up”
87
45
23
14 33 42 67
6 98
to_do
index
3
2
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
88. The Fifth “Bubble Up”
88
45
23
14 33 42 67
6 98
to_do
index
3
3
N 8 did_swap false
1 2 3 4 5 6 7 8
89. The Fifth “Bubble Up”
89
45
23
14 33 42 67
6 98
to_do
index
3
3
N 8 did_swap false
No Swap
1 2 3 4 5 6 7 8
90. After Fifth Pass of Outer Loop
90
45
23
14 33 42 67
6 98
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
1 2 3 4 5 6 7 8
91. Finished “Early”
91
45
23
14 33 42 67
6 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
92. 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. 93
#include <iostream>
using namespace std;
// perform bubble sort
void bubbleSort(int array[], int size) {
// check if swapping occurs
int flag = 0;
// loop to access each array element
for (int step = 0; step < size; ++step) {
// loop to compare array elements
for (int i = 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
int temp = 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;
}
}
94. 94
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "n";
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
// find array's length
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout << "Sorted Array in Ascending Order:n";
printArray(data, size);
return 0;
}
95. 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
97. 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
1
3
2
9
6
4
8
98. Assignment # 1
• Perform selection sort on user defined data and implement the code
in C++.
• Deadline: 4th March, 2024
98