码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-06-26 10:47:49      阅读:273      评论:0      收藏:0      [点我收藏+]

标签: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;
}




快速排序-c++(分别用数组和容器实现),布布扣,bubuko.com

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

标签:c++

原文地址:http://blog.csdn.net/u011409995/article/details/34491907

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!