Created
March 11, 2018 22:39
-
-
Save sojohnnysaid/b4b206ea6e264224b4f6b7069fa5beea to your computer and use it in GitHub Desktop.
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> | |
void print(int* array, int length); | |
void main(void) | |
{ | |
// bubble sort | |
// while the sorted counter is less than 1 | |
// loop through n-1 elements | |
// if n+1 is less than n, swap and add one to swap counter | |
int array[] = {5,2,4,3,1}; | |
int len = sizeof array / sizeof array[0]; | |
int i, sort_counter; | |
do | |
{ | |
sort_counter = 0; | |
for(i = 0; i < len-1; i++) | |
{ | |
if(array[i+1] < array[i]) | |
{ | |
int temp = array[i]; | |
array[i] = array[i+1]; | |
array[i+1] = temp; | |
sort_counter++; | |
} | |
} | |
}while(sort_counter > 0); | |
print(array, len); | |
} | |
void print(int* array, int length) | |
{ | |
printf("["); | |
for(int i = 0; i < length; i++) | |
{ | |
if(i == length-1) | |
printf("%d", array[i]); | |
else | |
printf("%d, ", array[i]); | |
} | |
printf("]\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment