北京邮电大学数据结构实验三题目1

匿名 (未验证) 提交于 2019-12-03 00:22:01

题目1

使用简单数组实现下面各种排序算法,并进行比较。

排序算法:

3、冒泡排序

4、快速排序

5、简单选择排序

6、堆排序(选作)

7、归并排序(选作)

8、基数排序(选作)

9、其他

要求:

编写测试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<n; i++)  		cout << r[i] << ' '; 	cout << endl; 	cout << "比较次数为" << compare; 	cout << endl; 	cout << "移动次数为" << move << '\n' << endl; } void paixu::Insertsort(int r[], int n, int* compare, int* move)//插入排序 { 	int *a = new int[n]; 	for (int w = 0; w<n; w++) 	{ 		a[w] = r[w]; 	} 	*compare = 0; 	*move = 0; 	int i; 	int j; 	for (i = 1; i<n; i++) 	{ 		int x = a[i]; 		for (j = i - 1; x<a[j] && j >= 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<n; i++) 	{ 		b[i] = r[i]; 	} 	*compare = 0; 	*move = 0; 	int j; 	for (int d = n / 2; d >= 1; d = d / 2) 	{ 		for (int i = d; i <= n - 1; i++)//从a[d]往后逐个元素确定是否需要前移 		{ 			if (b[i]<b[i - d]) 			{ 				int x = b[i]; 				for (j = i - d; (j >= 0) && (x<b[j]); j = j - d) 				{ 					(*compare)++; 					(*move)++; 					b[j + d] = b[j]; 				} 				if (j >= 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; j<n - 1; j++)//本冒泡排序从后往前遍历,先把最小的元素冒到最上面 	{ 		for (int i = n - 1; i>j; i--) 		{ 			if (c[i] <c[i - 1]) 			{ 				(*compare)++; 				(*move) += 3; 				x = c[i]; 				c[i] = c[i - 1]; 				c[i - 1] = x; 			} 			else (*compare)++; 		} 	} 	Print(c, n, 3, *compare, *move); }  int paixu::Partion(int r[], int first, int end, int* compare, int* move)//快速排序中的轴定位 { 	int i = first; 	int j = end; 	int zhou = r[i];//默认第一个元素为轴 	while (i<j) 	{ 		while ((i<j) && (r[j] >= zhou)) //查看右侧元素与轴的大小关系 		{ 			(*compare)++; 			j--; 		} 		if (i<j) 		{ 			(*compare)++; 			(*move)++; 			r[i] = r[j];//发现轴右侧的某数比轴值小,将其前置 		} 		while ((i<j) && (r[i] <= zhou))//查看左侧元素与轴的大小关系 		{ 			(*compare)++; 			i++; 		} 		if (i<j) 		{ 			(*compare)++; 			(*move)++; 			r[j] = r[i];//发现轴左侧的某数比轴值小,将其后置 		} 	} 	r[i] = zhou;//最后确定轴的位置 	return i; }  void paixu::Qsort(int r[], int n,int i, int j, int* compare, int* move)//快速排序 { 	if (i<j) 	{ 		int centre = Partion(r, i, j, compare, move); 		Qsort(r, n,i, centre - 1, compare, move); 		Qsort(r, n,centre + 1, j, compare, move); 	} 	 	 }  void paixu::Selectsort(int r[], int n, int* compare, int* move)//选择排序 { 	int *e = new int[n]; 	for (int i = 0; i < n; i++) 	{ 		e[i] = r[i]; 	} 	*compare = 0; 	*move = 0; 	for (int i = 0; i<n - 1; i++) 	{ 		int min = i; 		for (int j = i + 1; j<n; j++)//找到真正的第i个最小值 		{ 			(*compare)++; 			if (e[j]<e[min]) 				min = j; 		} 		if (min != i)       //实现最小值和当前元素的交换 		{ 			(*move) += 3; 			int Min; 			Min = e[min]; 			e[min] = e[i]; 			e[i] = Min; 		} 	} 	Print(e, n, 4, *compare, *move); } 

Դ.cpp

#include<iostream> #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);  }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!