SELECTION SORT
SELECTION SORT
ALGORITHM:-
STEP 1. REPEAT STEP 2 & 3 FOR K=1,2...N-1
STEP 2. MIN(A,K,N,LOC)
STEP 3. SET TEMP:=A[K],A[K]:=A[LOC] AND A[LOC];=TEMP
STEP 4. EXIT
CODE:-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,arr[20],min=0,temp=0;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\n");
for(i=0; i<n; i++)
{
printf("enter array element : ");
scanf("%d",&arr[i]);
}
printf("\nThe Unsorted array you entered : \n");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
printf("\nThe Sorted array we got using Selection sort : \n");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getch();
return 0;
}
Comments
Post a Comment
Kaushikmadhav77@gmail.com