Skip to content

Instantly share code, notes, and snippets.

@d630
Last active May 29, 2018 17:53
Show Gist options
  • Save d630/e3a048251dcb3b1e7d4f078c058a768f to your computer and use it in GitHub Desktop.
Save d630/e3a048251dcb3b1e7d4f078c058a768f to your computer and use it in GitHub Desktop.
Bubble sort
#include <stdio.h>
#include <stdlib.h>
void
bubble_sort (int *arr, int len)
{
int tmp;
for (int i = len - 1; i >= 0; i--)
for (int j = 0; j < i; j++)
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
void
test_print(int *arr, int len)
{
for (int i = 0; i < len; i++)
printf("%d %d\n", i, arr[i]);
puts("");
}
int
main()
{
int iarr[] = { 3, 1, 7, 5, 2, 6 };
int len = sizeof(iarr)/sizeof(iarr[0]);
test_print(iarr, len);
bubble_sort(iarr, len);
test_print(iarr, len);
return 0;
}
// vim: set ft=c :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment