#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