diff --git a/semester3/pset4/7-3-航空公司VIP客户查询.c b/semester3/pset4/7-3-航空公司VIP客户查询.c index 47b2eaa..5414bbc 100644 --- a/semester3/pset4/7-3-航空公司VIP客户查询.c +++ b/semester3/pset4/7-3-航空公司VIP客户查询.c @@ -6,7 +6,7 @@ #define IDLEN 19 #define P 13 -typedef struct hashtable { +typedef struct { int m; int p; char **key; diff --git a/semester3/pset5/7-1-Insertion-sort.c b/semester3/pset5/7-1-Insertion-sort.c new file mode 100644 index 0000000..1e944dd --- /dev/null +++ b/semester3/pset5/7-1-Insertion-sort.c @@ -0,0 +1,41 @@ +#include + +void print(int *arr, int N) { + for (int i = 0; i < N; i++) { + printf("%d,", arr[i]); + } + printf("\n"); +} + +void insertionSort(int *arr, int N) { + int key; + int j; + // i starts at 1. + for (int i = 1; i < N; i++) { + key = arr[i]; + j = i; + + while (j > 0 && arr[j - 1] > key) { + arr[j] = arr[j - 1]; + j--; + } + + arr[j] = key; + print(arr, N); + } +} + +int main(void) { + int N; + + scanf("%d", &N); + int arr[N]; + + for (int i = 0; i < N; i++) { + scanf("%d,", &arr[i]); + } + + insertionSort(arr, N); + + return 0; +} diff --git a/semester3/pset5/7-2-Shell-Sort.c b/semester3/pset5/7-2-Shell-Sort.c new file mode 100644 index 0000000..1871b4a --- /dev/null +++ b/semester3/pset5/7-2-Shell-Sort.c @@ -0,0 +1,42 @@ +#include + +void print(int *arr, int N) { + for (int i = 0; i < N; i++) { + printf("%d,", arr[i]); + } + printf("\n"); +} + +void shellSort(int *arr, int N) { + int j, key; + for (int gap = N / 2; gap > 0; gap /= 2) { + // insertion sort, with gaps + for (int i = gap; i < N; i++) { + key = arr[i]; + j = i; + + // remember to compare with gaps + while (j >= gap && arr[j - gap] < key) { + arr[j] = arr[j - gap]; + j -= gap; + } + arr[j] = key; + } + print(arr, N); + } +} + +int main(void) { + int N; + + scanf("%d", &N); + int arr[N]; + + for (int i = 0; i < N; i++) { + scanf("%d,", &arr[i]); + } + + shellSort(arr, N); + + return 0; +} diff --git a/semester3/pset5/7-3-Quick-Sort.c b/semester3/pset5/7-3-Quick-Sort.c new file mode 100644 index 0000000..618b1b4 --- /dev/null +++ b/semester3/pset5/7-3-Quick-Sort.c @@ -0,0 +1,56 @@ +#include +#define N 10 +#define CUTOFF 3 + +void print(int *arr) { + for (int i = 0; i < N; i++) { + printf("%d,", arr[i]); + } +} + +void swap(int *arr, int l, int r) { + if (l != r) { + int tmp = arr[l]; + arr[l] = arr[r]; + arr[r] = tmp; + } +} + +int partition(int *arr, int l, int r) { + // both edges included + int index = l; + int pivot = arr[l]; + for (int i = l + 1; i <= r; i++) { + if (arr[i] < pivot) { + swap(arr, i, index); + index++; + } + } + + swap(arr, index, l); + return index; +} + +void quickSort(int *arr, int l, int r) { + + int pi; + if (l < r) { + if (r - l < CUTOFF) { + printf("insert(%d,%d):", l, r - l + 1); + } else { + printf("Qsort(%d,%d):", l, r); + } + + pi = partition(arr, l, r); + + print(arr); + + quickSort(arr, l, pi - 1); + quickSort(arr, pi + 1, r); + } +} + +int main(void) { + int arr[] = {49, 38, 65, 97, 76, 13, 27, 50, 2, 8}; + return 0; +} diff --git a/semester3/pset5/7-4-Selection-Sort.c b/semester3/pset5/7-4-Selection-Sort.c new file mode 100644 index 0000000..999a937 --- /dev/null +++ b/semester3/pset5/7-4-Selection-Sort.c @@ -0,0 +1,43 @@ +#include + +void swap(int *arr, int l, int r) { + int tmp = arr[l]; + arr[l] = arr[r]; + arr[r] = tmp; +} + +void print(int *arr, int N) { + for (int i = 0; i < N; i++) { + printf("%d,", arr[i]); + } + printf("\n"); +} + +void selectionSort(int *arr, int N) { + for (int i = 0; i < N - 1; i++) { + int min = arr[i]; + int minIndex = i; + for (int j = i + 1; j < N; j++) { + if (arr[j] < min) { + min = arr[j]; + minIndex = j; + } + } + swap(arr, minIndex, i); + print(arr, N); + } +} + +int main(void) { + int N; + scanf("%d", &N); + + int arr[N]; + for (int i = 0; i < N; i++) { + scanf("%d,", &arr[i]); + } + + selectionSort(arr, N); + + return 0; +} diff --git a/semester3/pset5/7-5-Radix-Sort.c b/semester3/pset5/7-5-Radix-Sort.c new file mode 100644 index 0000000..3f67500 --- /dev/null +++ b/semester3/pset5/7-5-Radix-Sort.c @@ -0,0 +1,75 @@ +#include +#define SIZE 42 + +int getDigit(int i, int pass) { + while (--pass && i) { + i = i / 10; + } + return i % 10; +} + +void print(int *arr, int N) { + for (int i = 0; i < N; i++) { + printf("%d,", arr[i]); + } + printf("\n"); +} + +void radixSort(int *arr, int N, int pass) { + int bucket[10] = {0}; + int output[N]; + + for (int i = 0; i < N; i++) { + bucket[getDigit(arr[i], pass)]++; + } + + for (int i = 1; i < 10; i++) { + bucket[i] += bucket[i - 1]; + } + + for (int i = N - 1; i >= 0; i--) { + output[bucket[getDigit(arr[i], pass)] - 1] = arr[i]; + // this is important, to avoid collision + bucket[getDigit(arr[i], pass)]--; + } + + for (int i = 0; i < N; i++) { + arr[i] = output[i]; + } +} + +int getDigits(int i) { + // this is used to offset the string + int count = 0; + do { + i /= 10; + count++; + } while (i); + + return count; +} + +int main(void) { + int arr[SIZE]; + int N = 0; + char buf[SIZE]; + fgets(buf, SIZE, stdin); + int offset = 0; + + while (sscanf(buf + offset, "%d,", &arr[N]) != EOF) { + // remember the comma! + offset += getDigits(arr[N]) + 1; + N++; + } + + int stepPass; + scanf("%d", &stepPass); + + for (int i = 1; i <= stepPass; i++) { + radixSort(arr, N, i); + } + + // only output the last step + print(arr, N); + return 0; +}