-
Notifications
You must be signed in to change notification settings - Fork 46
【Sv】哈希表建立和查找,线性解决冲突+排序作业 #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| #include"Sort.h" | ||
| void Print(RecordType r[], int length) | ||
| { | ||
| for (int i = 1; i <= length; i++) | ||
| { | ||
| printf("%d ", r[i]); | ||
| } | ||
| printf("\n"); | ||
| } | ||
| void InsSort(RecordType r[], int length,int *CpNumber,int *MovNumber) | ||
| { | ||
| *CpNumber = 0;//�Ƚϴ��� | ||
| *MovNumber = 0;//�ƶ����� | ||
| int i, j; | ||
| for ( i = 2; i <=length; i++) { | ||
| r[0] = r[i]; /* ���������¼��ŵ���ʱ������ */ | ||
| (*MovNumber)++; | ||
| j = i - 1; /* ���һ����������ı߽�λ�� */ | ||
| while ((++(*CpNumber))&&(r[0] < r[j])) { /* Ѱ�Ҳ���λ�� */ | ||
| r[j + 1] = r[j]; /* ��С��������Ԫ������ */ | ||
| (*MovNumber)++; | ||
| j = j - 1; /* ������Ԫ��������������һ��������һλ��Ԫ�ؼ������бȽ� */ | ||
| } | ||
| r[j + 1] = r[0]; /* ���������¼���뵽������������� */ | ||
| (*MovNumber)++; | ||
| } | ||
| Print(r, length); | ||
| } | ||
| void ShellInsert(RecordType r[], int length, int delta,int *CpNumber, int *MovNumber) { | ||
| int i,j; | ||
| *CpNumber = 0; | ||
| *MovNumber = 0; | ||
| for (i = 1 + delta; i <= length; i++) { /* 1+deltaΪ��һ�������еĵڶ���Ԫ�ص��±� */ | ||
| if ((++(*CpNumber))&&(r[i] < r[i - delta])) { | ||
| r[0] = r[i]; /* ����r[i] (����������) */ | ||
| for (j = i - delta; j > 0 &&(++*(CpNumber))&& r[0] < r[j] ; j -= delta) { | ||
| r[j + delta] = r[j]; | ||
| (*MovNumber)++; | ||
| } | ||
| r[j + delta] = r[0]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* �Լ�¼����r��ϣ������ lengthΪ����ij���*/ | ||
| void ShellSort(RecordType r[], int length,int *CpNumber, int *MovNumber) { | ||
| int d = length / 2; | ||
| while (d >= 1) { | ||
| ShellInsert(r, length, d,CpNumber,MovNumber); | ||
| d = d / 2; | ||
| } | ||
| Print(r, length); | ||
| } | ||
|
|
||
| void Copy(RecordType a[], RecordType b[], int c) | ||
| { | ||
| for (int i = 0; i <= c; i++) | ||
| { | ||
| a[i] = b[i]; | ||
| } | ||
| } | ||
|
|
||
| void BubbleSort(RecordType r[], int length,int *c,int *m) { | ||
| *c = 0; *m = 0; | ||
| int n = length,i,j,x; | ||
| int change = 1;//TRUE | ||
| for (i = 1; i <= n - 1 && change; ++i) { | ||
| change = 0;//FALSE | ||
| for (j = 1; j <= n - i; ++j) { | ||
| if ((++(*c))&&r[j] > r[j + 1]) { | ||
| x = r[j]; | ||
| r[j] = r[j + 1]; | ||
| r[j + 1] = x; | ||
| (*m)++; | ||
| change = 1; | ||
| } | ||
| } | ||
| } | ||
| Print(r, length); | ||
| } | ||
|
|
||
| int QKPass(RecordType r[], int left, int right, int *c, int *m) { | ||
| int x = r[left]; /* ѡ�����¼*/ | ||
| int low = left; | ||
| int high = right; | ||
| while (low < high) { | ||
| while (low < high &&(++(*c))&& r[high] >= x) { /* high���ҵ�����С��x.key�ļ�¼ */ | ||
| high--; | ||
| } | ||
| r[low] = r[high]; /* �ҵ�С��x.key�ļ�¼������н��� */ | ||
| ++(*m); | ||
| while (low < high && (++(*c)) && r[low] < x) { /* low�������Ҳ�С��x.key�ļ�¼ */ | ||
| low++; | ||
| } | ||
| r[high] = r[low]; /* �ҵ���С��x.key�ļ�¼����*/ | ||
| ++(*m); | ||
| } | ||
|
|
||
| r[low] = x; /* ������¼���浽low=high��λ�� */ | ||
| ++(*m); | ||
| return low; /*���ػ���¼��λ��*/ | ||
| } | ||
|
|
||
| void QKSort(RecordType r[], int low, int high,int *c,int *m) { | ||
| if (low < high) { | ||
| int pivot = QKPass(r, low, high,c,m); | ||
| QKSort(r, low, pivot - 1,c,m); | ||
| QKSort(r, pivot + 1, high,c,m); | ||
| } | ||
| } | ||
|
|
||
| void SelectSort(RecordType r[], int length, int *c, int *m) | ||
| { | ||
| *c = 0; *m = 0; | ||
| int n = length; | ||
| int i,k,j; | ||
| for (i = 1; i <= n-1; ++i) { | ||
| k = i; | ||
| for (j = i + 1; j <= n; ++j) { | ||
| if ((++(*c))&&r[j] < r[k]) { | ||
| k = j; | ||
| } | ||
| } | ||
|
|
||
| if (k != i) { | ||
| int x = r[i]; | ||
| r[i] = r[k]; | ||
| r[k] = x; | ||
| (*m)++; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
| Print(r, 10); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #include<stdio.h> | ||
| #include<stdlib.h> | ||
| #include<time.h> | ||
| typedef int RecordType; | ||
| void Print(RecordType r[], int length); | ||
| void InsSort(RecordType r[], int length,int *a,int *b); | ||
| void ShellInsert(RecordType r[], int length, int delta,int *,int *); | ||
| void ShellSort(RecordType r[], int length,int *,int *); | ||
| void Copy(RecordType a[], RecordType b[], int c); | ||
| void BubbleSort(RecordType r[], int length, int *, int *); | ||
| void QKSort(RecordType r[], int low, int high,int *,int *); | ||
| void SelectSort(RecordType r[], int length, int *, int *); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #include"Sort.h" | ||
| int main() | ||
| { | ||
| RecordType r[11]; | ||
| RecordType temp[11]; | ||
| srand(time(0)); | ||
| temp[0] = 0; | ||
| for (int i = 1; i < 11; i++) | ||
| { | ||
| temp[i] = rand() % 100; | ||
| } | ||
| int a, b; | ||
| int *CpNumber, *MovNumber; | ||
| CpNumber = &a; MovNumber = &b; | ||
| printf("����\n"); | ||
| Print(temp, 10); | ||
| Copy(r, temp,10); | ||
| printf("\n��������\n"); | ||
| InsSort(r, 10,CpNumber,MovNumber); | ||
| printf("�Ƚϴ�����%d���ƶ�������%d\n\n", (*CpNumber), (*MovNumber)); | ||
| Copy(r, temp, 10); | ||
| printf("ϣ������\n"); | ||
| ShellSort(r, 10, CpNumber, MovNumber); | ||
| printf("�Ƚϴ�����%d���ƶ�������%d\n\n", (*CpNumber), (*MovNumber)); | ||
| Copy(r, temp, 10); | ||
| printf("����\n"); | ||
| BubbleSort(r, 10, CpNumber, MovNumber); | ||
| printf("�Ƚϴ�����%d���ƶ�������%d\n\n", (*CpNumber), (*MovNumber)); | ||
| Copy(r, temp, 10); | ||
| *CpNumber = 0; *MovNumber = 0; | ||
| printf("��������\n"); | ||
| QKSort(r, 1, 10, CpNumber, MovNumber); | ||
| Print(r, 10); | ||
| printf("�Ƚϴ�����%d���ƶ�������%d\n\n", (*CpNumber), (*MovNumber)); | ||
| Copy(r, temp, 10); | ||
| printf("��ѡ������\n"); | ||
| SelectSort(r, 10, CpNumber, MovNumber); | ||
| printf("�Ƚϴ�����%d���ƶ�������%d\n\n", (*CpNumber), (*MovNumber)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #include"OHash.h" | ||
| bool isSushu(int a) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| for (int i = 2; i < sqrt(a)+2; i++) | ||
|
|
||
| { | ||
| if (a%i == 0)return false; | ||
| } | ||
| return true; | ||
| } | ||
| int hashsize(int size) | ||
| { | ||
| int p = size; | ||
| if (p == 1)return 1; | ||
| while (!isSushu(p--));//��size��ʼ���µ������ĵ�һ���������˴�������� | ||
| return p + 1; | ||
| } | ||
| int Hash(KeyType K, int size) | ||
| { | ||
| int p = hashsize(size); | ||
| return K%p; | ||
| } | ||
| bool EQ(int a, int b) { return(a == b); } | ||
| void collision(int *p, int *c){ (*p)++ ; (*c)++; } | ||
|
|
||
| HashTable NewHashTable(int size) | ||
| { | ||
| HashTable temp; | ||
| temp.count = 0; | ||
| temp.sizeindex = size; | ||
| temp.elem = (ElemType*)malloc(size * sizeof(ElemType)); | ||
| for (int i = 0; i < size; i++) | ||
| { | ||
| temp.elem[i].key = -1; | ||
| temp.elem[i].val = 0; | ||
| } | ||
| return temp; | ||
| } | ||
| void RecreateHashTable(HashTable *H) | ||
| { | ||
| HashTable temp = NewHashTable(H->sizeindex * 2); | ||
| int a = H->sizeindex; | ||
| for (int i = 0; i <a; i++) | ||
| { | ||
| temp.elem[i] = H->elem[i]; | ||
| } | ||
| H->sizeindex *= 2; | ||
| H->elem= (ElemType*)malloc(H->sizeindex * sizeof(ElemType)); | ||
| for (int i = 0; i < H->sizeindex; i++) | ||
| { | ||
| H->elem[i].key = -1; H->elem[i].val = 0; | ||
| } | ||
| for (int i = 0; i < a; i++) | ||
| { | ||
| int p = Hash(temp.elem[i].key, H->sizeindex); | ||
| H->elem[p] = temp.elem[i]; | ||
| } | ||
| } | ||
| Status SearchHash(HashTable H, KeyType K, int *p, int *c) | ||
| { | ||
| *p = Hash(K,H.sizeindex); // ��ù�ϣ��ַ | ||
| while (H.elem[*p].key != NULLKEY && !EQ(K, H.elem[*p].key)) { | ||
| collision(p, c); //��ͻ�������һ̽���ַ p | ||
| //printf("***"); | ||
| } | ||
|
|
||
| if (EQ(K, H.elem[*p].key)) { // ���ҳɹ���p���ش�������Ԫ��λ�� | ||
| return SUCCESS; | ||
| } | ||
| else { | ||
| return FAILED; // ���Ҳ��ɹ� | ||
| } | ||
| } | ||
| Status InsertHash(HashTable &H, ElemType e) | ||
| { | ||
| // ���Ҳ��ɹ�ʱ��������Ԫ��e�����ŵ�ַ��ϣ��H�У�������OK | ||
| // ����ͻ�����������ؽ���ϣ�� | ||
| int a = 0,b=0; | ||
| int *c ;//���ڼ�¼��ͻ���� | ||
| c = &a; | ||
| int *p ; | ||
| p = &b; | ||
| if (SearchHash(H, e.key, p, c) == SUCCESS) { // ���������� e ����ͬ�ؼ��ֵ�Ԫ�� | ||
| if ((*c) != 0) | ||
| { | ||
| printf("\n��ϣ��ͻ��������%d���ؼ���ֵ��%d->%d\n", *c, e.key, e.val); | ||
| } | ||
| return DUPLICATE; | ||
| } | ||
|
|
||
| else if (*c < hashsize(H.sizeindex) / 2) { | ||
| // ��ͻ���� c δ�ﵽ���ޣ�����ֵ�ɵ����˴���Ϊʾ���� | ||
| H.elem[*p] = e; | ||
| ++H.count; | ||
| return OK; | ||
| } | ||
| else { | ||
| // �ؽ���ϣ������������¿��ܹ�ϣ������������ | ||
| // ����ͨ�����ؽ����̶����������¹�ϣ�������� | ||
| RecreateHashTable(&H); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include<stdio.h> | ||
| #include<stdlib.h> | ||
| #include<time.h> | ||
| #include<math.h> | ||
| #define NULLKEY -1 | ||
| //----------------------------------- | ||
| typedef int KeyType; | ||
| typedef int ValueType; | ||
| typedef struct _ElemType { | ||
| KeyType key; // �ؼ��� | ||
| ValueType val; // ֵ | ||
| #ifdef CHAINED_HASH | ||
| struct _ElemType *next; | ||
| #endif | ||
| } ElemType; | ||
| typedef struct _bool{ true,false }bool; | ||
| typedef struct { | ||
| ElemType *elem;//��ַ | ||
| int count; // ��ǰ����Ԫ�ظ��� | ||
| int sizeindex; // hashsize[sizeindex]Ϊ��ǰ���� | ||
| } HashTable; | ||
| typedef enum _status | ||
| { | ||
| SUCCESS, FAILED, DUPLICATE,OK | ||
| }Status; | ||
| //----------------------------------- | ||
| bool EQ(int, int); | ||
| bool isSushu(int a);//�ж��Ƿ�Ϊ���� | ||
| int hashsize(int size); | ||
| void RecreateHashTable(HashTable *H); | ||
| Status SearchHash(HashTable H, KeyType K, int *p, int *c);//��ϣ���в��ҹؼ�ֵΪK�ļ�¼��c��¼������p���ص�ַ | ||
| int Hash(KeyType,int size); | ||
| Status InsertHash(HashTable &H, ElemType e); | ||
| HashTable NewHashTable(int size); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.