Created
December 3, 2014 15:34
-
-
Save tibbon/1bf2e87be1c4247c04d3 to your computer and use it in GitHub Desktop.
Bubble Sort in C
This file contains 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> | |
#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