Code:
#include
#define MAX_SIZE 100
int main()
{
int array[MAX_SIZE];
int size;
int i, j, temp;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array: ");
for(i=0; i
{
scanf("%d", &array[i]);
}
for(i=0; i
{
/*
* Place the currently selected element array[i]
* to its correct place.
*/
for(j=i+1; j
{
/*
* Swap if currently selected array element
* is not at its correct position.
*/
if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
/* Print the sorted array */
printf("\nElements of array in sorted ascending order: ");
for(i=0; i
{
printf("%d\t", array[i]);
}
return 0;
}
Output:
Enter size of array: 10
Enter elements in array: 20 2 10 6 52 31 0 45 79 40
Elements of array in sorted ascending order: 0 2 6 10 20 31 40 45 52 79
More C Programs: