浏览代码

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

Lily Carpenter 12 年之前
父节点
当前提交
eb36717f8e
共有 2 个文件被更改,包括 23 次插入19 次删除
  1. 二进制
      mysort
  2. 23 19
      mysort.c

二进制
mysort


+ 23 - 19
mysort.c

@@ -5,9 +5,9 @@
5 5
 
6 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 11
     save = realloc(a, new_size);
12 12
 
13 13
     if (save == NULL) {
@@ -18,14 +18,14 @@ int *resize_array(int *a, size_t new_size)
18 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 23
     *a = *b;
24 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 29
     swap(&array[pivot], &array[right]);
30 30
     int storeIndex = left;
31 31
     for(int i = left; i < right; i++){
@@ -40,7 +40,7 @@ int partition(int *array, int left, int right, int pivot){
40 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 44
     if (left < right){
45 45
         int pivot = right;
46 46
 
@@ -52,22 +52,26 @@ void quicksort(int *array, int left, int right){
52 52
 
53 53
 int main()
54 54
 {
55
+    int temp_size = 50;
55 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 76
     quicksort(numbers, 0, size - 1);
73 77
     for (int i = 0; i < size; i++){