⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d_11_2.cpp

📁 C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体
💻 CPP
字号:
#include "stdafx.h"
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;

template <class Type>
class A {
private:
	Type array[10];
public:
	A(Type a[10])
	{ for (int i=0;i<10;++i)
	  array[i] = a[i];
	}
	void quickSort(int head,int tail)
	{  	int i=head,j=tail;
		Type temp=array[head];   //取第一个对象为进行调整的标准对象
		while(i<j)
		{	while(i<j && temp<=array[j]) j--;//在数组的右端扫描
			if(i<j)
			{	array[i]=array[j];
				i++;
			}
			while(i<j&& array[i]<temp) i++;//在数组的左端扫描
				if(i<j)
				{	array[j]=array[i];
					j--;
				}
		}
		array[i]=temp;
		if(head<i) quickSort(head,i-1);   //递归实现
		if(i<tail) quickSort(j+1,tail);
	}

	void disp()
	{
		for(int i=0;i<10;i++)
		cout<<setw(4)<<array[i];
	    cout<<endl;
	}
};

void main()
{	int temp1[10]={10,95,38,74,16,5,44,13,20,1};
    A <int>a(temp1);
	cout<<"a的原始顺序为:"<<endl;
	a.disp();
	cout<<endl<<"排序后a的顺序为:"<<endl;
	a.quickSort(0,9);
	a.disp();

	char temp2[12]={'M', 'o' , 'Y', 's', 'a', 'A', 'O', 'E', 'p', 'R' };
	A <char> b(temp2);
	cout<<"b的原始顺序为:"<<endl;
	b.disp();
	cout<<endl<<"排序后b的顺序为:"<<endl;
	b.quickSort(0,9);
	b.disp();
	cin.get(); //等待结束,以便调测程序,可以删除

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -