Skip to content

Instantly share code, notes, and snippets.

@barrysteyn
Created July 7, 2013 17:28
Show Gist options
  • Save barrysteyn/5944217 to your computer and use it in GitHub Desktop.
Save barrysteyn/5944217 to your computer and use it in GitHub Desktop.
Bubble Sort
/*
* My Canonical example of a C bubble sort
* This is still O(n^2) (no getting away from that)
* but it is slightly more efficient than the classic
* examples given in textbooks because the inner loop
* loops one less every time
*/
void bubbleSort_c(int *arr, int size) {
int temp = 0;
for (int i=size-1; i >=0; i--) {
for (int j=0; j < i; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment