Skip to content

Instantly share code, notes, and snippets.

@twlca
Created February 17, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twlca/0d235977b00db583b1df to your computer and use it in GitHub Desktop.
Save twlca/0d235977b00db583b1df to your computer and use it in GitHub Desktop.
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