C Program to Sort N Numbers in Ascending Order using Bubble Sort - IProgramX

Q. Write a function to sort an array of n integers using Bubble sort method. Accept n numbers from the user, store them in an array and sort them using this function. Display the sorted array


Program

#include<stdio.h>
#include<stdlib.h>
void bubblesort(int a[],int size);
void main()
{
int a[50],n,i;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubblesort(a,n);
printf("\n the sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void bubblesort(int a[],int size)
{
int temp,i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

Output:

 enter the size of the array 5

 enter the array
5
3
8
6
1

 the sorted array is
1       3       5       6       8

Post a Comment

0 Comments