changeset 6:7e3fab27a577

add source unfinished quick_sort
author Taiki TAIRA <e095767@ie.u-ryukyu.ac.jp>
date Wed, 08 Aug 2012 18:50:01 +0900
parents 77c6ae852e6c
children 2698082de0ea
files quick_sort/Makefile quick_sort/quick_sort quick_sort/quick_sort.c quick_sort/quick_sort.cbc
diffstat 4 files changed, 282 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quick_sort/Makefile	Wed Aug 08 18:50:01 2012 +0900
@@ -0,0 +1,15 @@
+CBC = cbc-gcc-4.6.0
+
+CBCFLAGS = -O2 \
+		   -Wall \
+		   -g \
+		   -o
+
+TARGET = quick_sort
+
+$(TARGET): quick_sort.cbc
+	cp quick_sort.c quick_sort.cbc
+	$(CBC) $(CBCFLAGS) $(TARGET) quick_sort.cbc
+
+clean:
+	rm quick_sort
Binary file quick_sort/quick_sort has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quick_sort/quick_sort.c	Wed Aug 08 18:50:01 2012 +0900
@@ -0,0 +1,133 @@
+/* 
+ * This program is quick sort.
+ * move low value to low subscript, and high value to high subscript.
+ *
+ *
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+enum SetValue
+{
+    ARRAY_MAX = 20,
+    COUNT_START_VALUE = 0,
+};
+
+struct array_state {
+    int *int_array_p;
+    int *divide_index;
+    int index_num;
+    int array_length;
+    int upper_count;
+    int lower_count;
+    int divide_count;
+    int base_value;
+    int low_tmp;
+    int up_tmp;
+};
+
+__code search_lower_array(struct array_state *as_p);
+__code manage_array(struct array_state *as_p);
+
+__code change_tmp_num(struct array_state *as_p)
+{
+    int tmp = *(as_p->int_array_p + as_p->upper_count);
+    *(as_p->int_array_p + as_p->upper_count) = *(as_p->int_array_p + as_p->lower_count);
+    *(as_p->int_array_p + as_p->lower_count) = tmp;
+    if (as_p->upper_count > as_p->lower_count) {
+        goto search_lower_array(as_p);
+    } else {
+        as_p->index_num++;
+        goto manage_array(as_p);
+    }
+}
+
+__code search_upper_array(struct array_state *as_p)
+{
+    if (as_p->base_value < *(as_p->int_array_p + as_p->upper_count)) {
+        as_p->upper_count--;
+        goto search_lower_array(as_p);
+    } else {
+        goto change_tmp_num(as_p);
+    }
+}
+
+
+__code search_lower_array(struct array_state *as_p)
+{
+    if (as_p->base_value < *(as_p->int_array_p + as_p->lower_count)) {
+        as_p->lower_count++;
+        goto search_upper_array(as_p);
+    } else {
+        search_lower_array(as_p);
+    }
+}
+
+__code manage_array(struct array_state *as_p)
+{
+    if (as_p->index_num!=0) {
+        as_p->divide_index[as_p->index_num] = as_p->upper_count; 
+        as_p->base_value = as_p->base_value/2;
+    } 
+    goto search_lower_array(as_p);
+}
+
+__code set_base_value(int *int_array_p)
+{
+    
+    int base_value = *(int_array_p + (ARRAY_MAX / 2));
+    int divide_index[100];
+    struct array_state *as_p = malloc(sizeof(struct array_state));
+    as_p->int_array_p = int_array_p;
+    as_p->index_num = 0;
+    divide_index[as_p->index_num] = 0;
+    as_p->divide_index = divide_index;
+    as_p->upper_count = ARRAY_MAX;
+    as_p->lower_count = COUNT_START_VALUE;
+    as_p->base_value = base_value;
+    as_p->array_length = ARRAY_MAX;
+
+    goto manage_array(as_p);
+}
+
+__code print_value(int *int_array_p, int count)
+{
+    if (count < ARRAY_MAX) {
+        count++;
+        printf("[%d] %d \n", count, *(int_array_p + count));
+        goto print_value(int_array_p, count);
+    } else {
+        printf("\n");
+        goto set_base_value(int_array_p);
+    }
+
+}
+
+__code init_random(int *int_array_p, int count)
+{
+    if (count < ARRAY_MAX) {
+        *(int_array_p + count) = (int)(rand() * (ARRAY_MAX + 1.0)/(RAND_MAX + 1.0));
+        count++;
+        goto init_random(int_array_p, count); 
+    } else {
+        goto print_value(int_array_p, COUNT_START_VALUE);
+    }
+}
+
+
+void
+make_array()
+{
+    int *int_array_p = malloc(ARRAY_MAX * sizeof(int));
+    srand((unsigned int)time(NULL));
+    goto init_random(int_array_p, COUNT_START_VALUE);
+}
+
+int
+main()
+{
+    make_array(); 
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quick_sort/quick_sort.cbc	Wed Aug 08 18:50:01 2012 +0900
@@ -0,0 +1,134 @@
+/* 
+ * This program is quick sort.
+ * move low value to low subscript, and high value to high subscript.
+ *
+ *
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+enum SetValue
+{
+    ARRAY_MAX = 20,
+    COUNT_START_VALUE = 0,
+};
+
+struct array_state {
+    int *int_array_p;
+    int *divide_index;
+    int index_num;
+    int array_length;
+    int upper_count;
+    int lower_count;
+    int divide_count;
+    int base_value;
+    int low_tmp;
+    int up_tmp;
+};
+
+__code search_lower_array(struct array_state *as_p);
+__code manage_array(struct array_state *as_p);
+
+__code change_tmp_num(struct array_state *as_p)
+{
+    int tmp = *(as_p->int_array_p + as_p->upper_count);
+    *(as_p->int_array_p + as_p->upper_count) = *(as_p->int_array_p + as_p->lower_count);
+    *(as_p->int_array_p + as_p->lower_count) = tmp;
+    if (as_p->upper_count > as_p->lower_count) {
+        goto search_lower_array(as_p);
+    } else {
+        as_p->index_num++;
+        goto manage_array(as_p);
+    }
+}
+
+__code search_upper_array(struct array_state *as_p)
+{
+    if (as_p->base_value < *(as_p->int_array_p + as_p->upper_count)) {
+        as_p->upper_count--;
+        goto search_lower_array(as_p);
+    } else {
+        goto change_tmp_num(as_p);
+    }
+}
+
+
+__code search_lower_array(struct array_state *as_p)
+{
+    if (as_p->base_value < *(as_p->int_array_p + as_p->lower_count)) {
+        as_p->lower_count++;
+        goto search_upper_array(as_p);
+    } else {
+        search_lower_array(as_p);
+    }
+}
+
+__code manage_array(struct array_state *as_p)
+{
+    if (as_p->index_num!=0) {
+        as_p->divide_index[as_p->index_num] = as_p->upper_count; 
+        as_p->base_value = as_p->base_value/2;
+        
+    } 
+    goto search_lower_array(as_p);
+}
+
+__code set_base_value(int *int_array_p)
+{
+    
+    int base_value = *(int_array_p + (ARRAY_MAX / 2));
+    int divide_index[100];
+    struct array_state *as_p = malloc(sizeof(struct array_state));
+    as_p->int_array_p = int_array_p;
+    as_p->index_num = 0;
+    divide_index[as_p->index_num] = 0;
+    as_p->divide_index = divide_index;
+    as_p->upper_count = ARRAY_MAX;
+    as_p->lower_count = COUNT_START_VALUE;
+    as_p->base_value = base_value;
+    as_p->array_length = ARRAY_MAX;
+
+    goto manage_array(as_p);
+}
+
+__code print_value(int *int_array_p, int count)
+{
+    if (count < ARRAY_MAX) {
+        count++;
+        printf("[%d] %d \n", count, *(int_array_p + count));
+        goto print_value(int_array_p, count);
+    } else {
+        printf("\n");
+        goto set_base_value(int_array_p);
+    }
+
+}
+
+__code init_random(int *int_array_p, int count)
+{
+    if (count < ARRAY_MAX) {
+        *(int_array_p + count) = (int)(rand() * (ARRAY_MAX + 1.0)/(RAND_MAX + 1.0));
+        count++;
+        goto init_random(int_array_p, count); 
+    } else {
+        goto print_value(int_array_p, COUNT_START_VALUE);
+    }
+}
+
+
+void
+make_array()
+{
+    int *int_array_p = malloc(ARRAY_MAX * sizeof(int));
+    srand((unsigned int)time(NULL));
+    goto init_random(int_array_p, COUNT_START_VALUE);
+}
+
+int
+main()
+{
+    make_array(); 
+    return 0;
+}