#include using namespace std; void bubble_sort(int array[], size_t length){ // while the array isn't sorted yet: // run through the array comparing each pair of numbers bool sorted = false; size_t end_count = 1; while(!sorted){ sorted = true; for(int i = 0; i < length - end_count; i++){ if(array[i] > array[i+1]){ // if this never happens, it's sorted! int tmp = array[i]; array[i] = array[i+1]; array[i+1] = tmp; sorted = false; } } end_count++; } cout << "End count ended as: " << end_count << endl; cout << "We went through the array " << end_count - 1 << " times\n"; } void print_array(int array[], size_t length){ for(int i = 0; i < length; i++) cout << array[i] << " "; cout << "\n"; } int main(){ int arr[20]; for(int& i : arr) i = (random() % 100); print_array(arr, 20); bubble_sort(arr, 20); print_array(arr, 20); bubble_sort(arr, 20); // It's already sorted! return 0; }