-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-selection_sort.c
More file actions
47 lines (41 loc) · 794 Bytes
/
2-selection_sort.c
File metadata and controls
47 lines (41 loc) · 794 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
#include "sort.h"
/**
* selection_sort - Function to sort array of int w/ selection sort algorithm
* @array: The array
* @size: The array size
*
* Return: None
*/
void selection_sort(int *array, size_t size)
{
int *min_elem;
size_t i, j;
if (array == NULL || size < 2)
return;
for (i = 0; i < size - 1; i++)
{
min_elem = array + i;
for (j = i + 1; j < size; j++)
if (array[j] < *min_elem)
min_elem = array + j;
if ((array + i) != min_elem)
{
swap(array + i, min_elem);
print_array(array, size);
}
}
}
/**
* swap - Function to swap position of 2 elements in list
* @first: Fist element
* @second: Second element
*
* Return: None
*/
void swap(int *first, int *second)
{
int tmp_elem;
tmp_elem = *first;
*first = *second;
*second = tmp_elem;
}