ソースを参照

Removed old quicksort, it was seg faulting after about 320,000 numbers and the new one is cleaner anyway

Lily Carpenter 12 年 前
コミット
2b4a9be616
共有3 個のファイルを変更した80 個の追加87 個の削除を含む
  1. BIN
      mysort
  2. 80 0
      mysort.c
  3. 0 87
      mysort_simple.c

+ 80 - 0
mysort.c

@@ -0,0 +1,80 @@
1
+#include <stddef.h>
2
+#include <stdlib.h>
3
+#include <stdio.h>
4
+#include <fcntl.h>
5
+
6
+int counter;
7
+
8
+int *resize_array(int *a, size_t new_size)
9
+{
10
+    int *save;
11
+    save = realloc(a, new_size);
12
+
13
+    if (save == NULL) {
14
+        fprintf(stderr, "Memory exhuasted\n");
15
+        exit(EXIT_FAILURE);
16
+    }
17
+
18
+    return save;
19
+}
20
+
21
+void swap(int *a, int *b){
22
+    int old_a = *a;
23
+    *a = *b;
24
+    *b = old_a;
25
+}
26
+
27
+int partition(int *array, int left, int right, int pivot){
28
+    int pivot_value = array[pivot];
29
+    swap(&array[pivot], &array[right]);
30
+    int storeIndex = left;
31
+    for(int i = left; i < right; i++){
32
+        if (array[i] < pivot_value){
33
+            swap(&array[i], &array[storeIndex]);
34
+            storeIndex++;
35
+        }
36
+    }
37
+
38
+    swap(&array[storeIndex], &array[right]);
39
+
40
+    return storeIndex;
41
+}
42
+
43
+void quicksort(int *array, int left, int right){
44
+    if (left < right){
45
+        int pivot = right;
46
+
47
+        pivot = partition(array, left, right, pivot);
48
+        quicksort(array, left, pivot - 1);
49
+        quicksort(array, pivot + 1, right);
50
+    }
51
+}
52
+
53
+int main()
54
+{
55
+    int size = 1;
56
+    int *numbers = malloc(size * sizeof *numbers);
57
+    int *temp = malloc(size * sizeof *temp);
58
+    int error;
59
+
60
+    /// Loop through stdin stream grabbing `size` numbers each iteration
61
+    while ((error = fread(temp, sizeof *numbers, 1, stdin))){
62
+        // Should not happen if EOF has been reached or we have loaded all data
63
+        // May happen if we reached EOF at exactly size [didn't find clear documention], in which case next iteration should catch it
64
+        if (error != 0){
65
+            numbers[size - 1] = temp[0];
66
+            size++;
67
+            numbers = resize_array(numbers, size * sizeof *numbers);
68
+        }
69
+    }
70
+    size--;
71
+
72
+    quicksort(numbers, 0, size - 1);
73
+    for (int i = 0; i < size; i++){
74
+        printf("%i\n", numbers[i]);
75
+    }
76
+
77
+    free(temp);
78
+    free(numbers);
79
+    return 0;
80
+}

+ 0 - 87
mysort_simple.c

@@ -1,87 +0,0 @@
1
-#include <stddef.h>
2
-#include <stdlib.h>
3
-#include <stdio.h>
4
-#include <fcntl.h>
5
-
6
-int *resize_array(int *a, size_t new_size)
7
-{
8
-    int *save;
9
-    save = realloc(a, new_size);
10
-
11
-    if (save == NULL) {
12
-        fprintf(stderr, "Memory exhuasted\n");
13
-        exit(EXIT_FAILURE);
14
-    }
15
-
16
-    return save;
17
-}
18
-
19
-int *quicksort(int *array, size_t size)
20
-{
21
-    int lesser[size], less_counter = 0;
22
-    int greater[size], great_counter = 0;
23
-
24
-    if (size < 2)
25
-        return array;
26
-
27
-    // Take last element and make it the pivot, then subtract one
28
-    // from size afterwards so that it will no longer be accesssed
29
-    int pivot = array[size - 1];
30
-    size--;
31
-
32
-    for(unsigned int i = 0; i < size; i++){
33
-        if (array[i] <= pivot){
34
-            lesser[less_counter++] = array[i];
35
-        }
36
-        else {
37
-            greater[great_counter++] = array[i];
38
-        }
39
-    }
40
-
41
-    int *lesser_sorted = quicksort(lesser, (size_t)less_counter);
42
-    int *greater_sorted = quicksort(greater, (size_t)great_counter);
43
-
44
-    int main_counter;
45
-    for(main_counter = 0; main_counter < less_counter; main_counter++){
46
-        array[main_counter] = lesser_sorted[main_counter];
47
-    }
48
-
49
-    array[main_counter++] = pivot;
50
-
51
-    for (int i = 0; i < great_counter; i++){
52
-        array[main_counter] = greater_sorted[i];
53
-        main_counter++;
54
-    }
55
-
56
-    return array;
57
-}
58
-
59
-int main()
60
-{
61
-    int size = 1;
62
-    int *numbers = malloc(size * sizeof *numbers);
63
-    int *temp = malloc(size * sizeof *temp);
64
-    int error;
65
-
66
-    /// Loop through stdin stream grabbing `size` numbers each iteration
67
-    while ((error = fread(temp, sizeof *numbers, 1, stdin))){
68
-        // Should not happen if EOF has been reached or we have loaded all data
69
-        // May happen if we reached EOF at exactly size [didn't find clear documention], in which case next iteration should catch it
70
-        if (error != 0){
71
-            numbers[size - 1] = temp[0];
72
-            size++;
73
-            numbers = resize_array(numbers, size * sizeof *numbers);
74
-        }
75
-    }
76
-    size--;
77
-
78
-    int *sorted_numbers = quicksort(numbers, size);
79
-    for (int i = 0; i < size; i++){
80
-        printf("%i\n", sorted_numbers[i]);
81
-    }
82
-
83
-    free(temp);
84
-    free(sorted_numbers);
85
-    return 0;
86
-}
87
-