Skip to content

Instantly share code, notes, and snippets.

@petomalina
Last active August 29, 2015 14:13
Show Gist options
  • Save petomalina/e7e11019c9a380e1baf9 to your computer and use it in GitHub Desktop.
Save petomalina/e7e11019c9a380e1baf9 to your computer and use it in GitHub Desktop.
Bubble sort with no swap detection
int *bubbleSort(int *array, int count) {
int unsorted = count; // number of unsorted elements in array
bool swapped = false;
while(unsorted > 1) {
swapped = false;
for(int i = 0; i < unsorted-1; i++) {
if(array[i] > array[i+1]) {
// swap
int temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
swapped = true; // need another iteration
}
}
if(swapped == false) { // no swaps, dont need next iteration
break;
}
unsorted--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment