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

📄 b_11_1.cpp

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

template<class Type>
void quickSort(Type*const array,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(array,head,i-1);   //递归实现
	if(i<tail) quickSort(array,j+1,tail);
}

void main()
{	int a[10]={10,95,38,74,16,5,44,13,20,1},i;
	cout<<"a的原始顺序为:"<<endl;
	for(i=0;i<10;i++)
		cout<<setw(4)<<a[i];
	quickSort(a,0,9);
	cout<<endl<<"排序后a的顺序为:"<<endl;
	for(i=0;i<10;i++)
		cout<<setw(4)<<a[i];
	cout<<endl;
	char b[12]={'M','o','Y','s','a','A','O','E','p','R','d','u'};
	cout<<"b的原始顺序为:"<<endl;
	for(i=0;i<12;i++)
		cout<<setw(4)<<b[i];
	quickSort(b,0,11);
	cout<<endl<<"排序后b的顺序为:"<<endl;
	for(i=0;i<12;i++)
		cout<<setw(4)<<b[i];
	cout<<endl;
	cin.get(); //等待结束,以便调测程序,可以删除
}

⌨️ 快捷键说明

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