blob: 43890fdca01aaf8c74493cf0f454536569268ea3 (
plain)
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
|
#include "main.h"
#include "bubble.h"
void bubble_sort(int* arr, unsigned size) {
// index array (stabiliteit)
// int* indices = malloc(size * sizeof(int));
// for (unsigned i = 0; i < size; i++) indices[i] = i;
int outer, inner;
bool sorted;
for (outer = 0; outer < size - 2; outer++) {
sorted = true; // check if array was modified during inner loop
for (inner = size - 2; inner >= outer; inner--) {
if (arr[inner] >= arr[inner+1]) {
swap(arr[inner], arr[inner+1]);
// swap(indices[inner], indices[inner+1]); // swap indices array (stabiliteit)
sorted = false;
}
}
printf("outer %d: ", outer);
print_arr(arr, size);
// print index array (stabiliteit)
// printf("index %d: ", outer);
// print_arr(indices, size);
if (sorted) break; // break if nothing changed (already sorted)
}
}
|