aboutsummaryrefslogtreecommitdiff
path: root/insertsort.c
blob: 1a30a1a05d33b03b28578f64c3406338a57f112f (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "util.h"

typedef int T;

void
insertsort(T a[], size_t n)
{
	for ( size_t i = 1; i < n; ++i ) {
		size_t  j     = i;
		const T value = a[i];

		for ( ; j > 0 && a[j - 1] > value; --j )
			a[j] = a[j - 1];

		a[j] = value;
	}
}

int
main(void)
{
	T a[10];

	srand(time(NULL));
	for ( size_t i = 0; i != NELEM(a); ++i )
		a[i] = rand() % 100;

	insertsort(a, NELEM(a));

	for ( size_t i = 0; i != NELEM(a); ++i )
		printf("%d ", a[i]);
	putchar('\n');
	return EXIT_SUCCESS;
}