c programming for bubble sort

#include<stdio.h>
void bubble_sort(int [],int num);
int main()
{
int arr[100],num,i;
printf(“n Enter the size of the arrayn”);
scanf(“%d”,&num);
printf(“n Enter the elements in the array one by onen”);
for(i=0;i<num;i++)
{
scanf(“%d”,&arr[i]);
}
bubble_sort(arr,num);
//output
for(i=0;i<num;i++)
{
printf(“%d”,arr[i]);
}
}

void bubble_sort(int arr[],int num)
{
int i,j,temp;
for(i=1;i<=num-1;i++)//i is for passes, if the size of the array is 4 we need to go for n-1 indices only
{
for(j=0;j<=num-i-1;j++)// here in j<=num-i-1 we are subtracting i as in each pass one element is sorted so to avoid comaring with that we are sutracting i means with pass
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}

OUTPUT-
 Enter the size of the array
4
 Enter the elements in the array one by one
9
8
6
4
4689

Leave a Comment

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

Hire a server Expert to resolve the issue Now.

Hire a server Expert to resolve the issue Now.

Resolve this issue in just 5$ from https://serverexpert.io

 

You have Successfully Subscribed!

Open chat