Created
July 7, 2013 17:28
-
-
Save barrysteyn/5944217 to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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