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

Homework9 各种排序算法 #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 64 additions & 0 deletions 2017-1/Jasmine/exp09/冒泡排序.c
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)++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 一次交换的移动次数是3次

}
}
}
}
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);
}
73 changes: 73 additions & 0 deletions 2017-1/Jasmine/exp09/希尔排序.c
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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
77 changes: 77 additions & 0 deletions 2017-1/Jasmine/exp09/快速排序.c
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);
}
59 changes: 59 additions & 0 deletions 2017-1/Jasmine/exp09/直接插入排序.c
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);
}


}
69 changes: 69 additions & 0 deletions 2017-1/Jasmine/exp09/简单选择排序.c
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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

第i趟简单选择排序是指通过n−i次关键字的比较,从n−i+1个记录中选出关键字最小的记录,并与第i个记录进行交换

  • 你这样写也能排序成功,但是已经脱离选择排序的初衷了

{
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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 最后,这些排序算法完全可以写在一个程序文件中不同的方法。复用代码更值得去学习。
  • 源程序文件名不要使用中文,注意细节!