diff --git "a/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/1.PNG" "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/1.PNG" new file mode 100644 index 00000000..d116ce77 Binary files /dev/null and "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/1.PNG" differ diff --git "a/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/2.PNG" "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/2.PNG" new file mode 100644 index 00000000..706cb0e4 Binary files /dev/null and "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/2.PNG" differ diff --git "a/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/3.PNG" "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/3.PNG" new file mode 100644 index 00000000..6998f8c5 Binary files /dev/null and "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/3.PNG" differ diff --git "a/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/Hashtable.c" "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/Hashtable.c" new file mode 100644 index 00000000..8d430a4f --- /dev/null +++ "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/Hashtable.c" @@ -0,0 +1,97 @@ +#include "Hashtable.h" + +Hashtable CreatHash(int size) {//创建哈希表 + int i; + Hashtable temp; + temp.size = size; + temp.used = 0; + temp.elements = (ElemType*)malloc(size * sizeof(ElemType)); + for (i = 0; i < HashLength; ++i) { + temp.elements[i].key = -1; + temp.elements[i].val = 0; + } + return temp; +} + +Status CreatArray(int arr[][2]) {//随机生成需要输入的数组 + int i; + srand(time(0)); + for (i = 0; i < ArrayLenth; ++i) { + arr[i][0] = rand() % 100 + 1; + arr[i][1] = rand() % 100 + 1; + } + return OK; +} + +bool isFull(Hashtable H) {//判断哈希表是否已经完全使用 + return H.size == H.used; +} + +Status Insert(Hashtable H, KeyType keyvalue, ValueType value) {//往哈希表内插入元素 + int d = 0; + int pos = (keyvalue + d) % H.size; + if (isFull(H)) { + printf("哈希表已满\n"); + RebulidHash(&H); + return ERROR; + } + while (H.elements[pos].key != -1 && keyvalue != -1) { + d++; + pos = (pos + d) % H.size; + } + if (keyvalue != -1) { + H.elements[pos].key = keyvalue; + H.elements[pos].val = value; + } + if (d != 0) { + printf("\n----[插入%d元素时发生了冲突%d次]----\n", keyvalue, d); + } + return OK; +} + +Status Find(Hashtable H, KeyType keyval) {//在哈希表中查找元素 + int d = 0; + int pos = (keyval + d) % H.size; + printf("\n在表中查找关键字%d\n", keyval); + while (H.elements[pos].key != keyval) { + if (H.elements[pos].key == -1) { + printf("所查找的关键字不在表中\n"); + return ERROR; + } + d++; + pos = (pos + d) % H.size; + } + printf("在哈希表中位置%d找到关键字%d\n\n", pos, keyval); + return OK; +} + +Status RebulidHash(Hashtable *H) {//重建哈希表 + int newsize = (*H).size; + int i; + Hashtable temp = CreatHash((*H).size); + printf("\n重建哈希表\n"); + for (i = 0; i < (*H).size; ++i) { + temp.elements[i].key = (*H).elements[i].key; + temp.elements[i].val = (*H).elements[i].val; + } + (*H).size *= 2; + (*H).elements = (ElemType*)malloc((*H).size * sizeof(ElemType)); + for (i = 0; i < (*H).size; ++i) { + (*H).elements[i].key = -1; + (*H).elements[i].val = 0; + } + for (i = 0; i < newsize; ++i) { + Insert(*H, temp.elements[i].key, temp.elements[i].val); + } + return OK; +} + +Status TraverseHash(Hashtable H) {//遍历哈希表 + int i; + int flag=0; + for (i = 0; i < H.size; ++i) { + printf("{ [%d] : %d->%d } \n", i, H.elements[i].key, H.elements[i].val); + } + printf("\n"); + return OK; +} diff --git "a/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/Hashtable.h" "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/Hashtable.h" new file mode 100644 index 00000000..820caa67 --- /dev/null +++ "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/Hashtable.h" @@ -0,0 +1,35 @@ +#include +#include +#include + +#define ArrayLenth 10 //输入的数组长度 +#define HashLength 12 //哈希表初始化长度 + +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +} ElemType; +typedef struct { + int size; + int used; + ElemType* elements; +}Hashtable; +typedef enum { + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; + +Hashtable CreatHash(int size); +Status CreatArray(int arr[][2]); +bool isFull(Hashtable H); +Status Insert(Hashtable H, KeyType keyvalue, ValueType value); +Status Find(Hashtable H, KeyType keyval); +Status RebulidHash(Hashtable *H); +Status TraverseHash(Hashtable H); \ No newline at end of file diff --git "a/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/main.c" "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/main.c" new file mode 100644 index 00000000..bc635d88 --- /dev/null +++ "b/2017-1/li-yc/\345\223\210\345\270\214\350\241\250/main.c" @@ -0,0 +1,34 @@ +#include "Hashtable.h" + +int main() { + Hashtable H; + int arr[ArrayLenth][2]; + int i; + + srand(time(0)); + + //创建哈希表 + H = CreatHash(HashLength); + CreatArray(arr); + + //初始化哈希表 + for (i = 0; i < ArrayLenth; ++i) { + Insert(H, arr[i][0], arr[i][1]); + } + printf("哈希表建立完毕\n"); + TraverseHash(H); + + //关键字查找 + for (i = 0; i < HashLength / 2; ++i) { + Find(H, arr[rand() % ArrayLenth][0]); + } + for (i = 0; i < HashLength / 2; ++i) { + Find(H, rand() % 100 + 1); + } + + //重建哈希表 + RebulidHash(&H); + TraverseHash(H); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\346\216\222\345\272\217/1.PNG" "b/2017-1/li-yc/\346\216\222\345\272\217/1.PNG" new file mode 100644 index 00000000..d5156ddb Binary files /dev/null and "b/2017-1/li-yc/\346\216\222\345\272\217/1.PNG" differ diff --git "a/2017-1/li-yc/\346\216\222\345\272\217/2.PNG" "b/2017-1/li-yc/\346\216\222\345\272\217/2.PNG" new file mode 100644 index 00000000..d050cb9e Binary files /dev/null and "b/2017-1/li-yc/\346\216\222\345\272\217/2.PNG" differ diff --git "a/2017-1/li-yc/\346\216\222\345\272\217/Sort.c" "b/2017-1/li-yc/\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..be06e0eb --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,131 @@ +#include "Sort.h" + +Status InsertSort(SqList *a, int *ctime, int *mtime) { + int temp; + int i; + int j; + for (i = 1; i < a->length; ++i) { + if (a->r[i] < a->r[i - 1] && ++(*ctime)) { + temp = a->r[i]; + a->r[i] = a->r[i - 1]; + ++(*mtime); + for (j = i - 1; temp < a->r[j]; --j) { + a->r[j + 1] = a->r[j]; + ++(*mtime); + } + a->r[j + 1] = temp; + ++(*ctime); + } + } + return OK; +} + +Status ShellInsert(SqList *a, int dk, int *ctime, int *mtime) { + int temp; + int i; + int j; + for (i = dk; i < a->length; ++i) { + if (a->r[i] < a->r[i - dk] && ++(*ctime)) { + temp = a->r[i]; + a->r[i] = a->r[i - dk]; + (*mtime)++; + for (j = i - dk; j > 0 && temp < a->r[j]; --j) { + a->r[j + dk] = a->r[j]; + ++(*mtime); + } + a->r[j + dk] = temp; + ++(*ctime); + } + } + return OK; +} + +Status BubbleSort(SqList *a, int *ctime, int *mtime) { + int i; + int j; + int temp; + for (i = 0; i < a->length; ++i) { + for (j = i + 1; j < a->length; ++j) { + if (a->r[i] > a->r[j] && ++(*ctime)) { + temp = a->r[i]; + a->r[i] = a->r[j]; + a->r[j] = temp; + (*mtime) += 3; + } + } + } + return OK; +} + +Status QKSort(SqList *a, int low, int high, int *ctime, int *mtime) { + int pivotlo; + if (low < high) { + pivotlo = QKPass(a, low, high, ctime, mtime); + QKSort(a, low, pivotlo - 1, ctime, mtime); + QKSort(a, pivotlo + 1, high, ctime, mtime); + } + return OK; +} + +int QKPass(SqList *a, int left, int right, int *ctime, int *mtime) { + int key = a->r[left]; + int low = left; + int high = right; + while (low < high) { + while (low < high&&a->r[high] >= key&&++(*ctime)) { + high--; + } + a->r[low] = a->r[high]; + ++(*mtime); + while (low < high&&a->r[low] < key&&++(*ctime)) { + low++; + } + a->r[high] = a->r[low]; + ++(*mtime); + } + a->r[low] = key; + ++(*mtime); + return low; +} + +Status SelectSort(SqList *a, int *ctime, int *mtime) { + int i; + int j; + int key; + int temp; + for (i = 0; i < a->length; ++i) { + key = i; + for (j = i + 1; j < a->length; ++j) { + if (a->r[j] < a->r[key] && ++(*ctime)) { + key = j; + } + } + if (key != i) { + temp = a->r[i]; + a->r[i] = a->r[key]; + a->r[key] = temp; + (*mtime) += 3; + } + } + return OK; +} + +Status Traverse(SqList a) { + int i; + for (i = 0; i < a.length; ++i) { + printf("%d ", a.r[i]); + } + printf("\n"); + return OK; +} + +Status InitTestArrary(SqList *a, SqList *a1, int *ctime, int *mtime) { + int i; + for (i = 0; i < a->length; ++i) { + a1->r[i] = a->r[i]; + } + *ctime = 0; + *mtime = 0; + printf("\n将排序完成的数组恢复成原来顺序\n\n"); + return OK; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\346\216\222\345\272\217/Sort.h" "b/2017-1/li-yc/\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..9e815926 --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,28 @@ +#include +#include +#include + +#define MAXSIZE 20 + +typedef enum { + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct { + int r[MAXSIZE]; + int length; +}SqList; + +Status InsertSort(SqList *a, int *ctime, int *mtime); +Status ShellInsert(SqList *a, int dk, int *ctime, int *mtime); +Status BubbleSort(SqList *a, int *ctime, int *mtime); +Status QKSort(SqList *a, int low, int high, int *ctime, int *mtime); +int QKPass(SqList *a, int left, int right, int *ctime, int *mtime); +Status SelectSort(SqList *a, int *ctime, int *mtime); +Status Traverse(SqList a); +Status InitTestArrary(SqList *a, SqList *a1, int *ctime, int *mtime); \ No newline at end of file diff --git "a/2017-1/li-yc/\346\216\222\345\272\217/main.c" "b/2017-1/li-yc/\346\216\222\345\272\217/main.c" new file mode 100644 index 00000000..6c33cbe7 --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217/main.c" @@ -0,0 +1,59 @@ +#include "Sort.h" + +int main() { + SqList a, test; + int i; + int comparetimes = 0, movetimes = 0; + int dk; + + srand((unsigned)time(NULL)); + a.length = test.length = 0; + for (i = 0; i < MAXSIZE; ++i) { + a.r[i] = test.r[i] = rand() % 100 + 1; + a.length++; + test.length++; + } + + printf("初始化的数组为:\n"); + Traverse(a); + printf("待排序的数组为:\n"); + Traverse(test); + + InsertSort(&test, &comparetimes, &movetimes); + printf("\n经过直接插入排序后的数组:\n"); + Traverse(test); + printf("总比较次数为:%d\n总移动记录次数:%d\n两者次数之和:%d\n", comparetimes, movetimes, comparetimes + movetimes); + + InitTestArrary(&a, &test, &comparetimes, &movetimes); + + dk = 4; + ShellInsert(&test, dk, &comparetimes, &movetimes); + dk = 6; + ShellInsert(&test, dk, &comparetimes, &movetimes); + InsertSort(&test, &comparetimes, &movetimes); + printf("经过希尔排序后的数组:\n"); + Traverse(test); + printf("总比较次数为:%d\n总移动记录次数:%d\n两者次数之和:%d\n", comparetimes, movetimes, comparetimes + movetimes); + + InitTestArrary(&a, &test, &comparetimes, &movetimes); + + BubbleSort(&test, &comparetimes, &movetimes); + printf("经过气泡排序后的数组:\n"); + Traverse(test); + printf("总比较次数为:%d\n总移动记录次数:%d\n两者次数之和:%d\n", comparetimes, movetimes, comparetimes + movetimes); + + InitTestArrary(&a, &test, &comparetimes, &movetimes); + + QKSort(&test, 0, 19, &comparetimes, &movetimes); + printf("经过快速排序后的数组:\n"); + Traverse(test); + printf("总比较次数为:%d\n总移动记录次数:%d\n两者次数之和:%d\n", comparetimes, movetimes, comparetimes + movetimes); + + InitTestArrary(&a, &test, &comparetimes, &movetimes); + + SelectSort(&test, &comparetimes, &movetimes); + printf("经过简单选择排序后的数组:\n"); + Traverse(test); + printf("总比较次数为:%d\n总移动记录次数:%d\n两者次数之和:%d\n", comparetimes, movetimes, comparetimes + movetimes); + return 0; +} \ No newline at end of file