This Program will Takes 2 inputs
- For the size of array
- Input for arrays
#include<stdio.h>
#define MAX 100
int main(void)
{
int arr[MAX],i,j,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}
/*Bubble sort*/
for(i=0; i<n-1 ;i++)
{
xchanges = 0;
for(j=0; j<n-1-i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
xchanges++;
}
}
if(xchanges==0) /*If list is sorted*/
break;
}
printf("Sorted list is :\n");
for(i=0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
return 0;
}
Output
Enter the number of elements : 6
Enter element 1 : 12
Enter element 2 : 13
Enter element 3 : 14
Enter element 4 : 15
Enter element 5 : 16
Enter element 6 : 17
Sorted list is :
12 13 14 15 16 17