Skip to content

Instantly share code, notes, and snippets.

@ardrabczyk
Last active March 16, 2022 23:35
Show Gist options
  • Save ardrabczyk/034755783ac50d276063 to your computer and use it in GitHub Desktop.
Save ardrabczyk/034755783ac50d276063 to your computer and use it in GitHub Desktop.
Bubble sort implementation in C using do...while loop
#include <stdio.h>
#include <stdlib.h>
void bubble(int arr[], size_t size)
{
int i = 0;
int temp;
int again = 1;
int iter = size - 1;
do {
again = 0;
i = 0;
for(; i < iter; ++i)
{
if (arr[i + 1] < arr[i])
{
again = 1;
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
--iter;
} while (again);
}
int main(void)
{
int a[] = {5, 1, 4, 2, 8, 0, -11};
bubble(a, sizeof a / sizeof (int));
unsigned i = 0;
for(; i < (sizeof a / sizeof (int)); ++i)
{
printf("%d\n", a[i]);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment