Skip to content

Instantly share code, notes, and snippets.

@tibbon
Created December 3, 2014 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tibbon/1bf2e87be1c4247c04d3 to your computer and use it in GitHub Desktop.
Save tibbon/1bf2e87be1c4247c04d3 to your computer and use it in GitHub Desktop.
Bubble Sort in C
#include <stdio.h>
#define ARRAYSIZE 8
void bubble_srt(int unsortedArray[], int arraySize) {
int temp;
for(int i = 0; i < arraySize; i++) {
for(int j = 0; j < (arraySize - 1); j++) {
if(unsortedArray[j] > unsortedArray[j + 1]) {
temp = unsortedArray[j];
unsortedArray[j] = unsortedArray[j + 1];
unsortedArray[j + 1] = temp;
}
}
}
}
int main(void) {
int array[ARRAYSIZE] = {12, 9, 4, 99, 120, 1, 3, 10};
printf("Before the sort:\n"); // Show results after the sort
for(int i = 0; i < ARRAYSIZE; i++)
printf("%d ", array[i]);
printf("\n");
bubble_srt(array, ARRAYSIZE);
printf("After the sort:\n"); // Show results after the sort
for(int i = 0; i < ARRAYSIZE; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment