Created
February 17, 2016 07:19
-
-
Save twlca/0d235977b00db583b1df to your computer and use it in GitHub Desktop.
C Programs
This file contains hidden or 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
/* 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