forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.c
More file actions
41 lines (35 loc) · 724 Bytes
/
SelectionSort.c
File metadata and controls
41 lines (35 loc) · 724 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 array[], int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
void selection_sort(int array[], int n)
{
int min, i, j;
for (i = 0; i < n; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (array[min] > array[j])
{
min = j;
}
}
if (min != i)
swap(array, min, i);
}
}
int main()
{
int array_size = 10;
int array[10] = {45, 7, 125, 18, 3, 5, 11, 107, 60, 4};
selection_sort(array, array_size);
printf("Sorted Array:\n");
int i;
for (i = 0; i < array_size; i++)
printf("%d ", array[i]);
return 0;
}