-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
38 lines (35 loc) · 883 Bytes
/
Copy pathbubble_sort.cpp
File metadata and controls
38 lines (35 loc) · 883 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
37
38
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void bubble_swap(int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
void bubble_sort(int numbers[], int size) {
for (int i = 0; i < size - 1; i++) {
int ordered = 0;
for (int j = 0; j < size - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
bubble_swap(&numbers[j], &numbers[j + 1]);
ordered = 1;
}
}
if (ordered == 0) {
break;
}
}
}
int bubble_sort_main() {
int numbers[] = { 8, 9, 3, 7, 1, 0, 2, 4, 12, 13, 10 };
int ordered_numbers[] = { 0, 1, 2, 3, 4, 7, 8, 9, 10, 12, 13 };
int size = 11;
bubble_sort(numbers, size);
for (int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
return 0;
}