aboutsummaryrefslogtreecommitdiff
path: root/sort.c
blob: 5584867ac63c0700cf47fa3edf296f1e86b0605a (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>

#define _OUTPUT

int main(int argc, char* argv[])
{		
	int count = 1000;
	int *list;
	int swaps = 0;
	int runs = 0;
	int biggest = 0;
	int smallest = 0;

	printf("Random list sorting\nLenght of List:%i \n\n", count);

	list = malloc(sizeof(int) * count);
	
	srandom(list); 

	for(int i = 0; i < count; i++)
	{
		list[i] = random()/361913;
#ifdef _OUTPUT
		printf("%i,", list[i]);
#endif
	}

	printf("\n==========Sorting==========\n");

	do {
		swaps = 0;
		runs++;
		for( int i = 1; i < count; i++ )
		{
			if(list[i - 1] > list [i])
			{
				int tmp = list[i - 1];
				list[i - 1] = list[i];
				list[i] = tmp;
				swaps++;
			}
		}
	} while(swaps);	

#ifdef _OUTPUT
	for(int i = 0; i < count; i++)
	{
		printf("%i,",list[i]);
	}
#endif

	printf("\n\nNeeded runs: %i\n", runs);
	free(list);
	return 0;
}