C++学习之类的使用(排序算法)

要求:

1) 完成至少3 种排序算法。

2) 排序数据采用*号量化表示。
(即用不同数量的*号表示排序数据,可以参考http://www.sorting-algorithms.com/,需要浏览器允许运行Java Applet)
3) 使用sleep(),system("cls");显示排序过程。

4) 可以通过键盘选择排序算法。

编程要求
1) 类的成员函数、成员函数的定义必须包括private public 访问属性。
2) main 函数中类对象的建立包括两类,类对象,对象指针。
3) 使用new 和delete 建立类对象和释放类对象。
4) 界面设计友好,给出菜单选项,及输入提示。
 扩展要求
尝试对比所有算法对不同排序数据的排序效率(可以以交换数据次数进行对比,也可以通过获取系统处理时间
进行对比),如对已经升序数据、对降序数据、对多组随机数据在不同排序算法的效率进行对比。

初学C++,最开始理解类有点困难,

作为一个大二才学C++的弱菜的我一个排序算法一个类,每个类里面的函数大同小异,可以用继承(省时省力。。)


sort.h

<span style="font-family:Microsoft YaHei;font-size:18px;">#define max 1000
#include<iostream>
#include<windows.h>
#include<stdio.h>
using namespace std;
//int array[max];
int len;
int sign = 0;

