We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ec78dc5 commit ac0085dCopy full SHA for ac0085d
sort/selection_sort.c
@@ -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