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 array\n");
scanf("%d",&num);
printf("\n Enter the elements in the array one by one\n");
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

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation