Skip to content

Instantly share code, notes, and snippets.

@obstschale
Created July 23, 2012 11:57
Show Gist options
  • Save obstschale/3163241 to your computer and use it in GitHub Desktop.
Save obstschale/3163241 to your computer and use it in GitHub Desktop.
Flagged Bubble Sort
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