-
Notifications
You must be signed in to change notification settings - Fork 2
/
isort.c
43 lines (35 loc) · 886 Bytes
/
isort.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#include <errno.h>
#include <getopt.h>
#include "helpers.h"
#include "isort.h"
void insertion_sort(void *array,
unsigned int count,
unsigned int size,
assign_t assign,
swap_t swp,
compare_t cmp)
{
int i, j;
unsigned int t[size/sizeof(unsigned int)+1];
if (count <= 1)
return;
if (2 == count) {
if (cmp(array, array+size) == cmp_result_greater)
swp(array, array+size);
return;
}
for (i=1;i<count;++i) {
assign(t, array+size*i);
for (j=i-1;j>=0 && cmp(array+size*j, t) == cmp_result_greater;--j)
assign(array+size*(j+1), array+size*j);
if (i != j+1)
assign(array+size*(j+1), t);
}
}