- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-selection_sort.c
44 lines (38 loc) · 866 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
#include"sort.h"
/**
* selection_sort - sorts an array of integers in ascending order using
* the Selection sort algorithm
*
* @array: the array to sort
* @size: the size of the array
*/
voidselection_sort(int*array, size_tsize)
{
size_ti, j, cur_min;
boolmin_found; /* tracks whether a lesser value is found */
/* there's no need to perform sorting on empty or one-element arrays */
if (array==NULL||size<2)
return;
for (i=0; i<size-1; i++)
{
cur_min=i;
min_found= false;
for (j=i+1; j<size; j++)
{
if (array[j] <array[cur_min])
{
cur_min=j;
min_found= true;
}
}
if (min_found)
{
/* swap the elements */
array[i] ^= array[cur_min];
array[cur_min] ^= array[i];
array[i] ^= array[cur_min];
/* print the current view of the array */
print_array(array, size);
}
}
}