標籤:c++ 快速排序
#include <iostream>using namespace std;// quick sort //另外,如果數組是引用傳遞可以通過函數模板的實參推斷來自動獲得它長度template<typename T,int N> void printArr(T (&arr)[N]){ int i = 0; while(i<N) { cout << arr[i] <<" "; i++; } cout <<endl;}void swap(int &p,int &q){int temp = p;p= q;q=temp;}int getIndex(int arr[],int nLow,int nHigh){int nTemp = arr[nHigh];int j = nLow - 1;for(int i=nLow;i<nHigh;i++){if ( arr[i] <= nTemp) { j=j+1;swap(arr[j],arr[i]);}}swap(arr[j+1],arr[nHigh]);return j+1;}void quickSort(int arr[],int nLow,int nHigh){if (nLow < nHigh){int nIndex = getIndex(arr,nLow,nHigh);quickSort(arr,nLow,nIndex-1);quickSort(arr,nIndex + 1,nHigh);}}int main(){#ifdef __cpluspluscout <<"c++" <<endl;#elsecout <<"c " <<endl;#endifint arr[] = {52,49,80,36,14,75,58,56,57,78,42,22,22,44}; quickSort(arr,0,sizeof(arr)/sizeof(arr[0]));printArr(arr);return 0;}
http://www.cnblogs.com/pugang/archive/2012/06/27/2565093.html
具體邏輯見上面的連結
android 用paint,canvas 繪圖