Sfoglia il codice sorgente

Better commenting, should be done now

Lily Carpenter 12 anni fa
parent
commit
b67b9fda7e
2 ha cambiato i file con 20 aggiunte e 2 eliminazioni
  1. BIN
      mysort
  2. 20 2
      mysort.c

BIN
mysort


+ 20 - 2
mysort.c

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