forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.c
More file actions
44 lines (40 loc) · 828 Bytes
/
selection_sort.c
File metadata and controls
44 lines (40 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Selection Sort implementation in C;
* Author : Felipe Gabriel;
* Input : Array lenght and elements;
* Output : Sorted array elements;
*/
#include <stdio.h>
void selection_sort(int size, int *v){
int i, j, min, aux;
for (i = 0; i < (size-1); i++){
min = i;
for (j = (i+1); j < size; j++) {
if(v[j] < v[min]){
min = j;
}
}
if (v[i] != v[min]) {
aux = v[i];
v[i] = v[min];
v[min] = aux;
}
}
}
int main(){
int size,j;
scanf("%d",&size);
int v[size];
for(j = 0; j < size; j++){
scanf("%d",&v[j]);
}
selection_sort(size,v);
for(j = 0; j < size; j++){
if(j != size-1){
printf("%d ",v[j]);
}
else{
printf("%d\n",v[j]);
}
}
return 0;
}