diff --git "a/2017-1/Jasmine/exp09/\345\206\222\346\263\241\346\216\222\345\272\217.c" "b/2017-1/Jasmine/exp09/\345\206\222\346\263\241\346\216\222\345\272\217.c" new file mode 100644 index 00000000..08cc4cef --- /dev/null +++ "b/2017-1/Jasmine/exp09/\345\206\222\346\263\241\346\216\222\345\272\217.c" @@ -0,0 +1,64 @@ +#include +#include +#include +#define Max 20 +typedef struct List +{ + int data[Max];//data[0]作为监视哨位 + int length; +}SqList;//List的结构定义 +void BubbleSort(SqList *l,int* compare,int* move) +{ + int i, j; + for (i = 0;i < l->length;i++) + { + for (j = i + 1;j < l->length;j++) + { + (*compare)++; + if (l->data[i] > l->data[j]) + { + int temp = l->data[i]; + l->data[i] = l->data[j]; + l->data[j] = temp; + (*move)++; + } + } + } +} +void Print(SqList*l) +{ + int i; + for (i = 0;i < l->length;i++) + { + printf("%d ", l->data[i]); + + } +} +int main() +{ + srand(time(NULL)); + SqList L; + int length;//随机生成列表长度 + do + { + length = rand() % 10; + } while (length < 5); + L.length = length; + int i, a; + int compare, move; + compare = move = 0; + for (i = 0;i < Max;i++) + { + L.data[i] = 0; + } + for (i = 0;i < length;i++) + { + a = rand() % 20;//随机生成列表元素 + L.data[i] = a; + } + Print(&L); + BubbleSort(&L,&compare,&move); + printf("\nAfter Sort:\n"); + Print(&L); + printf("\ncompare time:%d\nmove time:%d\n", compare, move); +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp09/\345\270\214\345\260\224\346\216\222\345\272\217.c" "b/2017-1/Jasmine/exp09/\345\270\214\345\260\224\346\216\222\345\272\217.c" new file mode 100644 index 00000000..fa2b7b93 --- /dev/null +++ "b/2017-1/Jasmine/exp09/\345\270\214\345\260\224\346\216\222\345\272\217.c" @@ -0,0 +1,73 @@ +#include +#include +#include +#define Max 20 +typedef struct List +{ + int data[Max]; + int length; +}SqList;//List的结构定义 +void ShellSort(SqList *l) +{ + int gap = l->length / 2; //设置步长为list长度的一半 + for (;gap >= 1;gap = gap / 2) + { //步长每次减半,直至步长为一,进行直接插入排序 + int m; + for (m = 0;m < gap;m++) + { //找到每个子序列的起点,m,m+gap,m+gap+gap...则我们对这个序列进行直接插入排序 + int i; + int temp; + for (i = m + gap;i < l->length;i = i + gap) + { + int j = i; + while (j != m) + { + if (l->data[j] <= l->data[j - gap]) + { + temp = l->data[j - gap]; + l->data[j - gap] = l->data[j]; + l->data[j] = temp; + } + j = j - gap; //下标跳转到下一个元素 + } + } + } + } +} +void Print(SqList*l) +{ + int i; + for (i = 0;i < l->length;i++) + { + printf("%d ", l->data[i]); + + } +} +int main() +{ + srand(time(NULL)); + SqList L; + int length;//随机生成列表长度 + do + { + length = rand() % 10; + } while (length < 5); + L.length = length; + int i, a; + int compare, move; + compare = move = 0; + for (i = 0;i < Max;i++) + { + L.data[i] = 0;//初始化data数组 + } + for (i = 0;i < length;i++) + { + a = rand() % 20;//随机生成列表元素 + L.data[i] = a;//依次插入数据进入数组 + } + Print(&L);//打印无序列表 + ShellSort(&L, 0, L.length - 1); + printf("\nAfter Sort:\n"); + Print(&L); + //printf("\ncompare time:%d\nmove time:%d\n", compare, move); +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp09/\345\277\253\351\200\237\346\216\222\345\272\217.c" "b/2017-1/Jasmine/exp09/\345\277\253\351\200\237\346\216\222\345\272\217.c" new file mode 100644 index 00000000..4cccb303 --- /dev/null +++ "b/2017-1/Jasmine/exp09/\345\277\253\351\200\237\346\216\222\345\272\217.c" @@ -0,0 +1,77 @@ +#include +#include +#include +#define Max 20 +typedef struct List +{ + int data[Max]; + int length; +}SqList;//List的结构定义 + +int FindPos(SqList*l, int i, int j) +{ + int val = l->data[i]; + while (idata[j] >= val) + { //表示只有当前指针没有重合并且当前右边j指向的值大于val时,才会向左移动j + j = j - 1; + } + l->data[i] = l->data[j]; //将比val小的值赋到i + while (idata[i] <= val) + { + i = i + 1; + } + l->data[j] = l->data[i]; //将比val大的值赋到j上 + + } + l->data[i] = val; + return j; +} +void QuickSort(SqList* l, int low, int high)//快速排序 +{ + int pos; + if (low < high) + { + pos = FindPos(l, low, high); + QuickSort(l, low, pos - 1); + QuickSort(l, pos + 1, high); + } +} +void Print(SqList*l) +{ + int i; + for (i = 0;i < l->length;i++) + { + printf("%d ", l->data[i]); + + } +} +int main() +{ + srand(time(NULL)); + SqList L; + int length;//随机生成列表长度 + do + { + length = rand() % 10; + } while (length < 5); + L.length = length; + int i, a; + int compare, move; + compare = move = 0; + for (i = 0;i < Max;i++) + { + L.data[i] = 0;//初始化data数组 + } + for (i = 0;i < length;i++) + { + a = rand() % 20;//随机生成列表元素 + L.data[i] = a;//依次插入数据进入数组 + } + Print(&L);//打印无序列表 + QuickSort(&L, 0, L.length - 1); + printf("\nAfter Sort:\n"); + Print(&L); + //printf("\ncompare time:%d\nmove time:%d\n", compare, move); +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp09/\347\233\264\346\216\245\346\217\222\345\205\245\346\216\222\345\272\217.c" "b/2017-1/Jasmine/exp09/\347\233\264\346\216\245\346\217\222\345\205\245\346\216\222\345\272\217.c" new file mode 100644 index 00000000..5b059291 --- /dev/null +++ "b/2017-1/Jasmine/exp09/\347\233\264\346\216\245\346\217\222\345\205\245\346\216\222\345\272\217.c" @@ -0,0 +1,59 @@ +#include +#include +#include +#define Max 20 +typedef struct List +{ + int data[Max];//data[0]作为监视哨位 + int length; +}SqList;//List的结构定义 +void InsertSort(SqList *l,int a) +{ + int i, j; + l->length++; + l->data[0] = a;//将插入的数据先放入监视哨位 + i = l->length - 1; + while (l->data[0] < l->data[i]) + { + l->data[i + 1] = l->data[i];//数据后移 + i--; + } + l->data[i+1] = l->data[0];//合理位置插入数据 +} + +void Print(SqList*l) +{ + int i; + for (i = 1;i < l->length;i++) + { + printf("%d ", l->data[i]); + + } + //printf("compare time:%d\nmove time:%d\n", compare, move); +} +int main() +{ + srand(time(NULL)); + SqList L; + L.length = 0; + int length;//随机生成列表长度 + do + { + length = rand() % 10; + } while (length < 5); + int i,a; + int compare, move; + compare = move = 0; + for (i = 0;i < Max;i++) + { + L.data[i] = 0; + } + for (i = 0;i < length;i++) + { + a = rand() % 20;//随机生成列表元素 + InsertSort(&L, a); + Print(&L); + } + + +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp09/\347\256\200\345\215\225\351\200\211\346\213\251\346\216\222\345\272\217.c" "b/2017-1/Jasmine/exp09/\347\256\200\345\215\225\351\200\211\346\213\251\346\216\222\345\272\217.c" new file mode 100644 index 00000000..aaec0af1 --- /dev/null +++ "b/2017-1/Jasmine/exp09/\347\256\200\345\215\225\351\200\211\346\213\251\346\216\222\345\272\217.c" @@ -0,0 +1,69 @@ +#include +#include +#include +#define Max 20 +typedef struct List +{ + int data[Max]; + int length; +}SqList;//List的结构定义 +void SelectSort(SqList* l, int* compare, int* move) +{ + int min; + for (int i = 0; i < l->length-1; i++) + { + min = i; + for (int j = i + 1; j < l->length; j++) + { + (*compare)++; + if (l->data[min] > l->data[j]) + { + min = j; + } + } + if (min != i) + { + int temp = l->data[i]; + l->data[i] = l->data[min]; + l->data[min] = temp; + (*move)++; + } + } +} +void Print(SqList*l) +{ + int i; + for (i = 0;i < l->length;i++) + { + printf("%d ", l->data[i]); + + } +} +int main() +{ + srand(time(NULL)); + SqList L; + int length;//随机生成列表长度 + do + { + length = rand() % 10; + } while (length < 5); + L.length = length; + int i, a; + int compare, move; + compare = move = 0; + for (i = 0;i < Max;i++) + { + L.data[i] = 0;//初始化data数组 + } + for (i = 0;i < length;i++) + { + a = rand() % 20;//随机生成列表元素 + L.data[i] = a;//依次插入数据进入数组 + } + Print(&L);//打印无序列表 + SelectSort(&L, &compare, &move); + printf("\nAfter Sort:\n"); + Print(&L); + printf("\ncompare time:%d\nmove time:%d\n", compare, move); +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp10/\345\223\210\345\270\214\345\244\247\344\275\234\344\270\232.c" "b/2017-1/Jasmine/exp10/\345\223\210\345\270\214\345\244\247\344\275\234\344\270\232.c" new file mode 100644 index 00000000..45d5dd2a --- /dev/null +++ "b/2017-1/Jasmine/exp10/\345\223\210\345\270\214\345\244\247\344\275\234\344\270\232.c" @@ -0,0 +1,253 @@ +#include +#include +#include +#include + +#define SUCCESS 1 +#define FAILED 0 +#define DUPLICATE -1 +#define NULLKEY -1 +#define NONEVALUE 0 + +typedef int KeyType; +typedef int ValueType; + +typedef enum { + OK, + ERROR +}Status; + +typedef enum +{ + FALSE, TRUE +}bool; + +typedef struct _ElemType +{ + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; + +typedef struct { + ElemType *elem; // 储存哈希表元素 + int count; // 元素个数 + int sizeindex; // 容量 +} HashTable; + +Status CreateHash(HashTable *H, int size); // 构建Hash表 +bool Empty(HashTable H); // 判断Hash表是否为空 +bool Prime(int n); // 判断是否为素数 +int SearchHash(HashTable H, KeyType K, int *p, int *c); // 在Hash表中查找关键字为K的元素 +Status InsertHash(HashTable *H, ElemType e); // 往Hash表中插入新的元素 +bool EQ(KeyType a, KeyType b); // 判断关键字是否相等 +Status RecreateHashTable(HashTable *H); // 重建Hash表 +Status Print(HashTable H); // 输出Hash表 + +Status CreateHash(HashTable *H, int size) { + // 构建Hash表 + ElemType temp; + int i; + + (*H).sizeindex = size; + (*H).count = 0; + (*H).elem = (ElemType *)malloc(sizeof(ElemType) * size); + + if (!((*H).elem)) { + return ERROR; + } + + for (i = 0; i < size; i++) { + (*H).elem[i].key = NULLKEY; + (*H).elem[i].val = NONEVALUE; + } + + printf("==========Creating HashTable==========\n"); + printf("HashTable size:%d\n", (*H).sizeindex); + for (i = 0; H->count < size / 2; i++) { + temp.key = (int)rand() % 501; + temp.val = (int)rand() % 501; + InsertHash(H, temp); + } + printf("Crating Over\n\n"); + + return OK; +} + +bool Empty(HashTable H) { + // 判断Hash表是否为空,即没有元素 + if (H.count == 0) { + return TRUE; + } + + return FALSE; +} + +bool Prime(int n) { + // 判断一个数是否为素数 + if (n == 2) { + return TRUE; + } + + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) { + return FALSE; + } + } + + return TRUE; +} + +int SearchHash(HashTable H, KeyType K, int *p, int *c) { + // 在开放定址哈希表H中查找关键值为K的记录 + *p = K % H.sizeindex; // 求得哈希地址 + *c = 0; // c用于记录冲突次数,初值置0 + while (H.elem[*p].key != NULLKEY && !EQ(K, H.elem[*p].key)) { + if ((*p) < H.sizeindex) { + printf("Collide with %d->%d\n", H.elem[*p].key, H.elem[*p].val); + (*p)++; + (*c)++; + } + else + break; + } + if (EQ(K, H.elem[*p].key)) { // 查找成功,p返回待查数据元素位置 + return SUCCESS; + } + else { + return FAILED; // 查找不成功 + } +} + +Status InsertHash(HashTable *H, ElemType e) { + // 查找不成功时插入数据元素e到开放地址哈希表H中,并返回OK + // 若冲突次数过大或找到末尾,则重建哈希表 + int c, p; + if (SearchHash(*H, e.key, &p, &c) == SUCCESS) + //表中已有与 e 有相同关键字的元素 + { + return ERROR; + } + else { + if (p == (*H).sizeindex || c > (*H).sizeindex / 2) { + RecreateHashTable(H); + } + (*H).elem[p] = e; + ++(*H).count; + printf("Collision times:%d\n", c); + printf("Insert:%d->%d\n", H->elem[p].key, H->elem[p].val); + printf("--------------------------\n"); + return OK; + } +} + +bool EQ(KeyType a, KeyType b) { + // 判断关键值a是否等于b + if (a == b) { + return TRUE; + } + + return FALSE; +} + +Status RecreateHashTable(HashTable *H) { + // 重建Hash表,即扩充(一个麻烦的做法) + int i, n; + HashTable temp; // 临时Hash表 + ElemType e; + + if (Empty(*H)) { + return ERROR; + } + + e.key = NULLKEY; + e.val = NONEVALUE; + n = 2 * (*H).sizeindex; + + while (!Prime(n)) { + n++; + }; + + temp.sizeindex = n; + temp.count = (*H).count; + temp.elem = (ElemType *)malloc(sizeof(ElemType)*n); + for (i = 0; i < n; i++) { + temp.elem[i].key = NULLKEY; + temp.elem[i].val = NONEVALUE; + } + for (i = 0; i < (*H).sizeindex; i++) { + temp.elem[i].key = (*H).elem[i].key; + temp.elem[i].val = (*H).elem[i].val; + } + + (*H).sizeindex = temp.sizeindex; + (*H).count = temp.count; + (*H).elem = (ElemType *)malloc(sizeof(ElemType)*n); + for (i = 0; i < n; i++) { + (*H).elem[i].key = temp.elem[i].key; + (*H).elem[i].val = temp.elem[i].val; + } + + free(temp.elem); + + return OK; +} + +Status Print(HashTable H) { + // 遍历输出Hash表 + if (Empty(H)) { + return ERROR; + } + + printf("==========Print HashTable==========\n"); + for (int i = 0; i < H.sizeindex; i++) + printf("[%d] : %d->%d\n", i, H.elem[i].key, H.elem[i].val); + printf("Print Over\n"); + printf("\n"); + + return OK; +} + +int main() +{ + int size, i; + int p, c; + HashTable H; + ElemType e; + + srand(time(NULL)); // 随机种子 + + do { + size = (int)rand() % 11 + 20; // 构建哈希表,大小在20~30之间 + } while (!Prime(size)); + + CreateHash(&H, size); + Print(H); + + p = c = 0; + printf("==========Searching==========\n"); + for (i = 0; i < H.sizeindex; i++) { + e.key = (int)rand() % 501; + printf("Search : %d\n", e.key); + if (SearchHash(H, e.key, &p, &c) == SUCCESS) { + printf("Find %d\n", e.key); + printf("The Element Is : %d->%d\n", H.elem[p].key, H.elem[p].val); + printf("--------------------------\n"); + } + else { + printf("Didn't Find %d\n", e.key); + printf("--------------------------\n"); + } + } + printf("Search Over\n\n"); + + printf("==========Rebuild HashTable==========\n"); + if (!RecreateHashTable(&H)) { + printf("Rebuild Over\n"); + Print(H); + } + + return 0; +} \ No newline at end of file