Last active
August 29, 2015 14:13
-
-
Save petomalina/e7e11019c9a380e1baf9 to your computer and use it in GitHub Desktop.
Bubble sort with no swap detection
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
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