forked from Ella711/sorting_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-selection_sort.c
52 lines (45 loc) · 870 Bytes
/
2-selection_sort.c
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
51
#include "sort.h"
/**
* swap - Swap values of start and next index.
* @array: the array to work in.
* @start: start point of the array.
* @min: value to swap with start point.
* Return: nothing.
*/
void swap(int *array, size_t start, size_t min)
{
int a, b;
a = array[start];
b = array[min];
array[start] = b;
array[min] = a;
}
/**
* selection_sort - Sort an array with selection algorithm.
* @array: the array to sort.
* @size: size of the array.
* Return: nothing.
*/
void selection_sort(int *array, size_t size)
{
size_t index = 0, start = 0, min = 0;
if (!array || size < 2)
return;
while (start < size)
{
min = start;
index = start + 1;
while (index < size)
{
if (array[index] < array[min])
min = index;
index++;
}
if (min != start)
{
swap(array, start, min);
print_array(array, size);
}
start++;
}
}