Explorar el Código

Fixed this to use unsigned integers, cleaned up the fread part a bit.

Lily Carpenter hace 12 años
padre
commit
eb36717f8e
Se han modificado 2 ficheros con 23 adiciones y 19 borrados
  1. BIN
      mysort
  2. 23 19
      mysort.c

BIN
mysort


+ 23 - 19
mysort.c

5
 
5
 
6
 int counter;
6
 int counter;
7
 
7
 
8
-int *resize_array(int *a, size_t new_size)
8
+unsigned int *resize_array(unsigned int *a, size_t new_size)
9
 {
9
 {
10
-    int *save;
10
+    unsigned int *save;
11
     save = realloc(a, new_size);
11
     save = realloc(a, new_size);
12
 
12
 
13
     if (save == NULL) {
13
     if (save == NULL) {
18
     return save;
18
     return save;
19
 }
19
 }
20
 
20
 
21
-void swap(int *a, int *b){
22
-    int old_a = *a;
21
+void swap(unsigned int *a, unsigned int *b){
22
+    unsigned int old_a = *a;
23
     *a = *b;
23
     *a = *b;
24
     *b = old_a;
24
     *b = old_a;
25
 }
25
 }
26
 
26
 
27
-int partition(int *array, int left, int right, int pivot){
28
-    int pivot_value = array[pivot];
27
+int partition(unsigned int *array, int left, int right, int pivot){
28
+    unsigned int pivot_value = array[pivot];
29
     swap(&array[pivot], &array[right]);
29
     swap(&array[pivot], &array[right]);
30
     int storeIndex = left;
30
     int storeIndex = left;
31
     for(int i = left; i < right; i++){
31
     for(int i = left; i < right; i++){
40
     return storeIndex;
40
     return storeIndex;
41
 }
41
 }
42
 
42
 
43
-void quicksort(int *array, int left, int right){
43
+void quicksort(unsigned int *array, int left, int right){
44
     if (left < right){
44
     if (left < right){
45
         int pivot = right;
45
         int pivot = right;
46
 
46
 
52
 
52
 
53
 int main()
53
 int main()
54
 {
54
 {
55
+    int temp_size = 50;
55
     int size = 1;
56
     int size = 1;
56
-    int *numbers = malloc(size * sizeof *numbers);
57
-    int *temp = malloc(size * sizeof *temp);
58
-    int error;
57
+    unsigned int *numbers = malloc(size * sizeof *numbers);
58
+    unsigned int *temp = malloc(temp_size * sizeof *temp);
59
+    int ret_val;
59
 
60
 
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);
61
+    /// Loop through stdin stream grabbing `temp_size` numbers each iteration
62
+    while ((ret_val = fread(temp, sizeof *temp, temp_size, stdin))){
63
+        // Should not happen if EOF has been reached/we have loaded all data
64
+        if (ret_val != 0){
65
+            int new_size = size - 1 + ret_val;
66
+            numbers = resize_array(numbers, new_size * sizeof *numbers);
67
+
68
+            int temp_count = 0;
69
+            for(int i = size - 1; i < new_size; i++){
70
+                numbers[i] = temp[temp_count++];
71
+            }
72
+            size = new_size;
68
         }
73
         }
69
     }
74
     }
70
-    size--;
71
 
75
 
72
     quicksort(numbers, 0, size - 1);
76
     quicksort(numbers, 0, size - 1);
73
     for (int i = 0; i < size; i++){
77
     for (int i = 0; i < size; i++){