#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) } }