-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.c
More file actions
48 lines (44 loc) · 804 Bytes
/
Copy pathselection_sort.c
File metadata and controls
48 lines (44 loc) · 804 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
46
47
48
#include "common.h"
static void update(t_sort_state *s)
{
int t;
if (s->sorted)
return ;
if (s->j == s->i)
s->aux[0] = s->i;
s->hl[0] = s->j;
s->hl[1] = s->aux[0];
s->hl_count = 2;
s->comparisons++;
if (s->values[s->j] < s->values[s->aux[0]])
s->aux[0] = s->j;
s->j++;
if (s->j >= s->size)
{
if (s->aux[0] != s->i)
{
t = s->values[s->i];
s->values[s->i] = s->values[s->aux[0]];
s->values[s->aux[0]] = t;
s->swaps++;
}
s->i++;
s->j = s->i;
if (s->i >= s->size - 1)
{
s->sorted = true;
s->hl_count = 0;
}
}
}
void register_selection_sort(void)
{
t_algorithm algo;
algo.name = "Selection Sort";
algo.category = "Comparison";
algo.update = update;
algo.reset = NULL;
algo.stable = false;
algo.complexity = "O(n²)";
register_algorithm(algo);
}