-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.cpp
53 lines (46 loc) · 1.23 KB
/
bubblesort.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by Lucas on 12/03/21.
//
// Simple Bubble Sort algorithm, it accepts
// an std::vector of integers as parameter.
//
// 1. The console reads the number of elements of the array to be sorted.
// 2. The console reads all the numbers of the array separated by space.
// 3. The console outputs the sorted array.
#include <iostream>
#include <vector>
void BubbleSort (std::vector<int>& array, std::size_t n) {
for (size_t i = 0; i < n; i++) {
bool swapped = false;
for (size_t j = 1; j < n; j++) {
if (array[j - 1] > array[j]) {
std::swap(array[j - 1], array[j]);
swapped = true;
}
}
// If no swap occur, then the array is already sorted
if (!swapped) {
break;
}
}
}
std::vector<int> ReadInput() {
std::size_t n;
std::cin >> n;
std::vector<int> array(n)
for (size_t i = 0; i < n; i++) {
std::cin >> array[i];
}
return array;
}
void PrintArray(const std::vector<int>& array) {
for (size_t i = 0; i < array.size(); i++) {
std::cout << array[i] << ' ';
}
}
int main() {
std::vector<int> array = ReadInput();
BubbleSort(array);
PrintArray(array);
return 0;
}