Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Created March 11, 2018 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/b4b206ea6e264224b4f6b7069fa5beea to your computer and use it in GitHub Desktop.
Save sojohnnysaid/b4b206ea6e264224b4f6b7069fa5beea to your computer and use it in GitHub Desktop.
#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