Skip to content

Instantly share code, notes, and snippets.

@twlca twlca/bubble_sort.c
Created Feb 17, 2016

Embed
What would you like to do?
C Programs
/* File Name: bubble_sort.c
*/
#define N 10
int a[N] = {25, 3, 42, 1, 53, 12, 62, 14, 47, 20};
main()
{
int i, j, k, l, t;
int temp;
int count = 1;
printf("\nThe source data is : ");
for ( i = 0; i < N; i++ )
printf("%4d ", a[i]);
printf("\n");
i = 0;
k = N;
/* Start Bubble Sort */
while ( k != 0 )
{
printf("\nThe %2dth Pass:", ++i);
t = 0;
for ( j = 0; j <= k - 1; j++ )
{
if ( a[j] > a[j+1] )
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
printf("\nThe %2dth process ==> ", count++);
for ( l = 0; l < N; l++ )
printf("%4d ", a[l]);
t = j;
}
k = t;
}
/* Print Out Sorted Data */
printf("\n\nData after sort is : ");
for ( i = 0; i < N; i++ )
printf("%4d ", a[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.