Skip to content

Commit ac0085d

Browse files
authored
Create selection_sort.c
1 parent ec78dc5 commit ac0085d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

sort/selection_sort.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Selection Sort
2+
3+
#include<stdio.h>
4+
int main(){
5+
6+
int s,i,j,temp,a[20];
7+
8+
printf("Enter total elements: "); // total no of elements
9+
scanf("%d",&s);
10+
11+
printf("Enter %d elements: ",s); // the elements
12+
for(i=0;i<s;i++)
13+
scanf("%d",&a[i]);
14+
15+
for(i=0;i<s;i++){
16+
for(j=i+1;j<s;j++){
17+
if(a[i]>a[j]){
18+
temp=a[i]; // Compare between 2 consecutive elements and swap them if they are not in ascending order
19+
a[i]=a[j];
20+
a[j]=temp;
21+
}
22+
}
23+
}
24+
25+
printf("After sorting is: ");
26+
for(i=0;i<s;i++) // Print the sorted array
27+
printf(" %d",a[i]);
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)