-
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
Homework9 各种排序算法 #176
base: master
Are you sure you want to change the base?
Homework9 各种排序算法 #176
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include<stdio.h> | ||
#include<time.h> | ||
#include<stdlib.h> | ||
#define Max 20 | ||
typedef struct List | ||
{ | ||
int data[Max];//data[0]��Ϊ������λ | ||
int length; | ||
}SqList;//List�Ľṹ���� | ||
void BubbleSort(SqList *l,int* compare,int* move) | ||
{ | ||
int i, j; | ||
for (i = 0;i < l->length;i++) | ||
{ | ||
for (j = i + 1;j < l->length;j++) | ||
{ | ||
(*compare)++; | ||
if (l->data[i] > l->data[j]) | ||
{ | ||
int temp = l->data[i]; | ||
l->data[i] = l->data[j]; | ||
l->data[j] = temp; | ||
(*move)++; | ||
} | ||
} | ||
} | ||
} | ||
void Print(SqList*l) | ||
{ | ||
int i; | ||
for (i = 0;i < l->length;i++) | ||
{ | ||
printf("%d ", l->data[i]); | ||
|
||
} | ||
} | ||
int main() | ||
{ | ||
srand(time(NULL)); | ||
SqList L; | ||
int length;//������������ | ||
do | ||
{ | ||
length = rand() % 10; | ||
} while (length < 5); | ||
L.length = length; | ||
int i, a; | ||
int compare, move; | ||
compare = move = 0; | ||
for (i = 0;i < Max;i++) | ||
{ | ||
L.data[i] = 0; | ||
} | ||
for (i = 0;i < length;i++) | ||
{ | ||
a = rand() % 20;//��������б�Ԫ�� | ||
L.data[i] = a; | ||
} | ||
Print(&L); | ||
BubbleSort(&L,&compare,&move); | ||
printf("\nAfter Sort:\n"); | ||
Print(&L); | ||
printf("\ncompare time:%d\nmove time:%d\n", compare, move); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include<stdio.h> | ||
#include<time.h> | ||
#include<stdlib.h> | ||
#define Max 20 | ||
typedef struct List | ||
{ | ||
int data[Max]; | ||
int length; | ||
}SqList;//List�Ľṹ���� | ||
void ShellSort(SqList *l) | ||
{ | ||
int gap = l->length / 2; //���ò���Ϊlist���ȵ�һ�� | ||
for (;gap >= 1;gap = gap / 2) | ||
{ //����ÿ�μ��룬ֱ������Ϊһ������ֱ�Ӳ������� | ||
int m; | ||
for (m = 0;m < gap;m++) | ||
{ //�ҵ�ÿ�������е���㣬m,m+gap,m+gap+gap...�����Ƕ�������н���ֱ�Ӳ������� | ||
int i; | ||
int temp; | ||
for (i = m + gap;i < l->length;i = i + gap) | ||
{ | ||
int j = i; | ||
while (j != m) | ||
{ | ||
if (l->data[j] <= l->data[j - gap]) | ||
{ | ||
temp = l->data[j - gap]; | ||
l->data[j - gap] = l->data[j]; | ||
l->data[j] = temp; | ||
} | ||
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.
|
||
j = j - gap; //�±���ת����һ��Ԫ�� | ||
} | ||
} | ||
} | ||
} | ||
} | ||
void Print(SqList*l) | ||
{ | ||
int i; | ||
for (i = 0;i < l->length;i++) | ||
{ | ||
printf("%d ", l->data[i]); | ||
|
||
} | ||
} | ||
int main() | ||
{ | ||
srand(time(NULL)); | ||
SqList L; | ||
int length;//������������ | ||
do | ||
{ | ||
length = rand() % 10; | ||
} while (length < 5); | ||
L.length = length; | ||
int i, a; | ||
int compare, move; | ||
compare = move = 0; | ||
for (i = 0;i < Max;i++) | ||
{ | ||
L.data[i] = 0;//��ʼ��data���� | ||
} | ||
for (i = 0;i < length;i++) | ||
{ | ||
a = rand() % 20;//��������б�Ԫ�� | ||
L.data[i] = a;//���β������ݽ������� | ||
} | ||
Print(&L);//��ӡ�����б� | ||
ShellSort(&L, 0, L.length - 1); | ||
printf("\nAfter Sort:\n"); | ||
Print(&L); | ||
//printf("\ncompare time:%d\nmove time:%d\n", compare, move); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include<stdio.h> | ||
#include<time.h> | ||
#include<stdlib.h> | ||
#define Max 20 | ||
typedef struct List | ||
{ | ||
int data[Max]; | ||
int length; | ||
}SqList;//List�Ľṹ���� | ||
|
||
int FindPos(SqList*l, int i, int j) | ||
{ | ||
int val = l->data[i]; | ||
while (i<j) | ||
{ //��һ�ο���������ֻҪi��ֵ��jС�����Ǿ�Ӧ��ȥ�ƶ�����Ϊ��ǰ�������û����� | ||
while (i<j&&l->data[j] >= val) | ||
{ //��ʾֻ�е�ǰָ��û���غϲ��ҵ�ǰ�ұ�jָ���ֵ����valʱ���Ż������ƶ�j | ||
j = j - 1; | ||
} | ||
l->data[i] = l->data[j]; //����valС��ֵ����i | ||
while (i<j&&l->data[i] <= val) | ||
{ | ||
i = i + 1; | ||
} | ||
l->data[j] = l->data[i]; //����val���ֵ����j�� | ||
|
||
} | ||
l->data[i] = val; | ||
return j; | ||
} | ||
void QuickSort(SqList* l, int low, int high)//�������� | ||
{ | ||
int pos; | ||
if (low < high) | ||
{ | ||
pos = FindPos(l, low, high); | ||
QuickSort(l, low, pos - 1); | ||
QuickSort(l, pos + 1, high); | ||
} | ||
} | ||
void Print(SqList*l) | ||
{ | ||
int i; | ||
for (i = 0;i < l->length;i++) | ||
{ | ||
printf("%d ", l->data[i]); | ||
|
||
} | ||
} | ||
int main() | ||
{ | ||
srand(time(NULL)); | ||
SqList L; | ||
int length;//������������ | ||
do | ||
{ | ||
length = rand() % 10; | ||
} while (length < 5); | ||
L.length = length; | ||
int i, a; | ||
int compare, move; | ||
compare = move = 0; | ||
for (i = 0;i < Max;i++) | ||
{ | ||
L.data[i] = 0;//��ʼ��data���� | ||
} | ||
for (i = 0;i < length;i++) | ||
{ | ||
a = rand() % 20;//��������б�Ԫ�� | ||
L.data[i] = a;//���β������ݽ������� | ||
} | ||
Print(&L);//��ӡ�����б� | ||
QuickSort(&L, 0, L.length - 1); | ||
printf("\nAfter Sort:\n"); | ||
Print(&L); | ||
//printf("\ncompare time:%d\nmove time:%d\n", compare, move); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include<stdio.h> | ||
#include<time.h> | ||
#include<stdlib.h> | ||
#define Max 20 | ||
typedef struct List | ||
{ | ||
int data[Max];//data[0]��Ϊ������λ | ||
int length; | ||
}SqList;//List�Ľṹ���� | ||
void InsertSort(SqList *l,int a) | ||
{ | ||
int i, j; | ||
l->length++; | ||
l->data[0] = a;//������������ȷ��������λ | ||
i = l->length - 1; | ||
while (l->data[0] < l->data[i]) | ||
{ | ||
l->data[i + 1] = l->data[i];//���ݺ��� | ||
i--; | ||
} | ||
l->data[i+1] = l->data[0];//����λ�ò������� | ||
} | ||
|
||
void Print(SqList*l) | ||
{ | ||
int i; | ||
for (i = 1;i < l->length;i++) | ||
{ | ||
printf("%d ", l->data[i]); | ||
|
||
} | ||
//printf("compare time:%d\nmove time:%d\n", compare, move); | ||
} | ||
int main() | ||
{ | ||
srand(time(NULL)); | ||
SqList L; | ||
L.length = 0; | ||
int length;//������������ | ||
do | ||
{ | ||
length = rand() % 10; | ||
} while (length < 5); | ||
int i,a; | ||
int compare, move; | ||
compare = move = 0; | ||
for (i = 0;i < Max;i++) | ||
{ | ||
L.data[i] = 0; | ||
} | ||
for (i = 0;i < length;i++) | ||
{ | ||
a = rand() % 20;//��������б�Ԫ�� | ||
InsertSort(&L, a); | ||
Print(&L); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include<stdio.h> | ||
#include<time.h> | ||
#include<stdlib.h> | ||
#define Max 20 | ||
typedef struct List | ||
{ | ||
int data[Max]; | ||
int length; | ||
}SqList;//List�Ľṹ���� | ||
void SelectSort(SqList* l, int* compare, int* move) | ||
{ | ||
int min; | ||
for (int i = 0; i < l->length-1; i++) | ||
{ | ||
min = i; | ||
for (int j = i + 1; j < l->length; j++) | ||
{ | ||
(*compare)++; | ||
if (l->data[min] > l->data[j]) | ||
{ | ||
min = j; | ||
} | ||
} | ||
if (min != i) | ||
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.
|
||
{ | ||
int temp = l->data[i]; | ||
l->data[i] = l->data[min]; | ||
l->data[min] = temp; | ||
(*move)++; | ||
} | ||
} | ||
} | ||
void Print(SqList*l) | ||
{ | ||
int i; | ||
for (i = 0;i < l->length;i++) | ||
{ | ||
printf("%d ", l->data[i]); | ||
|
||
} | ||
} | ||
int main() | ||
{ | ||
srand(time(NULL)); | ||
SqList L; | ||
int length;//������������ | ||
do | ||
{ | ||
length = rand() % 10; | ||
} while (length < 5); | ||
L.length = length; | ||
int i, a; | ||
int compare, move; | ||
compare = move = 0; | ||
for (i = 0;i < Max;i++) | ||
{ | ||
L.data[i] = 0;//��ʼ��data���� | ||
} | ||
for (i = 0;i < length;i++) | ||
{ | ||
a = rand() % 20;//��������б�Ԫ�� | ||
L.data[i] = a;//���β������ݽ������� | ||
} | ||
Print(&L);//��ӡ�����б� | ||
SelectSort(&L, &compare, &move); | ||
printf("\nAfter Sort:\n"); | ||
Print(&L); | ||
printf("\ncompare time:%d\nmove time:%d\n", compare, move); | ||
} | ||
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.
|
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.