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

排序操作 #177

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
157 changes: 157 additions & 0 deletions 2017-1/luyj/Hash.c
Original file line number Diff line number Diff line change
@@ -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 <i; j++)
{
if (i%j == 0)
{
flag = 1;
break;
}
}
if (0 == flag)
{
p = i;
break;
}
}
return p;
}

int Hash(int k, int p)
{
//�ؼ��ֱ������ڱ�����������������Ľ��,����ȡ�ؼ�������λ��

return k%p;
}

Bool EQ(KeyType k, KeyType key)
{
if (k == key)
{
return TRUE;
}
else
{
return FALSE;
}
}

Status collision(int *q,int p)
{
(*q)++;
(*q) = (*q) % p;
return OK;
}

Status RecreateHashTable(HashTable *h)
{
int i;
int count = h->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++;
}
}
47 changes: 47 additions & 0 deletions 2017-1/luyj/Hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <malloc.h>
#include <time.h>

#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);
181 changes: 181 additions & 0 deletions 2017-1/luyj/Sort.c
Original file line number Diff line number Diff line change
@@ -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 &&LT(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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

简单选择排序算法理解有误

Copy link
Contributor Author

Choose a reason for hiding this comment

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

师姐,那该怎么理解 [捂脸]

}
}

/*=========�������=========*/
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;
}
Loading