快速排序C++ 递归实现
快速排序
快排思想:
与归并排序类似,也使用分治思想,算法开始选择一个元素值(第一个或最后一个),作为枢轴,要做的是左边的元素都小于该基准数(枢轴),右边的元素都大于该枢轴。
左右子序列的处理方法类似,这样子问题和原问题处一致,这样符合分治思路,如用递归,我们即可找到当子序列中只有一个元素就是算法的出口。
算法流程:
1、i = left, j = right, 将基准数挖出形成第一个坑。
2、从后往前找出比基准数小的数,遇到比它大的数j--,找到后挖出次数arr[j],填到arr[i]中;
3、从前往后找出比基准数大的数,遇到比它小的数i++, 找到后挖出此数arr[i],填到arr[j]中;
4、重复2)3),直到i = j,将基准数填到a[i]。
时间复杂度:
O(nlogn)但若初始序列基本有序时,快速排序反而退化为冒泡排序。
C++示例代码
#ifndef QuickSort_hpp
#define QuickSort_hpp
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class QuickSort {
public:
void sort(vector<int>& arr, int len) {
subSort(arr, 0, len-1);
}
void subSort(vector<int>& arr, int left, int right) {
if (left >= right) {
return;
}
int q = partion(arr, left, right);
subSort(arr, 0 , q-1);
subSort(arr, q+1, right);
}
int partion(vector<int>& arr, int left, int right) {
int pivot = arr[left];//选定最左边的元素为分割元素
int i = left;//初始化右移指针【最左元素】
int j = right;//初始化左移移指针【最右元素】
while(i < j){
//从右往左找第一个小于pivot的数
while (i < j && arr[j] >= pivot) {
j--;
}
//把这个小于pivot的数放到左半部分的一个位置
if(i < j){
arr[i] = arr[j];
}
//从左边往右边着第一个大于pivot的数
while (i < j && arr[i] < pivot) {
i++;
}
//把这个大于pivot的数放到右半部分的一个位置
if (i < j) {
arr[j] = arr[i];
}
}
if(i == j){
arr[i] = pivot;
cout << "pivot index : " << j << endl;
}
return j;
}
};
#endif /* QuickSort_hpp */
/*QuickSort.cpp*/
#include "QuickSort.hpp"
/*main*/
#include <iostream>
#include <vector>
#include "QuickSort.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
vector<int> arr6 = {7, 5, 3, 2, 4, 9, 1, 8, 0, 6};
cout << "arr6: " << endl;
for (int item : arr6) {
cout << item <<" ";
}
cout << endl;
QuickSort quickSort = QuickSort();
quickSort.sort(arr6, int(arr6.size()));
cout << "quick sort arr6: " << endl;
for (int item : arr6) {
cout << item <<" ";
}
cout << endl;
return 0;
}
控制台输出,可以看到每次递归选择的pivoit在数组中被调整的位置
arr6:
7 5 3 2 4 9 1 1 8 0 6
pivot index : 8
pivot index : 7
pivot index : 1
pivot index : 4
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 6
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 3
pivot index : 0
pivot index : 1
pivot index : 4
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 9
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 3
pivot index : 0
pivot index : 1
pivot index : 4
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 5
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 3
pivot index : 0
pivot index : 1
pivot index : 6
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 3
pivot index : 0
pivot index : 1
pivot index : 4
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 7
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 3
pivot index : 0
pivot index : 1
pivot index : 4
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 5
pivot index : 0
pivot index : 1
pivot index : 2
pivot index : 0
pivot index : 3
pivot index : 0
pivot index : 1
quick sort arr6:
0 1 1 2 3 4 5 6 7 8 9
Program ended with exit code: 0