C Program to Implement Bubble Sort of Given Numbers

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Here’s a simple C program to implement Bubble Sort

Program :

#include<stdio.h>
void bubble_sort(long[],long);
int main()
{
long arr[100],n,c,d,swap;
printf(“Enter number of element : “);
scanf(“%ld”,&n);
printf(“Enter %ld Longegers : \n”,n);
for(c=0;c<n;c++)
{
scanf(“%ld”,&arr[c]);
}
bubble_sort(arr,n);
printf(“Sorted listin ascending order : \n”);
for(c=0;c<n;c++)
{
printf(“%ld\n”,arr[c]);
}
return 0;
}
void bubble_sort(long list[], long n)
{
long c,d,t;
for(c=0;c<(n-1);c++)
{
for(d=0;d<n-c-1;d++)
{
if(list[d]>list[d+1])
{
t=list[d];
list[d]=list[d+1];
list[d+1]=t;
}
}
}
}

Ouput :

Leave a Comment

Your email address will not be published.

Shopping Basket
Verified by MonsterInsights