-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-selection_sort.c
More file actions
49 lines (41 loc) · 810 Bytes
/
2-selection_sort.c
File metadata and controls
49 lines (41 loc) · 810 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
#include "sort.h"
/**
* swap_int - Function to swap integers in an array
* @ia: The first integer
* @ib: The second integer
* Return: Null
*/
void swap_int(int *ia, int *ib)
{
int num;
num = *ia;
*ia = *ib;
*ib = num;
}
/**
* selection_sort - Function to sort array of integers in ascending order
* using selection sort algorithm
* @array: Pointer to the array of integers
* @size: pointer to the size of the array
* Return: Null
*/
void selection_sort(int *array, size_t size)
{
size_t i, j;
int *idx;
if (array == NULL || size < 2)
return;
for (i = 0; i < size - 1; i++)
{
idx = array + i;
for (j = i + 1; j < size; j++)
{
idx = (array[j] < *idx) ? (array + j) : idx;
}
if ((array + i) != idx)
{
swap_int(array + i, idx);
print_array(array, size);
}
}
}