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); +} +