7-3 not AC yet

This commit is contained in:
juan 2022-11-13 21:16:43 +08:00
parent 4dd3094c03
commit 3e1009a9e9
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
6 changed files with 258 additions and 1 deletions

View file

@ -6,7 +6,7 @@
#define IDLEN 19
#define P 13
typedef struct hashtable {
typedef struct {
int m;
int p;
char **key;

View file

@ -0,0 +1,41 @@
#include <stdio.h>
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;
}

View file

@ -0,0 +1,42 @@
#include <stdio.h>
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;
}

View file

@ -0,0 +1,56 @@
#include <stdio.h>
#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;
}

View file

@ -0,0 +1,43 @@
#include <stdio.h>
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;
}

View file

@ -0,0 +1,75 @@
#include <stdio.h>
#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;
}