class Cocktail
{
private:
	int n;
	int elem[max];
public:
	void print();
	void cocktail_sort();//cocktail_sort(int* a, int len)
	void copy(int array[max]);
};
class Insert
{
private:
	int n;
	int elem[max];

public:
	void print();
	void insert_sort();
	void copy(int array[max]);
};
class Qsort
{
private:
	int n;
	int elem[max];
public:
	void print();
	void Quick_sort(int first, int high);
	void copy(int array[max]);
};
class Bubble
{
private:
	int n;
	int elem[max];
public:
	void print();
	void bubble_sort();//	bubble_sort(number, SIZE);
	void bubble_opo();
	void copy(int array[max]);
	void sort();
	void showans();
};
//冒泡排序
void Bubble::print()
{

	for (int i = 0; i < n; i++)
	{
		for (int j = 1; j <= elem[i]; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}
void Bubble::bubble_sort()//n为数组a的元素个数
{
	int t = 0;
	if (sign == 1)
	{
		system("cls");
		print();
	}
	int i, j, temp;
	for (j = 0; j < n - 1; j++)
	{
		for (i = 0; i<n - 1 - j; i++)
		{
			if (elem[i]>elem[i + 1])//数组元素大小按升序排列
			{
				temp = elem[i];
				elem[i] = elem[i + 1];
				elem[i + 1] = temp;
				t++;
				if (sign == 1)
				{
					Sleep(230);
					system("cls");
					print();
				}
			}
		}
	}
	cout << "bubble_sort change times:" << t << endl;
}
void Bubble::bubble_opo()
{
	int t = 0;
	int i, j, temp;
	for (j = 0; j < n - 1; j++)
	{
		for (i = 0; i < n - 1 - j; i++)
		{
			if (elem[i] < elem[i + 1])//数组元素大小按升序排列
			{
				temp = elem[i];
				elem[i] = elem[i + 1];
				elem[i + 1] = temp;
			}
		}
	}
}
void Bubble::copy(int array[max])
{
	int i;
	n = len;
	for (i = 0; i < n; i++)
	{
		elem[i] = array[i];
	}
}
void Bubble::sort()
{
	int i, j, temp;
	int t = 0;
	for (j = 0; j < n - 1; j++)
	{
		for (i = 0; i<n - 1 - j; i++)
		{
			if (elem[i]>elem[i + 1])//数组元素大小按升序排列
			{
				temp = elem[i];
				elem[i] = elem[i + 1];
				elem[i + 1] = temp;
			}
		}
	}
}
void Bubble::showans()
{
	int i;
	cout << endl;
	cout << endl;
	cout << "排序结果为:" << endl;
	for (i = 0; i < n; i++)
	{
		cout << elem[i] << " ";
	}
	cout << endl;
	cout << endl;
}

//鸡尾酒排序
void Cocktail::print()
{

	for (int i = 0; i < n; i++)
	{
		for (int j = 1; j <= elem[i]; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}
void Cocktail::cocktail_sort()
{
	int bottom = 0;
	int top = n - 1;
	int t = 0;
	bool swapped = true;
	if (sign == 1)
	{
		system("cls");
		print();
	}

	while (swapped)
	{
		swapped = false;
		for (int i = bottom; i<top; i++)
		{
			if (elem[i]>elem[i + 1])
			{
				swap(elem[i], elem[i + 1]);
				t++;
				swapped = true;
				if (sign == 1)
				{
					Sleep(230);
					system("cls");
					print();
				}
			}
		}
		top = top - 1;
		for (int i = top; i > bottom; i--)
		{
			if (elem[i] < elem[i - 1])
			{
				swap(elem[i], elem[i - 1]);
				t++;
				swapped = true;
				if (sign == 1)
				{
					Sleep(230);
					system("cls");
					print();
				}

			}

		}
	}
	bottom = bottom + 1;

	cout << "cocktail_sort change times:" << t << endl;
}
void Cocktail::copy(int array[max])
{
	int i;
	n = len;
	for (i = 0; i < n; i++)
	{
		elem[i] = array[i];
	}
}

//插入排序
void Insert::print()
{

	for (int i = 0; i < n; i++)
	{
		for (int j = 1; j <= elem[i]; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}
void Insert::insert_sort()//int*array, unsigned int n
{
	int i, j;
	int temp;
	int k = 0;
	if (sign == 1)
	{
		system("cls");
		print();
	}


	for (i = 1; i<n; i++)
	{
		temp = elem[i];
		for (j = i; j>0 && elem[j - 1] > temp; j--)
		{
			elem[j] = elem[j - 1];
			k++;
			if (sign == 1)
			{
			Sleep(230);
			system("cls");
			print();
			}
		}
		elem[j] = temp;
		if (sign == 1)
		{
			Sleep(230);
			system("cls");
			print();
		}
	}
	cout << "insert_sort change times:" << k << endl;
}
void Insert::copy(int array[max])
{
	int i;
	n = len;
	for (i = 0; i < n; i++)
	{
		elem[i] = array[i];
	}
}

//快速排序

void Qsort::print()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 1; j <= elem[i]; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}
void Qsort::Quick_sort(int low, int high)
{
	if (low >= high)
	{
		return;
	}
	int first = low;
	int last = high;
	system("cls");
	print();
	int key = elem[first];/*用字表的第一个记录作为枢轴*/
	while (first < last)
	{
		while (first < last&&elem[last] >= key)
		{
			--last;
		}
		elem[first] = elem[last];/*将比第一个小的移到低端*/
		while (first < last&&elem[first] <= key)
		{
			++first;
		}
		elem[last] = elem[first];/*将比第一个大的移到高端*/
		if (sign == 1)
		{
			Sleep(230);
			system("cls");
			print();
		}
	}
	elem[first] = key;/*枢轴记录到位*/
	if (sign == 1)
	{
		Sleep(230);
		system("cls");
		print();
	}
	Quick_sort(low, first - 1);
	Quick_sort(first + 1, high);
}
void Qsort::copy(int array[max])
{
	int i;
	n = len;
	for (i = 0; i < n; i++)
	{
		elem[i] = array[i];
	}
}

</span>


main.c

<span style="font-family:Microsoft YaHei;font-size:18px;">//测试数据:80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5      
#include<iostream>
#include<windows.h>
#include"Sort.h"
using namespace std;
int main()
{
	int array[max];
	char temp;
	int i;
	cout << "//******************请输入数据总数*****************\\" << endl;
	cin >> len;
	cout << "//**************请输入数据,空格表示间隔!***********\\" << endl;
	for (i = 0; i < len; i++)
	{
		cin >> array[i];
	}
	cout << "顺序排序->s" << endl;
	cout << "逆序排序->r" << endl;
	char order;
	cin >> order;
	Bubble *t=new Bubble();
	t->copy(array);
	if (order == 'r')
	{
		t->bubble_opo();
	}
	else
	{
		t->sort();
	}
	t->showans();
	delete t;
	Bubble *s1 = new Bubble();
	Cocktail *s2 = new Cocktail();
	Insert *s3 = new Insert();
	s1->copy(array);
	s1->bubble_sort();
	s2->copy(array);
	s2->cocktail_sort();
	s3->copy(array);
	s3->insert_sort();
	delete s1;
	delete s2;
	delete s3;
	sign = 1;
	while (1)
	{
		cout << "查看排序过程:" << endl;
		cout << "------------a->冒泡排序(Bubble_sort)------------" << endl;
		cout << "----------b->鸡尾酒排序(cocktail_sort)----------" << endl;
		cout << "------------c->插入排序(insert_sort)------------" << endl;
		cout << "-------------d->快速排序(Quick_sort)------------" << endl;
		cout << "---------------------e->退出---------------------" << endl;
		cin >> temp;
		if (temp == 'a')
		{
			Bubble *s1=new Bubble();
			s1->copy(array);
			s1->bubble_sort();
			delete s1;
		}
		else if (temp == 'b')
		{
			Cocktail *s2=new Cocktail();
			s2->copy(array);
			s2->cocktail_sort();
			delete s2;
		}

		else if (temp == 'c')
		{
			Insert *s3=new Insert();
			s3->copy(array);
			s3->insert_sort();
			delete s3;
		}
		else if (temp == 'd')
		{
			Qsort *s4=new Qsort();
			s4->copy(array);
			s4->Quick_sort(0,len-1);
			delete s4;
		}
		else if (temp == 'e')
		{
			system("cls");
			cout << "Bye  Bye!" << endl;
			Sleep(100);
			break;
		}
	}
	return 0;
}</span>











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值