Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 2017-1/li-yc/哈希表/1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/li-yc/哈希表/2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/li-yc/哈希表/3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions 2017-1/li-yc/哈希表/Hashtable.c
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;
}
35 changes: 35 additions & 0 deletions 2017-1/li-yc/哈希表/Hashtable.h
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);
34 changes: 34 additions & 0 deletions 2017-1/li-yc/哈希表/main.c
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;
}
Binary file added 2017-1/li-yc/排序/1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/li-yc/排序/2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 131 additions & 0 deletions 2017-1/li-yc/排序/Sort.c
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;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再次理解简单选择排序算法原理

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;
}
28 changes: 28 additions & 0 deletions 2017-1/li-yc/排序/Sort.h
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);
59 changes: 59 additions & 0 deletions 2017-1/li-yc/排序/main.c
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;
}