-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-selection_sort.c
More file actions
54 lines (45 loc) · 828 Bytes
/
2-selection_sort.c
File metadata and controls
54 lines (45 loc) · 828 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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "sort.h"
/**
* min - search the minimun element
* @array: The array to be printed
* @size: Number of elements in @array
*
* Return: address of the minimum item of a array
*/
int *min(int *array, size_t size)
{
int *min_mul = &array[0];
while (size--)
{
if (array[size] < *min_mul)
{
min_mul = &array[size];
}
}
return (min_mul);
}
/**
* selection_sort - sort array by selection
* @array: The array to be printed
* @size: Number of elements in @array
*
* Return: void
*/
void selection_sort(int *array, size_t size)
{
size_t i;
if (!array || size < 2)
return;
for (i = 0; i < size; i++)
{
int *min_mul = min(&array[i], size - i);
int tmp = 0;
if (&array[i] != min_mul)
{
tmp = array[i];
array[i] = *min_mul;
*min_mul = tmp;
print_array(array, size);
}
}
}