使用简单数组实现下面各种排序算法,并进行比较。
排序算法:
1、插入排序
2、希尔排序
3、冒泡排序
4、快速排序
5、简单选择排序
6、堆排序(选作)
7、归并排序(选作)
8、基数排序(选作)
9、其他
要求:
1、测试数据分成三类:正序、逆序、随机数据
2、对于这三类数据,比较上述排序算法中关键字的比较次数和移动次数(其中关键字交换计为3次移动)。
3、对于这三类数据,比较上述排序算法中不同算法的执行时间,精确到微秒(选作)
4、对2和3的结果进行分析,验证上述各种算法的时间复杂度
编写测试main()函数测试线性表的正确性。
参考代码:
标头.h
using namespace std;
class paixu
{
public:
void Insertsort(int r[], int n, int* compare, int* move);
void ShellInsert(int r[], int n, int* compare, int* move);
void Bubblesort(int r[], int n, int* compare, int* move);
int Partion(int r[], int first, int end, int* compare, int* move);
void Qsort(int r[],int n, int i, int j, int* compare, int* move);
void Selectsort(int r[], int n, int* compare, int* move);
void Print(int r[],int n, int q,int compare, int move);
};
void paixu::Print(int r[],int n,int q, int compare, int move)
{
switch (q)
{
case 1:
cout << "插入排序为:" ;
break;
case 2:
cout << "希尔排序为:";
break;
case 3:
cout << "冒泡排序为:" ;
break;
case 4:
cout << "选择排序为:" ;
break;
case 5:
cout << "快速排序为:";
break;
}
for (int i = 0; i= 0; j--)
{
(*compare)++;
(*move)++;
a[j + 1] = a[j];
}
if (j >= 0)
(*compare)++;
a[j + 1] = x;
}
Print(a, n,1, *compare, *move);
}
void paixu::ShellInsert(int r[], int n, int* compare, int* move)//希尔排序
{
int *b = new int[n];
for (int i = 0; i= 1; d = d / 2)
{
for (int i = d; i <= n - 1; i++)//从a[d]往后逐个元素确定是否需要前移
{
if (b[i]= 0) && (x= 0)
(*compare)++;
b[j + d] = x;
}
else (*compare)++;
}
}
Print(b, n, 2, *compare, *move);
}
void paixu::Bubblesort(int r[], int n, int* compare, int* move)//冒泡排序
{
int *c = new int[n];
for (int i = 0; i < n; i++)
{
c[i] = r[i];
}
*compare = 0;
*move = 0;
int x;
for (int j = 0; jj; i--)
{
if (c[i] = zhou)) //查看右侧元素与轴的大小关系
{
(*compare)++;
j--;
}
if (i
源.cpp
#include
#include"标头.h"
using namespace std;
void main()
{
paixu text;
int i;
int compare = 0;
int move = 0;
cout << "请输入一个正序数组" << endl;
int n;
cout << "请输入数组中含有的元素数量:";
cin >> n;
int *r = new int[n];
cout << "请输入数组中的元素:";
for (i = 0; i < n; i++)
cin >> r[i];
text.Insertsort(r, n, &compare, &move);
text.ShellInsert(r, n, &compare, &move);
text.Bubblesort(r, n, &compare, &move);
compare = 0;
move = 0;
int *d = new int[n];
for (i = 0; i < n; i++) { d[i] = r[i]; }
text.Qsort(d, n, 0, n - 1, &compare, &move);
cout << "快速排序结果为:";
for (i = 0; i < n; i++) cout << d[i] << ' ';
cout << endl;
cout << "比较次数为" << compare;
cout << endl;
cout << "移动次数为" << move << '\n' << endl;
text.Selectsort(r, n, &compare, &move);
cout << "请输入一个逆序数组" << endl;
cout << "请输入数组中含有的元素数量:";
cin >> n;
cout << "请输入数组中的元素:";
for (i = 0; i < n; i++) cin >> r[i];
text.Insertsort(r, n, &compare, &move);
text.ShellInsert(r, n, &compare, &move);
text.Bubblesort(r, n, &compare, &move);
compare = 0;
move = 0;
int *t = new int[n];
for (i = 0; i < n; i++)
{ t[i] = r[i]; }
text.Qsort(t, n, 0, n - 1, &compare, &move);
cout << "快速排序结果为:";
for (i = 0; i < n; i++) cout << t[i] << ' ';
cout << endl;
cout << "比较次数为" << compare;
cout << endl;
cout << "移动次数为" << move << '\n' << endl;
text.Selectsort(r, n, &compare, &move);
cout << "请输入一个乱序数组" << endl;
cout << "请输入数组中含有的元素数量:";
cin >> n;
cout << "请输入数组中的元素:";
for (i = 0; i < n; i++) cin >> r[i];
text.Insertsort(r, n, &compare, &move);
text.ShellInsert(r, n, &compare, &move);
text.Bubblesort(r, n, &compare, &move);
compare = 0;
move = 0;
int *s = new int[n];
for (i = 0; i < n; i++)
{ s[i] = r[i]; }
text.Qsort(s, n, 0, n - 1, &compare, &move);
cout << "快速排序结果为:";
for (i = 0; i < n; i++) cout << s[i] << ' ';
cout << endl;
cout << "比较次数为" << compare;
cout << endl;
cout << "移动次数为" << move << '\n' << endl;
text.Selectsort(r, n, &compare, &move);
}