1. 先找一個(gè)基準(zhǔn)數(shù)base,通常選擇最左邊()或者最右邊()
2. 找到這個(gè)基準(zhǔn)數(shù)在數(shù)組中的位置(從小到大排序)
a. 兩個(gè)flag拟糕,一個(gè)從最左端開始(flag_low = left),一個(gè)從最右端開始(flag_high = right)七咧,一個(gè)向右和措,一個(gè)向左钉嘹,直到兩個(gè)flag相遇
b. 如果nums[low] <= base 剂娄,則代表nums[low]的位置合理--> low++蠢涝,繼續(xù)向右尋找;
c. 如果nums[high] >= base,則代表nums[high]的位置合理--> high--,繼續(xù)向左尋找;
:如果基準(zhǔn)數(shù)是nums[left], 則從high-- 開始趾牧,如果基準(zhǔn)數(shù)是nums[right],則從left++開始
d. 當(dāng)兩個(gè)flag都到達(dá)不符合的位置儿咱,則交換low high指向的值
e. 當(dāng)兩個(gè)flag相遇時(shí),就到了基準(zhǔn)數(shù)應(yīng)該在的位置场晶,nums[flag_high] = base;
3. 調(diào)用遞歸混埠,分別對(duì)基準(zhǔn)數(shù)左邊[ left, flag-1 ]和基準(zhǔn)數(shù)右邊[ flag+1, right ]的數(shù)組進(jìn)行排序
以數(shù)組最左邊為基準(zhǔn)數(shù):nums[left]
void quickSort (int left, int right, vector<int>& nums) {
if (left >= right) return;
// find base num
int base = nums[left];
// two flags. flag_low & flag_high
int flag_low = left;
int flag_high = right;
while (flag_low < flag_high) {
while (nums[flag_high] >= base && flag_low < flag_high) {
flag_high--;
}
while (nums[flag_low] <= base && flag_low < flag_high) {
flag_low++;
}
if (flag_low < flag_high) {
int tmp = nums[flag_low];
nums[flag_low] = nums[flag_high];
nums[flag_high] = tmp;
}
}
nums[left] = nums[flag_low];
int flag = flag_high;
nums[flag] = base; //flag_high = flag_low;
//遞歸
quickSort(left, flag-1, nums);
quickSort(flag+1, right, nums);
}
以數(shù)組最右邊為基準(zhǔn)數(shù):nums[right]
void quickSort (int left, int right, vector<int>& nums) {
if (left >= right) return;
// find base num
int base = nums[right];
// two flags. flag_low & flag_high
int flag_low = left;
int flag_high = right;
while (flag_low < flag_high) {
while (nums[flag_low] <= base && flag_low < flag_high) {
flag_low++;
}
while (nums[flag_high] >= base && flag_low < flag_high) {
flag_high--;
}
if (flag_low < flag_high) {
int tmp = nums[flag_low];
nums[flag_low] = nums[flag_high];
nums[flag_high] = tmp;
}
}
int flag = flag_high;
nums[right] = nums[flag];
nums[flag] = base; //flag_high = flag_low;
//遞歸
quickSort(left, flag-1, nums);
quickSort(flag+1, right, nums);
}
以左邊為基準(zhǔn),返回從大到小排序結(jié)果
void quickSort (int left, int right, vector<int>& nums) {
if (left >= right) return;
// find base num
int base = nums[left];
// two flags. flag_low & flag_high
int flag_low = left;
int flag_high = right;
while (flag_low < flag_high) {
while (nums[flag_high] <= base && flag_low < flag_high) {
flag_high--;
}
while (nums[flag_low] >= base && flag_low < flag_high) {
flag_low++;
}
if (flag_low < flag_high) {
int tmp = nums[flag_low];
nums[flag_low] = nums[flag_high];
nums[flag_high] = tmp;
}
}
int flag = flag_high;
nums[left] = nums[flag];
nums[flag] = base; //flag_high = flag_low;
//遞歸
quickSort(left, flag-1, nums);
quickSort(flag+1, right, nums);
}
- 排序算法是穩(wěn)定的嗎诗轻?
不穩(wěn)定
排序算法的穩(wěn)定性是說钳宪,如果在數(shù)組中,存在a[i] = a[j]扳炬,那在排序的過程中吏颖,如果a[i]和a[j]的前后順序不會(huì)發(fā)生改變,則代表它是穩(wěn)定的恨樟;如果順序會(huì)發(fā)生改變半醉,則不穩(wěn)定。 - 排序算法的時(shí)間復(fù)雜度
最壞:O(N^2)劝术,平均復(fù)雜度是O(N*log(N))
完成一次遍歷的復(fù)雜度是N缩多,需要遍歷的次數(shù)呆奕,最多是N,最少是log(N)