Skip to content

Commit a608e48

Browse files
committed
selection sort
1 parent afa94b2 commit a608e48

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2-selection_sort.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "sort.h"
2+
/**
3+
* selection_sort - function that sorts through an array using selection sort
4+
* @array: the array to be sorted
5+
* @size: size of the array
6+
*/
7+
void selection_sort(int *array, size_t size)
8+
{
9+
size_t i, j, smallest, temp;
10+
11+
if (size < 2 || array == NULL)
12+
return;
13+
for (i = 0; i < size - 1; i++)
14+
{
15+
smallest = i;
16+
for (j = i + 1; j < size; j++)
17+
if (array[j] < array[smallest])
18+
smallest = j;
19+
if (smallest != i)
20+
{
21+
temp = array[smallest];
22+
array[smallest] = array[i];
23+
array[i] = temp;
24+
print_array(array, size);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)