zoukankan      html  css  js  c++  java
  • 高速排序-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;
    }
    
    



  • 相关阅读:
    QGraphicsItem鼠标旋转控制研究
    QT场景视图父子关系图元打印研究
    QT绘制B样条曲线
    [转]localhost、127.0.0.1和0.0.0.0和本机IP的区别
    [转]C++ 堆栈溢出的原因以及可行的解决方法
    C++运算符重载学习总结
    关于C++中使用++it还是it++的问题
    [转]QT中的D指针与Q指针
    Python图像处理库:Pillow 初级教程
    STEP-MXO2开发板 [STEP FPGA开源社区]
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5252653.html
Copyright © 2011-2022 走看看