Skip to content

Commit 285a117

Browse files
committed
selection sort
1 parent 9ea2245 commit 285a117

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

2-selection_sort.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "sort.h"
2+
3+
/**
4+
* selection_sort - Sorts an array using the selection sort algorithm.
5+
* @array: The array to sort.
6+
* @size: The length of the array.
7+
*/
8+
void selection_sort(int *array, size_t size)
9+
{
10+
size_t i, j, low_index;
11+
int temp;
12+
13+
if (array != NULL)
14+
{
15+
for (i = 0; i < size - 1; i++)
16+
{
17+
low_index = i;
18+
for (j = size - 1; j > i; j--)
19+
{
20+
if (array[j] < array[low_index])
21+
{
22+
low_index = j;
23+
} /* End if */
24+
} /* End for */
25+
if (i != low_index)
26+
{
27+
/* Swapping */
28+
temp = array[i];
29+
array[l] = array[low_index];
30+
array[low_index] = temp;
31+
print_array(array, size);
32+
} /* End if */
33+
} /* End for */
34+
} /* End if */
35+
} /* End function */

0 commit comments

Comments
 (0)