Преглед на файлове

Better commenting, should be done now

Lily Carpenter преди 12 години
родител
ревизия
b67b9fda7e
променени са 2 файла, в които са добавени 20 реда и са изтрити 2 реда
  1. BIN
      mysort
  2. 20 2
      mysort.c

BIN
mysort


+ 20 - 2
mysort.c

@@ -3,6 +3,7 @@
3 3
 #include <stdio.h>
4 4
 #include <fcntl.h>
5 5
 
6
+// Resizes array a using realloc
6 7
 unsigned int *resize_array(unsigned int *a, size_t new_size)
7 8
 {
8 9
     unsigned int *save;
@@ -16,12 +17,14 @@ unsigned int *resize_array(unsigned int *a, size_t new_size)
16 17
     return save;
17 18
 }
18 19
 
20
+// Swaps the values of a and b
19 21
 void swap(unsigned int *a, unsigned int *b){
20 22
     unsigned int old_a = *a;
21 23
     *a = *b;
22 24
     *b = old_a;
23 25
 }
24 26
 
27
+// Creates a partition for use by quicksort
25 28
 int partition(unsigned int *array, int left, int right, int pivot){
26 29
     unsigned int pivot_value = array[pivot];
27 30
     swap(&array[pivot], &array[right]);
@@ -38,6 +41,7 @@ int partition(unsigned int *array, int left, int right, int pivot){
38 41
     return storeIndex;
39 42
 }
40 43
 
44
+// Using partition to sort `array`
41 45
 void quicksort(unsigned int *array, int left, int right){
42 46
     if (left < right){
43 47
         int pivot = right;
@@ -50,28 +54,42 @@ void quicksort(unsigned int *array, int left, int right){
50 54
 
51 55
 int main()
52 56
 {
53
-    int temp_size = 50;
57
+    const int temp_size = 50; // How many numbers to load at once?
54 58
     int size = 1;
59
+
60
+    // Holds all numbers we need to sort
55 61
     unsigned int *numbers = malloc(size * sizeof *numbers);
62
+
63
+    // Holds the numbers recently pulled from the stream
56 64
     unsigned int *temp = malloc(temp_size * sizeof *temp);
57
-    int ret_val;
65
+
66
+    int ret_val; // Holds the return value of fread so we know how many numbers were read
58 67
 
59 68
     /// Loop through stdin stream grabbing `temp_size` numbers each iteration
60 69
     while ((ret_val = fread(temp, sizeof *temp, temp_size, stdin))){
61 70
         // Should not happen if EOF has been reached/we have loaded all data
62 71
         if (ret_val != 0){
72
+
73
+            // This looks complicated, just the math required to figure the new size of `numbers`
63 74
             int new_size = size - 1 + ret_val;
75
+
76
+            // Actually resize array
64 77
             numbers = resize_array(numbers, new_size * sizeof *numbers);
65 78
 
79
+            // Go from size to new_size, putting the numbers from `temp` into `numbers`
66 80
             int temp_count = 0;
67 81
             for(int i = size - 1; i < new_size; i++){
68 82
                 numbers[i] = temp[temp_count++];
69 83
             }
84
+
85
+            // Update `size`, now that we are done with it's previous value
70 86
             size = new_size;
71 87
         }
72 88
     }
73 89
 
90
+
74 91
     quicksort(numbers, 0, size - 1);
92
+
75 93
     for (int i = 0; i < size; i++){
76 94
         printf("%u\n", numbers[i]);
77 95
     }