-
Notifications
You must be signed in to change notification settings - Fork 46
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
实现了必选的排序 #171
Open
Lyc-heng
wants to merge
2
commits into
CUCCS:master
Choose a base branch
from
Lyc-heng:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
实现了必选的排序 #171
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
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.
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 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,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; | ||
} |
This file contains 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,35 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
#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); |
This file contains 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,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; | ||
} |
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 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,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; | ||
} |
This file contains 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,28 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
#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); |
This file contains 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,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; | ||
} |
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.
再次理解简单选择排序算法原理