C++排序(快速排序)

xwfw 9年前發布 | 747 次閱讀 C/C++

    #include <iostream>

#define _SZ 10  
using namespace std;  


template<typename _Ty>  
class Grial  
{  
    public:  
    Grial(_Ty *_P,int _X=_SZ)  
    {  
        _SP=_X;  
        data = new _Ty[_SP];  
        for(int _I=0;_I<_SP;_I++)  
        {     
            data[_I]=_P[_I];  
        }  
        sort(data,0,_SP);  
    }  
    void view()  
    {  
        for(int _I=0;_I<_SP;_I++)  
        {  
            cout<<data[_I]<<"   ";  
        }  
        cout<<endl;  
    }  
    private:  
    void sort(_Ty *_A,int _L,int _R)  
    {  
            int _I=_L;  
            int _J=_R-1;  
            if(_L>=_R)return;  
            _Ty temp = _A[_I];  
         while(_I<_J)  
            {  
                while(_I<_J && _A[_J]>=temp)--_J;  
                _A[_I]=_A[_J];  

                while(_I<_J && _A[_I]<=temp)++_I;  
                _A[_J]=_A[_I];  
            }  
            _A[_I]=temp;  
            sort(_A,0,_I-1);  
            sort(_A,_I+1,_R);  
    }  
    private:  
    _Ty *data;  
    int _SP;  
};  

int main()  
{  
    int a[]={0,4,5,6,7,8,90,5,2,43,2,3,5};  
    Grial<int> g(a,13);  
    g.view();  
    return 0;  
}  </pre> 


 本文由用戶 xwfw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!