高速排序-c++(分别用数组和容器实现)

/**********************************************************************
*版权全部 (C)2014, cheng yang。
*
*文件名:高速排序.cpp 数组实现
*内容摘要:无
*其他说明:无
*当前版本号: V1.0
*作    者:cheng yang
*完毕日期: 20140625
*
*  版本号       改动时间     改动人           改动内容
********************************************************************
*   V1.0        20140625    cy			   创建
**********************************************************************/
#include<iostream>
#include <vector>
using namespace std;
inline void swap(int &a, int &b)//是取地址,是要改动数组的!!。!!
{
	int iTemp = a;
	a = b;
	b = iTemp;
}
/**********************************************************************
*功能描写叙述:数组的划分
*输入參数:数组,数组首下标。数组尾下标
*输出參数:无
*返回值:主元下标
*其他说明:无
*改动日期        版本号号            改动人         改动内容
* --------------------------------------------------------------
*
***********************************************************************/

int Partition(int ArrayInput[],int iFirst, int iLast)
{
	int iKey = ArrayInput[iLast];
	int j = iFirst-1 ;
	for (int i = iFirst; i != iLast; i++)
	{
		if (ArrayInput[i] <= iKey)
		{
			j++;
			if (j != i)
			{
				swap(ArrayInput[j], ArrayInput[i]);

			}
		}
	}
	swap(ArrayInput[iLast], ArrayInput[j + 1]);
	return j + 1;
}
/**********************************************************************
*功能描写叙述:quick sort
*输入參数:数组。数组首下标。数组尾下标
*输出參数:无
*返回值:无
*其他说明:无
*改动日期        版本号号            改动人         改动内容
* --------------------------------------------------------------
*
***********************************************************************/
void QuickSort(int ArrayInput[],int iFirst, int iLast)
{
	
	if (iFirst < iLast)
	{
		int iIndex = Partition(ArrayInput,iFirst, iLast);
		QuickSort(ArrayInput, iFirst, iIndex - 1);
		QuickSort(ArrayInput,iIndex + 1, iLast);
	}
}

/****************************************************************
*功能描写叙述:  主函数                                             *
*输入參数:  无                                                 *
*输出參数:  无                                                 *
*返回值  :无                                                 *
*其他说明:  无                                                 *
*改动日期        版本号号       改动人        改动内容
* ------------------------------------------------------------------
*
****************************************************************/

int main()
{
	int ArrayInput[10] = { 2, 4, 1, 5, 11, 6, 9, 16, 23, 10 };
	int iFirst = 0;
	int iLast = 9;
	QuickSort(ArrayInput,iFirst, iLast);

	int i = 0;
	while (i < 10)
	{
		cout << ArrayInput[i++] << endl;
	}
	system("pause");
}


容器实现

#include <iostream>
#include <vector>
using namespace std;

inline void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}


template<class T>
int Partition(vector<T>& a, int istart, int iend)
{
	int ipivot = a[iend];
	int i = istart - 1;
	for (int j = istart; j != iend; j++)
	{
		if (a[j] <= ipivot)
		{
			i++;
			if (i != j)
			{
				swap(a[i], a[j]);
			}
		}
	}
	swap(a[iend], a[i + 1]);
	return i + 1;
}


template<class T>
void QuickSort(vector<T>& a, int istart, int iend)
{
	if (istart< iend)
	{
		int ipivot_pos = Partition(a, istart, iend);
		QuickSort(a, istart, ipivot_pos - 1);
		QuickSort(a, ipivot_pos + 1, iend);
	}

}
int main()
{
	vector<int> a;
	int input{ 0 };
	while (cin >> input)
		a.push_back(input);
	int istart = 0;
	int iend = a.size()-1;//输入6个数,大小是6,数组下标别忘了减1啊
	 
	QuickSort(a, istart, iend);
	for (auto i = a.begin(); i != a.end(); i++)
	{
		cout << *i << endl;
	}
	system("pause");
	return 0;
}




原文地址:https://www.cnblogs.com/zfyouxi/p/5252653.html