forked from NITSkmOS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.c
More file actions
35 lines (31 loc) · 709 Bytes
/
Copy pathselection_sort.c
File metadata and controls
35 lines (31 loc) · 709 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
#include <stdio.h> // for printf function
void selection_sort(int[], int);
void print_array(int[], int);
int main() {
int arr[] = {4, 3, 42, 82, 5, 2, 33};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array is: ");
print_array(arr, n);
printf("\n");
return 0;
}
/* Function to print array */
void print_array(int arr[], int n) {
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
}
/* Selection Sort algorithm */
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
int small = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[small])
small = j;
}
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}