aboutsummaryrefslogtreecommitdiff
path: root/sort.c
diff options
context:
space:
mode:
Diffstat (limited to 'sort.c')
-rw-r--r--sort.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/sort.c b/sort.c
new file mode 100644
index 0000000..5584867
--- /dev/null
+++ b/sort.c
@@ -0,0 +1,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;
+}