Swapping required to sort the array [8, 22, 7, 9, 31, 19, 5, 13] in ascending order using bubble sort is
Answers
Here is your Answer:
/*
* C program to sort 8 numbers in ascending order using Bubble sort
* and print both the given and the sorted array
*/
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE]={8, 22, 7, 9, 31, 19, 5, 13};
int i, j, temp,num=8;
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
}
I hope this helped you so plz rate the answer accordingly..