diff --git a/2017-1/luyj/Hash.c b/2017-1/luyj/Hash.c new file mode 100644 index 00000000..3ff89f9d --- /dev/null +++ b/2017-1/luyj/Hash.c @@ -0,0 +1,157 @@ +#include "Hash.h" + + +int getP(int length) +{ + //找到不大于表长的最大质数 + int p = 0; + int flag = 0; + int i,j; + for (i = length; i > 0; i--) + { + flag = 0; + for (j = 2; j count; + ElemType *x; + ElemType *elem = (ElemType*)malloc(count*(sizeof(ElemType))); + x = elem; + for (i = 0; i < count; i++)//复制表数据到elem中 + { + *x = h->elem[i]; + x++; + } + h->count = 0; + h->sizeindex = h->sizeindex * 2; + x = (ElemType*)realloc(h->elem, h->sizeindex * sizeof(ElemType)); + if (!x) + { + return ERROR; + } + h->elem = x; + for (i = 0; i < h->sizeindex; i++) + { + h->elem->key = NULL; + } + x = elem; + for (i = 0; i < count; i++) + { + InsertHash(h, *x); + } + + +} +int SearchHash(HashTable h, KeyType k, int *q, int *c, int p) +{ + // 在开放定址哈希表H中查找关键码为K的记录 + // c用于记录冲突次数,初值置0 + + *q = Hash(k,p); // 求得哈希地址 + while (-1 != h.elem[*q].key && !EQ(k, h.elem[*q].key)) + { + (*c)++; + collision(q, p); + if ((*c) > h.sizeindex) + { + break; + } + }// 求得下一探查地址 p + if (EQ(k, h.elem[*q].key)) + { // 查找成功,p返回待查数据元素位置 + return SUCCESS; + } + else if (-1 == h.elem[*q].key) + { + return FAILED; // 查找不成功 + } +} + +int InsertHash(HashTable *h, ElemType e) +{ + // 查找不成功时插入数据元素e到开放地址哈希表H中,并返回OK + // 若冲突次数过大,则重建哈希表 + int c = 0;//记录冲突次数 + int q; + int p; + p = getP(h->sizeindex); + if (SUCCESS == SearchHash(*h, e.key, &q, &c, p)) + { + // 表中已有与 e 有相同关键字的元素 + return DUPLICATE; + } + else if (c < p) + { + // 冲突次数 c 未达到上限 + h->elem[q] = e; + ++h->count; + return OK; + } + else + { + // 重建哈希表,极端情况下可能哈希表“已满”。 + // 所以通常的重建过程都是先增大新哈希表的容量 + RecreateHashTable(h); + } +} + +Status InitHashTable(HashTable *h,int num) +{ + + int i; + h->count = 0; + h->sizeindex = num; + h->elem = (ElemType*)malloc(h->sizeindex * sizeof(ElemType)); + if (!(h->elem)) + return ERROR; + ElemType*p = h->elem; + for (i = 0; i < h->sizeindex; i++) + { + p->key = NULL; + p->val = 0; + p++; + } +} diff --git a/2017-1/luyj/Hash.h b/2017-1/luyj/Hash.h new file mode 100644 index 00000000..b4b57ae6 --- /dev/null +++ b/2017-1/luyj/Hash.h @@ -0,0 +1,47 @@ +#include +#include +#include + +#define NULL -1 +#define SUCCESS 1 +#define FAILED 0 +#define DUPLICATE -1 + +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType +{ + KeyType key; + ValueType val; +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif // CHAINED_HASH + +}ElemType; + +typedef struct +{ + ElemType *elem; + int count; // 当前数据元素个数 + int sizeindex; // hashsize[sizeindex]为当前容量 +} HashTable; + +typedef enum +{ + FALSE, + TRUE +}Bool; + +typedef enum +{ + OK, + ERROR +}Status; + +int InsertHash(HashTable *h, ElemType e); +int Hash(int k, int p); +Bool EQ(KeyType k, KeyType key); +Status collision(int *q, int p); +Status RecreateHashTable(HashTable *h); +int SearchHash(HashTable h, KeyType k, int *q, int *c, int p); +Status InitHashTable(HashTable *h, int num); diff --git a/2017-1/luyj/Sort.c b/2017-1/luyj/Sort.c new file mode 100644 index 00000000..47000f70 --- /dev/null +++ b/2017-1/luyj/Sort.c @@ -0,0 +1,181 @@ +#include "Sort.h" + +Bool LT(KeyType a, KeyType b) +{ + if (a < b) + { + return TRUE; + } + else + { + return FALSE; + } +} + +/*=========直接插入排序输出=========*/ +void InsSort(RecordType *r, int length,int *cmp_count,int *mov_count) +{ + RecordType temp; + int i, j; + for (i = 1; i < length; i++) + { + temp = r[i]; + (*mov_count)++; + j = i - 1; + while ((*cmp_count)++,LT(temp.key, r[j].key) && j >= 0) + { + r[j + 1] = r[j]; + (*mov_count)++; + j = j - 1; + } + r[j + 1] = temp; + (*mov_count)++; + } +} + +/*==========希尔排序输出=========*/ +void ShellSort(RecordType *r, int length, int *cmp_count, int *mov_count) +{ + int d = length / 2; + while (d >= 1) + { + ShellInsert(r, length, d, cmp_count, mov_count); + d = d / 2; + } +} +void ShellInsert(RecordType *r, int length, int delta, int *cmp_count, int *mov_count) +{ + RecordType temp; + int i, j; + for (i = delta; i < length; i++) + { + if ((*cmp_count)++,LT(r[i].key, r[i-delta].key)) + { + temp = r[i]; + (*mov_count)++; + for ((*cmp_count)++,j = i-delta; j >= 0 &<(temp.key , r[j].key); j -= delta) + { + r[j + delta] = r[j]; + (*mov_count)++; + } + r[j + delta] = temp; + (*mov_count)++; + } + + } +} + + +/*=========冒泡排序输出=========*/ +void BubbleSort(RecordType *r, int length, int *cmp_count, int *mov_count) +{ + RecordType temp; + int i, j; + Bool change = TRUE; + for (i = 0; i < length && change; i++) + { + change = FALSE; + for (j = 0; j < length-i-1; j++) + { + if ((*cmp_count)++,LT(r[j+1].key, r[j].key)) + { + temp = r[j]; + r[j] = r[j+1]; + r[j+1] = temp; + change = TRUE; + (*mov_count) =(*mov_count) + 3; + } + } + } +} + + +/*=========快速排序输出=========*/ +void QKSort(RecordType *r, int low, int high,int *cmp_count,int *mov_count) + + +{ + int pivot; + if (low < high) + { + pivot = QKPass(r, low, high,cmp_count,mov_count); + QKSort(r, low, pivot - 1,cmp_count,mov_count); + QKSort(r, pivot + 1, high,cmp_count,mov_count); + } +} + +int QKPass(RecordType *r, int left, int right, int *cmp_count, int *mov_count) +{ + RecordType x = r[left]; // 选择基准记录 + int low = left; + int high = right; + while (LT(low , high)) + { + while ((*cmp_count)++,LT(low , high) && r[high].key >= x.key) //high从右到左找小于x.key的记录 + { + high--; + } + r[low] = r[high]; //找到小于x.key的记录,则进行交换 + (*mov_count)++; + while ((*cmp_count)++, LT(low, high) && r[low].key < x.key)// low从左到右找不小于x.key的记录 + { + low++; + } + r[high] = r[low]; // 找到不小于x.key的记录,则交换 + (*mov_count)++; + } + + r[low] = x; //将基准记录保存到low=high的位置 + (*mov_count)++; + return low; //返回基准记录的位置 +} + +/*=========简单选择排序=========*/ +void SelectSort(RecordType *r, int length,int *cmp_count,int *mov_count) +{ + int i, j, k; + RecordType temp; + for (i = 0; i < length-1; ++i) + { + k = i; + for (j = i + 1; j < length; ++j) + { + if ((*cmp_count)++,LT(r[j].key , r[k].key)) + { + k = j; + } + } + + if (k != i) + { + temp = r[i]; + r[i] = r[k]; + r[k] = temp; + (*mov_count) = (*mov_count) + 3; + } + } +} + +/*=========数据输出=========*/ +void print(RecordType *r,int length,int cmp_count,int mov_count) +{ + int i; + for (i = 0; i < length; i++) + { + printf("%d ", r[i].key); + } + printf("\n比较次数:%d\n", cmp_count); + printf("移动次数:%d\n\n", mov_count); +} + +/*=========初值恢复=========*/ +void Inital(RecordType *counts, RecordType *count,int length, int *cmp_count, int *mov_count) +{ + int i; + for (i = 0; i < length; i++) + { + count[i] = counts[i]; + } + (*cmp_count) = 0; + (*mov_count) = 0; +} diff --git a/2017-1/luyj/Sort.h b/2017-1/luyj/Sort.h new file mode 100644 index 00000000..5fb7040a --- /dev/null +++ b/2017-1/luyj/Sort.h @@ -0,0 +1,31 @@ +#include +#include +#include +typedef int KeyType; +typedef int OtherType; + +typedef enum +{ + FALSE, + TRUE +}Bool; + +typedef struct +{ + KeyType key; + OtherType data; +}RecordType; + +void ShellInsert(RecordType *r, int length, int delta, int *cmp_count, int *mov_count); +int QKPass(RecordType *r, int left, int right, int *cmp_count, int *mov_count); +Bool LT(KeyType a, KeyType b); +void InsSort(RecordType *r, int length, int *cmp_count, int *mov_count); +void ShellSort(RecordType *r, int length, int *cmp_count, int *mov_count); +void ShellInsert(RecordType *r, int length, int delta, int *cmp_count, int *mov_count); +void BubbleSort(RecordType *r, int length, int *cmp_count, int *mov_count); +void QKSort(RecordType *r, int low, int high, int *cmp_count, int *mov_count); +int QKPass(RecordType *r, int left, int right, int *cmp_count, int *mov_count); + +void SelectSort(RecordType *r, int length, int *cmp_count, int *mov_count); +void print(RecordType *r, int length, int cmp_count, int mov_count); +void Inital(RecordType *counts, RecordType *count, int length, int *cmp_count, int *mov_count); diff --git a/2017-1/luyj/Sort.png b/2017-1/luyj/Sort.png new file mode 100644 index 00000000..3bb9b824 Binary files /dev/null and b/2017-1/luyj/Sort.png differ diff --git a/2017-1/luyj/hash.png b/2017-1/luyj/hash.png new file mode 100644 index 00000000..44eb05d3 Binary files /dev/null and b/2017-1/luyj/hash.png differ diff --git a/2017-1/luyj/s_Hash.c b/2017-1/luyj/s_Hash.c new file mode 100644 index 00000000..6c6d7f92 --- /dev/null +++ b/2017-1/luyj/s_Hash.c @@ -0,0 +1,46 @@ +#include "Hash.h" + +int main() +{ + HashTable h; + ElemType e; + int num = 0;//随机生成表长; + int numble = 0;//随机生成初始填充表数个数; + int i; + srand(time(0)); + num = rand() % 5 + 10; + numble = rand() % 5 + 5; + InitHashTable(&h, num); + printf("随机生成哈希表表长: %d\n", num); + printf("随机生成填充数据个数:%d\n", numble); + for (i = 0; i < numble; i++) + { + e.key = rand() % 20 + 1; + e.val = rand() % 20 + 1; + printf("key:%d val:%d\n", e.key, e.val); + InsertHash(&h, e); + } + for (i = 0; i < num; i++) + { + printf("{[%d]:%d->%d} ", i, h.elem[i]); + } + int p = getP(num); + + for (i = 0; i < 10; i++) + { + int c = 0; + e.key = rand() % 20 + 1; + e.val = rand() % 20 + 1; + int q = Hash(e.key, p); + printf("\n查找: key:%d val:%d\n", e.key, e.val); + if (1 == SearchHash(h, e.key, &q, &c, p)) + { + printf("查找成功 "); + } + else + { + printf("查找失败 "); + } + printf("冲突次数:%d\n", c); + } +} \ No newline at end of file diff --git a/2017-1/luyj/s_Sort.c b/2017-1/luyj/s_Sort.c new file mode 100644 index 00000000..3ab77c15 --- /dev/null +++ b/2017-1/luyj/s_Sort.c @@ -0,0 +1,50 @@ +#include "Sort.h" +int main() +{ + srand(time(0)); + int length = rand() % 9 + 2; + RecordType *counts; + RecordType *count; + counts = (RecordType*)malloc(length*(sizeof(RecordType))); + count = (RecordType*)malloc(length*(sizeof(RecordType))); + int mov_count = 0; + int cmp_count = 0; + int i; + for (i = 0; i < length; i++) + { + int a = rand() % 9 + 1; + counts[i].key = a; + counts[i].data = 0; + } + for (i = 0; i < length; i++) + { + printf("%d ", counts[i]); + } + + /*=========直接插入排序输出=========*/ + printf("\n直接插入排序:\n"); + Inital(counts, count, length, &cmp_count, &mov_count); + InsSort(count, length, &cmp_count, &mov_count); + print(count, length, cmp_count, mov_count); + /*==========希尔排序输出=========*/ + printf("希尔排序:\n"); + Inital(counts, count, length, &cmp_count, &mov_count); + ShellSort(count, length, &cmp_count, &mov_count); + print(count, length, cmp_count, mov_count); + /*=========冒泡排序输出=========*/ + printf("冒泡排序:\n"); + Inital(counts, count, length, &cmp_count, &mov_count); + BubbleSort(count, length, &cmp_count, &mov_count); + print(count, length, cmp_count, mov_count); + /*=========快速排序输出=========*/ + printf("快速排序:\n"); + Inital(counts, count, length, &cmp_count, &mov_count); + QKSort(count, 0, length - 1, &cmp_count, &mov_count); + print(count, length, cmp_count, mov_count); + /*=========简单选择排序=========*/ + printf("简单选择排序:\n"); + Inital(counts, count, length, &cmp_count, &mov_count); + SelectSort(count, length, &cmp_count, &mov_count); + print(count, length, cmp_count, mov_count); +} +