Skip to content

Commit 7ad2607

Browse files
committed
added selection sort in c
1 parent 90120ad commit 7ad2607

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Diff for: C/Sorting/SELECTION-SORT/minselection.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//SELECTION SORT
2+
//USING MIN ELEMENT
3+
//DESCENDING SORT
4+
#include <stdio.h>
5+
void selectionsort(int arr[],int n)
6+
{
7+
int min=0;
8+
for(register int i=n-1;i>=0;i--)
9+
{
10+
min=i;
11+
for(register int j=0;j<i;j++)
12+
{
13+
if(arr[min]>arr[j])
14+
{
15+
min=j;
16+
}
17+
}
18+
//swap
19+
int temp = arr[i];
20+
arr[i] =arr[min];
21+
arr[min]=temp;
22+
}
23+
}
24+
int main()
25+
{
26+
printf("Enter the size of the array\n");
27+
int n;
28+
scanf("%d",&n);
29+
int arr[n];
30+
printf("Enter the elements of the array\n");
31+
for(register int i=0;i<n;i++)
32+
{
33+
scanf("%d",&arr[i]);
34+
}
35+
selectionsort(arr,n);
36+
for(register int i=0;i<n;i++)
37+
{
38+
printf("%d ",arr[i]);
39+
}
40+
printf("\n");
41+
}

0 commit comments

Comments
 (0)