Created
May 28, 2017 12:07
-
-
Save nramsbottom/bb61571a1105e0f6a44f3193f1eb670c to your computer and use it in GitHub Desktop.
Simple Bubble Sort
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 show(int arr[], int count) { | |
| for (int n = 0; n < count - 1; n++) | |
| printf("%d ", arr[n]); | |
| printf("\n"); | |
| } | |
| void bubble_sort(int arr[], int count) { | |
| int x, y, z; | |
| // loop through every item | |
| for (x = 0; x < count - 1; x++) { | |
| // loop through all the items up until the current "inner" item | |
| for (y = 0; y < count - x - 1; y++) { | |
| // current value is greater than the next one? swap them | |
| if (arr[y] > arr[y + 1]) { | |
| z = arr[y]; | |
| arr[y] = arr[y + 1]; | |
| arr[y + 1] = z; | |
| } | |
| } | |
| } | |
| } | |
| int main(int argc, char *argv[]) { | |
| int arr1[] = { 1, 2, 5, 6, 7, 4, 3 }; | |
| int arr2[] = { 7, 2, 3, 6, 1, 4, 5 }; | |
| bubble_sort(arr1, 7); | |
| show(arr1, 7); | |
| bubble_sort(arr2, 7); | |
| show(arr2, 7); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment