自己动手写算法.Sort.QuickSort
Quick Sort Algorithm
Quick sort uses divide-conquer-combine stratage to sort array in place.
- namespace
- {
- int gSize = 0;
- int partation(int arr[], int nStart, int nEnd)
- {
- int pivot = arr[nEnd];
- int pos = nStart - 1;
- for(int i=nStart; i<nEnd; ++i)
- {
- if( arr[i] < pivot )
- {
- ++pos;
- std::swap(arr[i], arr[pos]);
- }
- }
- std::swap(arr[pos+1], arr[nEnd]);
- return pos+1;
- }
- void quickSort(int arr[], int nStart, int nEnd)
- {
- ALog::print(arr, gSize);
- if( nStart < nEnd )
- {
- int nPos = partation(arr, nStart, nEnd);
- quickSort(arr, nStart, nPos-1);
- quickSort(arr, nPos+1, nEnd);
- }
- }
- }
- /*
- Divide-Conquer-Combine
- */
- void SortQuick::sort(int arr[], const int nSize)
- {
- ALog::print(_T("# quick Sort: "));
- ALog::print(arr, nSize);
- gSize = nSize;
- quickSort(arr, 0, nSize-1);
- }