cppreference.com
template<typename ForwardIter, typename UnaryPred>ForwardIter myPartition(ForwardIter first, ForwardIter last, UnaryPred pred){ auto pos = first; for ( ; first != last; ++first) { if (pred(*first)) swap(*first, *pos++); } return pos;}template<typename ForwardIter>voID myQuickSort(ForwardIter first, ForwardIter last){ if (first == last) return; //必要 auto pivot = *next(first, distance(first, last) / 2); auto mID1 = myPartition(first, last, [pivot](const auto &em) { return em < pivot; }); auto mID2 = myPartition(mID1, last, [pivot](const auto &em) { return !(pivot < em); }); myQuickSort(first, mID1); myQuickSort(mID2, last);}voID test(){ vector<int> arr = { -1, 7, 2, 3, 4, 13, -8 }; myQuickSort(arr.begin(), arr.end()); //-8 -1 2 3 4 7 13 for (int i = 0; i < arr.size(); ++i) { cout << arr[i] << " "; } cout << endl;}
python
def quicksort(array): if len(array) < 2: return array else: pivot = array[0] less = [ i for i in array[1:] if i <= pivot ] greater = [i for i in array[1:] if i > pivot ] return quicksort(less) + [pivot] + quicksort(greater)print(quicksort([10, 5, 2, 3]))
总结 以上是内存溢出为你收集整理的快速排序 c++ stl实现和python实现全部内容,希望文章能够帮你解决快速排序 c++ stl实现和python实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)