-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.c
More file actions
43 lines (34 loc) · 756 Bytes
/
Copy pathselection_sort.c
File metadata and controls
43 lines (34 loc) · 756 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
#include <stdio.h>
void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void selection_sort(int unsort[], int n, int stop){
int minimum;
for (int i = 0; i < n - 1; i++){
minimum=i;
for (int j = i + 1; j < n ; j++){
if (unsort[j] < unsort[minimum]) {
minimum = j;
}
}
swap(&unsort[minimum],&unsort[i]);
if (stop-1 == i) break;
}
}
int main(){
int arr_size, state;
scanf("%d %d", &arr_size,&state);
int arr[arr_size];
int minimum;
for (int i = 0 ; i < arr_size ; i++){
scanf("%d",&arr[i]);
}
selection_sort(arr,arr_size,state);
for (int i = 0 ; i < arr_size ; i++){
printf("%d ",arr[i]);
}
return 0;
}