-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
36 lines (33 loc) · 816 Bytes
/
BubbleSort.cpp
File metadata and controls
36 lines (33 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "BubbleSort.h"
BubbleSort::BubbleSort()
{
}
/**
* Sorts an array using the Bubble Sort algorithm.
*
* @param arr the array to be sorted
* @param i the current index of the array being compared
* @param n the number of elements left to be sorted
* @param operation_counter the counter for tracking the number of operations
*
* @return true indicating the sorting was completed
*
* @throws None
*/
bool BubbleSort::tick(std::vector<int>& arr, int& i, int& n, int& operation_counter) {
bool swapped = false;
while (i < n + 1) {
if (arr[i - 1] > arr[i]) {
std::swap(arr[i - 1], arr[i]);
swapped = true;
operation_counter++;
break;
}
i++;
}
if (!swapped) {
i = 1;
n--;
}
return true;
}