Skip to content

Commit 90120ad

Browse files
committed
added selection sort in c using max element descending
1 parent 251240e commit 90120ad

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

C/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Using max element](Sorting/SELECTION-SORT/selection.c)
2929
* [Using min element](Sorting/SELECTION-SORT/selectionsort.c)
3030
* DESCENDING
31-
* [Using max element]()
31+
* [Using max element](Sorting/SELECTION-SORT/maxselection.c)
3232
* [Using min element]()
3333

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

0 commit comments

Comments
 (0)