/*
快速排序:
选取第一个记录为关键字K1,将小于关键字的记录移到关键字前
将大于关键字的记录移到关键字后
*/
# include <iostream>
using namespace std;
/*一趟快速排序*/
int QKPass(int r[],int left,int right)
{
int x = r[left];
int low = left;
int high = right;
while(low<high)
{
while(low<high&&r[high]>x)
high--;
if(low<high)
{
r[low]=r[high];
low++;
}
while(low<high&&r[low]<x)
low++;
if(low<high)
{
r[high]=r[low];
high--;
}
}
r[low]=x;
return low;
}
//快速排序
void QKSort(int r[],int low,int high)
{
if(low<high)
{
int pos = QKPass(r,low,high);
QKSort(r,low,pos-1);
QKSort(r,pos+1,high);
}
}
//打印数组元素
void printArray(int r[])
{
for(int i=0;i<10;i++)
cout<<r[i]<<" ";
cout<<endl;
}
void main()
{
int r[] = {48,62,35,77,55,14,35,98,22,40};
printArray(r);
QKSort(r,0,9);
printArray(r);
}
快速排序:
选取第一个记录为关键字K1,将小于关键字的记录移到关键字前
将大于关键字的记录移到关键字后
*/
# include <iostream>
using namespace std;
/*一趟快速排序*/
int QKPass(int r[],int left,int right)
{
int x = r[left];
int low = left;
int high = right;
while(low<high)
{
while(low<high&&r[high]>x)
high--;
if(low<high)
{
r[low]=r[high];
low++;
}
while(low<high&&r[low]<x)
low++;
if(low<high)
{
r[high]=r[low];
high--;
}
}
r[low]=x;
return low;
}
//快速排序
void QKSort(int r[],int low,int high)
{
if(low<high)
{
int pos = QKPass(r,low,high);
QKSort(r,low,pos-1);
QKSort(r,pos+1,high);
}
}
//打印数组元素
void printArray(int r[])
{
for(int i=0;i<10;i++)
cout<<r[i]<<" ";
cout<<endl;
}
void main()
{
int r[] = {48,62,35,77,55,14,35,98,22,40};
printArray(r);
QKSort(r,0,9);
printArray(r);
}