Tonight, i will talk about Quick Sorting.
从前有一个整型数组a[] = {37,2,6,4,89,8,10,12,68,45};
现在用快速排序按从小到大的顺序排序数组a
思路:递归,类似于二叉树的先序排序吧。
{
step 1:
以数组a的第一个元素为目标,
找到他在数组的最终位置,
(使得他的前面的所有数小于他,他的后面的所有数大于他)。
实现方法:
{
(1)从最右边的元素开始向前遍历,直至找到比他小数,
然后将这个数和目标元素(第一个元素)交换,交换后,使得所有在目标元素右边的元素都大于他。
(2)对目标元素的左边一样地操作
}
step 2:
以该最终位置为界,将数组分成左右两个子数组
step 3:
以左数组未目标,重复step 1 && step 2
step 4:
以右数组未目标,重复step 1 && step 2
}
函数的代码
递归的快速排序函数quickSort(int a[],int left,int right)
void quickSort(int a[],int left,int right)
{
int partition(int a[],int left,int right);
int currentPosition ;
if( left >= right ) //结束。例如,当子数组a[] = { a(目标元素) , b }, left = 1, right= 1 - 1 = 1
{ return; }
else
{ currentPosition = partition( a, left, right );//找出目标元素的最终位置
quickSort( a, currentPosition + 1, right );//左子数组先重复partition
quickSort( a, left, currentPosition - 1);//右子数组重复partition }
}
找出数组中目标元素的最终位置 partition( int a[],int left,int right )
int partition( int a[],int left,int right )
{
int currentPosition = left ;
void swap( int& x,int& y );
while( true )
{ //从右向左遍历
while( a[currentPosition] <= a[right] && right != currentPosition )
{ right--; }
if( currentPosition == right )//右边元素全部大于目标元素
{ return right; }
if( a[currentPosition] > a[right])
{
swap( a[currentPosition] , a[right] );//交换
currentPosition = right ;
}
//从左向右开始遍历
while( a[currentPosition] >= a[left] && left != currentPosition)
{ left++; }
if( left == currentPosition )//左边全部大于目标元素
{ return left ; }
if( a[left] > a[currentPosition] )
{
swap( a[left],a[currentPosition] );
currentPosition = left;
}
}
}
交换经常用到
void swap( int& x,int& y )
{
int temp = x;
x = y;
y = temp;
}
最后快速排序的全部代码汇总:
我认为:代码的注释是文字,那么算法思想的最好注释就是代码了!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <iostream>
using std::endl;
using std::cout;
#include <iomanip>
using std::setw;
const SIZE = 10;
void quickSort( int [], int, int );
void show( int a[] );
int main()
{
int a[SIZE] = {37,2,6,4,89,8,10,12,68,45};
show( a );
cout<<"*************Sort****************\n";////testing
quickSort( a, 0, SIZE - 1 );
cout<<"*********************************\n";////testing
show( a );
return 0;
}
void quickSort(int a[],int left,int right)
{
int partition(int a[],int left,int right);
int currentPosition ;
if( left >= right )
{ return; }
else
{
cout<<"--------currentPosition:-------\n";////testing
currentPosition = partition( a, left, right );
cout<<"---------------"<<a[currentPosition]<<"----------------\n";////testing
quickSort( a, currentPosition + 1, right );
quickSort( a, left, currentPosition - 1 );
}
}
int partition( int a[],int left,int right )
{
int currentPosition = left ;
void swap( int& x,int& y );
while( true )
{
while( a[currentPosition] <= a[right] && right != currentPosition )
{ right--; }
if( currentPosition == right )
{ return right; }
if( a[currentPosition] > a[right])
{
swap( a[currentPosition] , a[right] );
currentPosition = right ;
show( a );////testing
}
while( a[currentPosition] >= a[left] && left != currentPosition)
{ left++; }
if( left == currentPosition )
{ return left ; }
if( a[left] > a[currentPosition] )
{
swap( a[left],a[currentPosition] );
currentPosition = left;
show( a );////testing
}
}
}
void swap( int& x,int& y )
{
int temp = x;
x = y;
y = temp;
}
void show(int a[])
{
for(int i = 0;i<SIZE;i++ )
{
cout<<setw(4)<<a[i];
}
cout<<endl;
}
咳咳。。。
(reference book: Deitel C++ How to Program,thx it)