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