Explorar o código

Got a (pretty ugly looking) version of quicksort working. Checked for memory leaks, all good. Want to clean this up and probably make it an "in place" algorithm as well.

Lily Carpenter %!s(int64=12) %!d(string=hai) anos
pai
achega
b83cb9674a
Modificáronse 3 ficheiros con 61 adicións e 13 borrados
  1. BIN=BIN
      myecho
  2. BIN=BIN
      mysort
  3. 61 13
      mysort.c

BIN=BIN
myecho


BIN=BIN
mysort


+ 61 - 13
mysort.c

@@ -1,39 +1,87 @@
1
-void swap(int* a, int* b)
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)
2 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
+    }
3 15
 
16
+    return save;
4 17
 }
5 18
 
6
-int* quicksort(int* array, size_t size)
19
+int *quicksort(int *array, size_t size)
7 20
 {
8 21
     int lesser[size], less_counter = 0;
9 22
     int greater[size], great_counter = 0;
10 23
 
11
-    if (size <= 1)
24
+    if (size < 2)
12 25
         return array;
13 26
 
14 27
     // Take last element and make it the pivot, then subtract one
15 28
     // from size afterwards so that it will no longer be accesssed
16
-    int pivot = array[size--];
29
+    int pivot = array[size - 1];
30
+    size--;
17 31
 
18
-    for(int i = 0; i < size; i++){
19
-        if (array[i] <= pivot)
32
+    for(unsigned int i = 0; i < size; i++){
33
+        if (array[i] <= pivot){
20 34
             lesser[less_counter++] = array[i];
21
-        else
35
+        }
36
+        else {
22 37
             greater[great_counter++] = array[i];
38
+        }
23 39
     }
24 40
 
25
-    greater = quicksort(greater, great_counter + 1);
26
-    lesser = quicksort(lesser, less_counter + 1);
41
+    int *lesser_sorted = quicksort(lesser, (size_t)less_counter);
42
+    int *greater_sorted = quicksort(greater, (size_t)great_counter);
27 43
 
28 44
     int main_counter;
29
-    for(main_counter = 0; main_counter <= less_counter; main_counter++){
30
-        array[main_counter] = lesser[main_counter];
45
+    for(main_counter = 0; main_counter < less_counter; main_counter++){
46
+        array[main_counter] = lesser_sorted[main_counter];
31 47
     }
32 48
 
33
-}
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
+    }
34 55
 
56
+    return array;
57
+}
35 58
 
36
-void main()
59
+int main()
37 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--;
38 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;
39 86
 }
87
+