-
Notifications
You must be signed in to change notification settings - Fork 2
/
2-selection_sort.c
55 lines (50 loc) · 1017 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
52
53
54
55
#include "sort.h"
/**
* locate_min - Locatin the min From Current index in the array
* @array: The Array to be Searched
* @index: Starting Index of The Search
* @size: The Size of The Array
* Return: (int) index of min if found or
* same given index if index is the min
*/
int locate_min(int *array, int index, size_t size)
{
int min, idx_min;
int i;
min = array[index];
idx_min = index;
for (i = index; i < (int)size; i++)
{
if (array[i] < min)
{
min = array[i];
idx_min = i;
}
}
if (idx_min == index)
return (-1);
return (idx_min);
}
/**
* selection_sort - Implementation of selection Sort Algrithme
* @array: Array to sort type int *
* @size: The Size of The Given Array
*
* Return: (Void) Sorted Array
*/
void selection_sort(int *array, size_t size)
{
int i;
int min, tmp;
for (i = 0; i < (int)size; i++)
{
min = locate_min(array, i, size);
if (min != -1)
{
tmp = array[i];
array[i] = array[min];
array[min] = tmp;
print_array(array, size);
}
}
}