-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-selection_sort.c
More file actions
45 lines (39 loc) · 819 Bytes
/
2-selection_sort.c
File metadata and controls
45 lines (39 loc) · 819 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
#include "sort.h"
/**
* swap_ints - Swap two integers in an array.
* @a: The first integer to swap.
* @b: The second integer to swap.
*/
void swap_ints(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* selection_sort - Sort an array of integers in ascending order
* using the selection sort algorithm.
* @array: An array of integers.
* @size: The size of the array.
*
* Description: Prints the array after each swap.
*/
void selection_sort(int *array, size_t size)
{
int *min;
size_t i, j;
if (array == NULL || size < 2)
return;
for (i = 0; i < size - 1; i++)
{
min = array + i;
for (j = i + 1; j < size; j++)
min = (array[j] < *min) ? (array + j) : min;
if ((array + i) != min)
{
swap_ints(array + i, min);
print_array(array, size);
}
}
}