Skip to content

Instantly share code, notes, and snippets.

@chikoski
Created December 24, 2012 08:33
Show Gist options
  • Save chikoski/4368360 to your computer and use it in GitHub Desktop.
Save chikoski/4368360 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void printArray(int* arr, int length){
int i;
for(i = 0; i < length; i++){
printf("%d", arr[i]);
if(i + 1 < length){
printf(",");
}
}
printf("\n");
}
void bubbleSortHelper(int* arr, int length){
int i, tmp;
for(i = 1; i < length; i++){
if(arr[i-1] > arr[i]){
tmp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = tmp;
}
}
}
void bubbleSort(int *arr, int length){
int i;
for(i = length; i > 0; i--){
bubbleSortHelper(arr, i);
printArray(arr, length);
}
}
int main(int argc, char** argv){
int arr[] = {8, 23, 10, 3, 2, 4, 0, 1, 9};
int length = sizeof(arr) / sizeof(arr[0]);
printArray(arr, length);
bubbleSort(arr, length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment