add 3 to 6
This commit is contained in:
parent
170f2e3624
commit
155598172c
43
pset10/3-insertsort.c
Normal file
43
pset10/3-insertsort.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
|
||||
//插入排序(升序)
|
||||
//参数说明:数组,数组中已有元素个数
|
||||
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;
|
||||
}
|
36
pset10/4-shift.c
Normal file
36
pset10/4-shift.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void shift(int *array, int num, int size);
|
||||
|
||||
int main() {
|
||||
int i, n, p, array[100];
|
||||
|
||||
scanf(" %d%d", &n, &p); //测试用例保证0<p<n<=100
|
||||
for (i = 0; i < n; i++)
|
||||
scanf("%d", &array[i]); //测试用例保证所有输入可以用整型存储
|
||||
|
||||
shift(array, p, n); //向左移p位
|
||||
for (i = 0; i < n - 1; i++)
|
||||
printf("%d ", array[i]);
|
||||
printf("%d\n", array[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 请在这里填写答案 */
|
||||
void shift(int *array, int num, int size) {
|
||||
// Store data that will be overridden by shifting, starting from array[0]
|
||||
int cache;
|
||||
|
||||
// Start shifting once a time.
|
||||
for (int i = 0; i < num; i++) {
|
||||
// Shift to the left.
|
||||
cache = array[0];
|
||||
for (int j = 0; j < size - 1; j++) {
|
||||
array[j] = array[j + 1];
|
||||
}
|
||||
// Circular
|
||||
array[size - 1] = cache;
|
||||
}
|
||||
}
|
57
pset10/5-selectsort.c
Normal file
57
pset10/5-selectsort.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
|
||||
//选择排序(升序)
|
||||
//参数说明:数组,数组中已有元素个数
|
||||
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]);
|
||||
}
|
0
pset10/6-binsearch.c
Normal file
0
pset10/6-binsearch.c
Normal file
Loading…
Reference in a new issue