From 155598172c4f2ca71877035c9494527ae5d60bd6 Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 2 Dec 2021 17:32:01 +0800 Subject: [PATCH] add 3 to 6 --- pset10/3-insertsort.c | 43 ++++++++++++++++++++++++++++++++ pset10/4-shift.c | 36 +++++++++++++++++++++++++++ pset10/5-selectsort.c | 57 +++++++++++++++++++++++++++++++++++++++++++ pset10/6-binsearch.c | 0 4 files changed, 136 insertions(+) create mode 100644 pset10/3-insertsort.c create mode 100644 pset10/4-shift.c create mode 100644 pset10/5-selectsort.c create mode 100644 pset10/6-binsearch.c diff --git a/pset10/3-insertsort.c b/pset10/3-insertsort.c new file mode 100644 index 0000000..c91c1a9 --- /dev/null +++ b/pset10/3-insertsort.c @@ -0,0 +1,43 @@ +#include + +//插入排序(升序) +//参数说明:数组,数组中已有元素个数 +void InsertSort(int a[], int n); + +int main() { + int n, i, num[1000]; + + scanf("%d", &n); + for (i = 0; i < n; i++) + scanf("%d", &num[i]); + InsertSort(num, n); + return 0; +} + +/* 请在这里填写答案 */ +void InsertSort(int a[], int n) { + int loc; + int selected; + // sorted subarray: a[0 ~ i - 1] + for (int i = 1; i < n; i++) { + selected = a[i]; + loc = 0; + for (int j = 0; j < i; j++) { + if (a[j] <= selected) { + loc = j + 1; + } + } + // switch to location + /* printf("location: %d i: %d\n", loc, i); */ + for (int j = i; j > loc; j--) { + a[j] = a[j - 1]; + } + a[loc] = selected; + // print array + for (int j = 0; j < n - 1; j++) { + printf("%d ", a[j]); + } + printf("%d\n", a[n - 1]); + } + return; +} diff --git a/pset10/4-shift.c b/pset10/4-shift.c new file mode 100644 index 0000000..7241e67 --- /dev/null +++ b/pset10/4-shift.c @@ -0,0 +1,36 @@ +#include +#include + +void shift(int *array, int num, int size); + +int main() { + int i, n, p, array[100]; + + scanf(" %d%d", &n, &p); //测试用例保证0 + +//选择排序(升序) +//参数说明:数组,数组中已有元素个数 +void selectSort(int data[], int elementCount); + +//函数功能:找数组中的最小值元素,并返回其下标 +//参数说明:数组名,查找起始位置下标,查找终止位置下标 +int findMin(int data[], int startLoc, int endLoc); + +//输出数组中所有元素 +//参数说明:数组,数组中已有元素个数 +void outputData(int data[], int elementCount); + +int main() { + int n, i, num[1010]; + + scanf("%d", &n); + for (i = 0; i < n; i++) + scanf("%d", &num[i]); + selectSort(num, n); + return 0; +} + +/* 请在这里填写答案 */ +void selectSort(int data[], int elementCount) { + int tmp; + int min; + // Iterate n times + for (int i = 0; i < elementCount - 1; i++) { + // Swap data[i] and data[findMin()] + tmp = data[i]; + min = findMin(data, i, elementCount - 1); + /* printf("i: %d, min: %d\n", i, min); */ + data[i] = data[min]; + data[min] = tmp; + outputData(data, elementCount); + } +} + +int findMin(int data[], int startLoc, int endLoc) { + int min = data[startLoc], minloc = startLoc; + for (int i = startLoc; i <= endLoc; i++) { + if (data[i] <= min) { + minloc = i; + min = data[i]; + } + } + return minloc; +} + +void outputData(int data[], int elementCount) { + for (int i = 0; i < elementCount - 1; i++) { + printf("%d ", data[i]); + } + printf("%d\n", data[elementCount - 1]); +} diff --git a/pset10/6-binsearch.c b/pset10/6-binsearch.c new file mode 100644 index 0000000..e69de29