/* Bubble sort code */
#include <stdio.h>
void main()
{
int arr[20], n, c, d, swap;
printf("Enter 20 integers\n");
for (c = 0; c < 20; c++)
scanf("%d", &arr[c]);
for (c = 0 ; c < ( 20 - 1 ); c++)
{
for (d = 0 ; d < 20 - c - 1; d++)
{
if (arr[d] > arr[d+1]) /* For decreasing order use < */
{
swap = arr[d];
arr[d] = arr[d+1];
arr[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < 20 ; c++ )
printf("%d\n", arr[c]);
getch();
}
#include <stdio.h>
void main()
{
int arr[20], n, c, d, swap;
printf("Enter 20 integers\n");
for (c = 0; c < 20; c++)
scanf("%d", &arr[c]);
for (c = 0 ; c < ( 20 - 1 ); c++)
{
for (d = 0 ; d < 20 - c - 1; d++)
{
if (arr[d] > arr[d+1]) /* For decreasing order use < */
{
swap = arr[d];
arr[d] = arr[d+1];
arr[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < 20 ; c++ )
printf("%d\n", arr[c]);
getch();
}
No comments:
Post a Comment