Created
December 24, 2012 08:33
-
-
Save chikoski/4368360 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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