📄 8_74.cpp
字号:
#include<iostream.h>
#include<iomanip.h>
template<class T>
void QuickSort(T*const array,int low,int high)
{ //递归实现
int i=low,j=high;
T temp=array[low]; //取第一个对象为进行调整的标准对象
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(low<i) QuickSort(array,low,i-1);
if(i<high) QuickSort(array,j+1,high);
}
void main()
{
const int size=10;
int a[10]={10,9,8,7,6,5,4,3,2,1},i;
cout<<"The original order of a:"<<endl;
for(i=0;i<size;i++)
cout<<setw(4)<<a[i];
QuickSort(a,0,size-1);
cout<<endl<<"The sorted order of a:"<<endl;
for(i=0;i<size;i++)
cout<<setw(4)<<a[i];
cout<<endl;
char b[10]={'s','T','e','Y','a','A','o','E','c','R'};
cout<<"The original order of b:"<<endl;
for(i=0;i<size;i++)
cout<<setw(4)<<b[i];
QuickSort(b,0,size-1);
cout<<endl<<"The sorted order of b:"<<endl;
for(i=0;i<size;i++)
cout<<setw(4)<<b[i];
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -