sort.c 3.19 KB
 Niklas Rieken committed May 22, 2019 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 ``````#include #include void swap(int** ary, int i, int j) { (*ary)[i] += (*ary)[j]; (*ary)[j] = (*ary)[i] - (*ary)[j]; (*ary)[i] -= (*ary)[j]; } void bubblesort(int** ary, int len) { int i, j; for (i = 0; i < len; i++) { for (j = 0; j < len-(i+1); j++) { if ((*ary)[j] > (*ary)[j+1]) { swap(ary, j, j+1); } } } } void insertionsort(int** ary, int len) { int i, j, a; for (i = 1; i < len; i++) { a = (*ary)[i]; j = i; while (j > 0 && (*ary)[j-1] > a) { (*ary)[j] = (*ary)[j-1]; j--; } (*ary)[j] = a; } } void selectionsort(int** ary, int len) { int i, j; for (i = 0; i < len; i++) { for (j = i+1; j < len; j++) { if ((*ary)[i] > (*ary)[j]) { swap(ary, i, j); } } } } void merge(int** ary, int l, int m, int r) { int i, j, k; int left[m-l+1]; int right[r-m]; for (i = 0; i < m-l+1; i++) { left[i] = (*ary)[l+i]; } for (j = 0; j < r-m; j++) { right[j] = (*ary)[m+j+1]; } i = 0; j = 0; k = l; while (i < m-l+1 && j < r-m) { if (left[i] <= right[j]) { (*ary)[k] = left[i]; i++; } else { (*ary)[k] = right[j]; j++; } k++; } while (i < m-l+1) { (*ary)[k] = left[i]; i++; k++; } while (j < r-m) { (*ary)[k] = right[j]; j++; k++; } } void mergesort(int** ary, int l, int r) { int m; if (l < r) { m = l+(r-l)/2; mergesort(ary, l, m); mergesort(ary, m+1, r); merge(ary, l, m, r); } } void quicksort(int** ary, int L, int R) { int p, l, r, tmp; if (R > L) { p = (*ary)[L]; l = L; r = R+1; do { do { l++; } while ((*ary)[l] < p && l < r); do { r--; } while ((*ary)[r] > p); swap(ary, l, r); } while (l < r); tmp = (*ary)[r]; (*ary)[L] = (*ary)[l]; (*ary)[l] = tmp; (*ary)[r] = p; quicksort(ary, L, r-1); quicksort(ary, r+1, R); } } void heapify(int** ary, int len, int i) { int max = i; int l = 2*i+1; int r = 2*i+2; if (l < len && (*ary)[l] > (*ary)[max]) { max = l; } if (r < len && (*ary)[r] > (*ary)[max]) { max = r; } if (max != i) { swap(ary, i, max); heapify(ary, len, max); } } void heapsort(int** ary, int len) { int i; for (i = len/2-1; i >= 0; i--) { heapify(ary, len, i); } for (i = len-1; i >= 0; i--) { swap(ary, 0, i); heapify(ary, i, 0); } } `````` Niklas Rieken committed May 23, 2019 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 ``````void radixsort(int** ary, int len) { int i, j, k; int n[2] = {0, 0}; int part[2][len]; for (i = 0; i < 32; i++) { n[0] = 0; n[1] = 0; for (j = 0; j < len; j++) { k = ((*ary)[j] >> i) & 1; part[k][n[k]++] = (*ary)[j]; } for (j = 0; j < n[0]; j++) { (*ary)[j] = part[0][j]; } for (j = 0; j < n[1]; j++) { (*ary)[j+n[0]] = part[1][j]; } } } `````` Niklas Rieken committed May 22, 2019 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 ``````void print_array(int* ary, int len) { int i; printf("["); for (i = 0; i < len; i++) { printf("%d, ", ary[i]); } printf("\b\b]\n"); } int main(int argc, char** argv) { int i; int* ary = malloc((argc-1) * sizeof(int)); if (ary == NULL) { return 1; } for (i = 0; i < argc-1; i++) { *(ary+i) = atoi(argv[i+1]); } print_array(ary, argc-1); //bubblesort(&ary, argc-1); //insertionsort(&ary, argc-1); //selectionsort(&ary, argc-1); //mergesort(&ary, 0, argc-2); //quicksort(&ary, 0, argc-2); `````` Niklas Rieken committed May 23, 2019 198 199 `````` //heapsort(&ary, argc-1); radixsort(&ary, argc-1); `````` Niklas Rieken committed May 22, 2019 200 201 202 203 204 205 206 `````` print_array(ary, argc-1); free(ary); return 0; }``````