add bubblesort
authorJens Kasten <jens@kasten-edv.de>
Thu, 03 Mar 2011 15:48:04 +0100
changeset 1 18e7503af60f
parent 0 8da0314dad65
child 2 803affa7a5b4
add bubblesort
bubblesort/Makefile
bubblesort/bubblesort.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bubblesort/Makefile	Thu Mar 03 15:48:04 2011 +0100
@@ -0,0 +1,19 @@
+IDIR="/usr/src/linux/include/rsbac"
+CC=gcc
+CFLAGS=-I$(IDIR)
+
+ODIR=obj
+
+LIBS=-lrsbac
+OBJ=bubblesort.o
+
+bubblesort: $(OBJ)
+	gcc -o $@ $^ $(CFLAGS) $(LIBS)
+
+.PHONY: clean
+
+clean: 
+	rm -f *.o bubblesort
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bubblesort/bubblesort.c	Thu Mar 03 15:48:04 2011 +0100
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define MAX_RAND 40
+
+void create_random_pool(int *prandom_pool);
+void show_random_pool(int *prandom_pool);
+void sort_random_pool_to_less(int *prandom_pool);
+void sort_random_poot_to_greater(int *prandom_pool);
+
+int main(void)
+{
+    int random_pool[MAX_RAND+1];
+    int *prandom_pool;
+    int input_number, quit = 0;
+
+    prandom_pool = random_pool;
+
+    create_random_pool(prandom_pool);
+    show_random_pool(prandom_pool);
+
+    printf("Change a random amount of numbers to:\n");
+    while (quit == 0) {
+        printf("Choose an action:\n");
+        printf("1) sort from less to greater\n");
+        printf("2) sort from greater to less\n");
+        printf("3) quit program\n");
+        scanf("%d", &input_number);
+    
+        switch (input_number) {
+            case 1: sort_random_pool_to_greater(prandom_pool); 
+                    show_random_pool(prandom_pool); break;
+            case 2: sort_random_pool_to_less(prandom_pool); 
+                    show_random_pool(prandom_pool); break;
+            case 3: quit = 1; break;
+        }
+    }    
+
+    return 0;
+}    
+
+void sort_random_pool_to_less(int *prandom_pool)
+{
+    int i, j, temp;
+
+    for (i=0; i < MAX_RAND; i++) {
+        for (j=0; j < MAX_RAND; j++) {
+            if (prandom_pool[i] > prandom_pool[j]) {
+                temp = prandom_pool[j];
+                prandom_pool[j] = prandom_pool[i];
+                prandom_pool[i] = temp;
+            }
+        }      
+    }
+}
+
+void sort_random_pool_to_greater(int *prandom_pool)
+{
+    int i, j, temp;
+    
+    for (i=MAX_RAND-1; i >= 0; i--) {
+        for (j=MAX_RAND-1; j >= 0; j--) {
+            if (prandom_pool[i] > prandom_pool[j]) {
+                temp = prandom_pool[j];
+                prandom_pool[j] = prandom_pool[i];
+                prandom_pool[i] = temp;
+            }    
+        }
+    }
+}
+    
+void create_random_pool(int *prandom_pool)
+{
+    int i;
+   
+    srand(time(NULL));
+    for (i=0; i < MAX_RAND; i++) {
+        prandom_pool[i] = rand() % 100;
+    }
+}
+
+void show_random_pool(int *prandom_pool)
+{
+    int i;
+     
+    for (i=0; i < MAX_RAND; i++) {
+        printf("%d ", prandom_pool[i]);
+    }        
+    printf("\n");
+}        
+