Write a C program to implement the Bubble sort technique (ascending order) and for the following input: 58 42 10 25 60

6.A) Write a C program to implement the Bubble sort technique (ascending order) and trace
the program for the following input: 58 42 10 25 60.

Answer:-

#include<stdio.h>
void main()
{
 int a[10], n, i, j, temp;
 printf("Enter the number of elements\n");
 scanf("%d", &n);
 printf("Enter the array elements\n");
 for(i=0; i<n; i++)
 scanf("%d", &a[i]);
 for(j=1; j<n;j++)
 {
 for(i=0;i<n;i++)
 {
 if(a[i]>a[i+1])
 {
 temp=a[i];
 a[i]=a[i+1];
 a[i+1]=temp;
 }
 }
 }
printf("The sorted array is\t");
for(i=0; i<n;i++)
printf(“%d\t”,a[i]);
}


Output
Enter the number of elements 7
Enter the array elements 12 23 10 13 15 1 8
The sorted array is 1 8 10 12 13 15 23

Leave a Reply

Your email address will not be published. Required fields are marked *