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 arrayn”);
scanf(“%d”,&num);
printf(“nEnter the elements in array one by onen”);
for(i=0;i<num;i++)
{
scanf(“%d”,&arr[i]);
}
selection_sort(arr,num);
printf(“The sorted array isn”);
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

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