标签:style blog http color os io strong 2014
From:
http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F
Code:
#include "stdafx.h" #include <functional> #include <algorithm> #include <iterator> #include <iostream> using namespace std; template< typename BidirectionalIterator, typename Compare > void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) { if( first != last ) { BidirectionalIterator left = first; BidirectionalIterator right = last; BidirectionalIterator pivot = left++; while( left != right ) { if( cmp( *left, *pivot ) ) { ++left; } else { while( (left != right) && cmp( *pivot, *right ) ) --right; std::iter_swap( left, right ); } } --left; std::iter_swap( first, left ); quick_sort( first, left, cmp ); quick_sort( right, last, cmp ); } } template< typename BidirectionalIterator > inline void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) { quick_sort( first, last, std::less_equal< typename std::iterator_traits< BidirectionalIterator >::value_type >() ); } int _tmain(int argc, _TCHAR* argv[]) { int a[] = {4,9,8,3,6,5,1,7}; int *s = a; int *e =a + sizeof(a)/sizeof(int) - 1; quick_sort(s,e); while(s <= e) cout<< *s++ <<" "; return 0; }
Result:
标签:style blog http color os io strong 2014
原文地址:http://www.cnblogs.com/anit/p/3893064.html