Skip to content

Commit 33e7261

Browse files
authored
Selection Sort Program in
In Selection sort, the smallest element is exchanged with the first element of the unsorted list of elements (the exchanged element takes the place where smallest element is initially placed). Then the second smallest element is exchanged with the second element of the unsorted list of elements and so on until all the elements are sorted. In the following C program we have implemented the same logic.
1 parent c5c05c2 commit 33e7261

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Selection Sort Program in

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<stdio.h>
2+
int main(){
3+
/* Here i & j for loop counters, temp for swapping,
4+
* count for total number of elements, number[] to
5+
* store the input numbers in array. You can increase
6+
* or decrease the size of number array as per requirement
7+
*/
8+
int i, j, count, temp, number[25];
9+
10+
printf("How many numbers u are going to enter?: ");
11+
scanf("%d",&count);
12+
13+
printf("Enter %d elements: ", count);
14+
// Loop to get the elements stored in array
15+
for(i=0;i<count;i++)
16+
scanf("%d",&number[i]);
17+
18+
// Logic of selection sort algorithm
19+
for(i=0;i<count;i++){
20+
for(j=i+1;j<count;j++){
21+
if(number[i]>number[j]){
22+
temp=number[i];
23+
number[i]=number[j];
24+
number[j]=temp;
25+
}
26+
}
27+
}
28+
29+
printf("Sorted elements: ");
30+
for(i=0;i<count;i++)
31+
printf(" %d",number[i]);
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)