-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.c
More file actions
50 lines (40 loc) · 1.02 KB
/
selection_sort.c
File metadata and controls
50 lines (40 loc) · 1.02 KB
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
45
46
47
48
49
50
#include <stdio.h>
void selectionSort(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
// Start the min element at current position
int min_idx = i;
// Loop through unsorted part of array and find min element
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the min element with the current position
int tmp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = tmp;
}
}
/**
* Prints out an array to std out.
*/
void printArray(int *arr, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d", arr[i]);
if (i < n - 1) {
printf(", ");
}
}
printf("\n\n");
}
int main() {
int input_array[] = {9, 8, 2, 4, 10, 5, 7, 6, 3, 1, 7};
int size = 10;
printf("Before:\n");
printArray(input_array, size);
selectionSort(input_array, size);
printf("After:\n");
printArray(input_array, size);
return 0;
}