AllAlgorithms / c

Implementation of All ▲lgorithms in C Programming Language

Home Page:https://github.com/AllAlgorithms/c

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bubble Sort

bca072024 opened this issue · comments

Write a c program to sort array using bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[30],i,n;
void bubble_sort (int[],int);
printf("\nEnter no of elements");
scanf("%d",&n);
printf("\nEnter %d value",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nBefore sorting elements are");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
bubble_sort(arr,n);
printf("\nAfter sorting elements are");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
}
void bubble_sort (int arr[],int n)
{
int temp,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if (arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}