C Source code for implementing Bubble Sorting. This program accepts No. of values to be sorted and the actual values to be sorted from user. By using bubble sort algorithm it sorts the values and prints the output.
Source code
/*
Bubble sorting
--------------
Input :
1. No. of values to be sorted i.e. n.
2. Values to be sorted. i.e. n values.
Output:
List of sorted values.
*/
# include <stdio.h>
# include <conio.h>
int main() {
int list[100], n, i, j, temp;
clrscr();
printf("Enter number of values to be sorted (Maximum: 100):");
scanf("%d", &n);
printf("Enter %d values:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &list[i]);
for (i = 0 ; i < ( n - 1 ); i++) {
for (j = 0 ; j < n - i - 1; j++) {
if (list[j] > list[j+1]) {
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
printf("Sorted list:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", list[i]);
getch();
return 0;
}
Output
Source code
/*
Bubble sorting
--------------
Input :
1. No. of values to be sorted i.e. n.
2. Values to be sorted. i.e. n values.
Output:
List of sorted values.
*/
# include <stdio.h>
# include <conio.h>
int main() {
int list[100], n, i, j, temp;
clrscr();
printf("Enter number of values to be sorted (Maximum: 100):");
scanf("%d", &n);
printf("Enter %d values:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &list[i]);
for (i = 0 ; i < ( n - 1 ); i++) {
for (j = 0 ; j < n - i - 1; j++) {
if (list[j] > list[j+1]) {
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
printf("Sorted list:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", list[i]);
getch();
return 0;
}
Output
No comments:
Post a Comment