c programming for bubble sort

#include<stdio.h>
void selection_sort(int arr[],int num);
int main()
{
int arr[100],num,i;
printf("Enter the size of array\n");
scanf("%d",&num);
printf("\nEnter the elements in array one by one\n");
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
selection_sort(arr,num);
printf("The sorted array is\n");
for(i=0;i<num;i++)
{
printf("%d",arr[i]);
}
}
void selection_sort(int arr[],int num)
{
int i,j,temp,min;
for(i=0;i<=num-1;i++)
{
min=i;

for(j=i+1;j<=num-1;j++)
/*here in j=i+1 we are adding i to avoid error as we don't need to check for
already sorted number as previously sorted number is kept in the same array */
{
if(arr[j]<arr[i])

min=j;
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
OUTPUT-
Enter the size of array
5

Enter the elements in array one by one
5
4
3
2
1
The sorted array is
12345


Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation