-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.c
More file actions
33 lines (31 loc) · 946 Bytes
/
selectionSort.c
File metadata and controls
33 lines (31 loc) · 946 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
//
// Created by ali raz on 6/2/20.
//
#include "swap.h"
#include "selectionSort.h"
void selectionSort(int array[],int length){
/* a[0] to a[aLength-1] is the array to sort */
int i,j;
/* advance the position through the entire array */
/* (could do i < aLength-1 because single element is also min element) */
for (i = 0; i < length-1; i++)
{
/* find the min element in the unsorted a[i .. aLength-1] */
/* assume the min is the first element */
int jMin = i;
/* test against elements after i to find the smallest */
for (j = i+1; j < length; j++)
{
/* if this element is less, then it is the new minimum */
if (array[j] < array[jMin])
{
/* found new minimum; remember its index */
jMin = j;
}
}
if (jMin != i)
{
swap(&array[i], &array[jMin]);
}
}
}