Created
July 23, 2012 11:57
-
-
Save obstschale/3163241 to your computer and use it in GitHub Desktop.
Flagged 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
void bubbleSortV2(int a[ ], int n) // flagged bubble sort | |
{ | |
int i, temp, comps, sorted = 0; // sorted is initially false | |
comps = n – 1; | |
while ( !sorted ) // comps reduces on each pass | |
{ | |
sorted = 1; // set true for each pass | |
for (i = 0; i < comps; i++) | |
{ | |
if (a[i] > a[i + 1]) | |
{ | |
temp = a[i]; | |
a[i] = a[i + 1]; | |
a[i + 1] = temp; | |
sorted = 0; // not yet sorted | |
} | |
} // end of each pass | |
comps--; | |
} // end all passes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment