中文域名注冊網(wǎng)站惡意點擊競價時用的什么軟件
基本思想
任取一個元素為中心,所有比它小的元素一律前放,比他大的元素一律后放,形成左右兩個子表;對各子表重新選擇中心元素并依此規(guī)則調(diào)整,直到每個子表的元素只剩一個。
通過一趟排序,將待排序記錄分割成獨立的兩部分,其中一部分記錄的關鍵字都比兩一部分記錄的關鍵字小,則可對這兩部分記錄進行排序,以達到整個序列有序。
調(diào)試代碼
#include <iostream>
#include <vector>
using namespace std;
int counter = 0;//調(diào)試用int Partition(vector<int>&vec, int low, int high)
{//首先以第一個元素的值作為樞軸的值,此時它所在的位置空出來,然后從后往前遍歷,將第一個小于樞軸的值的元素放到空位int pivotVal = vec[low]; // 選取第一個元素的值作為樞軸的值while (low < high){while (low < high && vec[high] >= pivotVal) high--; // 從后往前遍歷,直到遇到比樞軸小的元素時停下swap(vec[low], vec[high]);while (low < high && vec[low] <= pivotVal) low++; // 從前往后遍歷,直到遇到比樞軸大的元素時停下swap(vec[low], vec[high]);}vec[low] = pivotVal;cout << "第" << ++counter << "趟: ";for (int i = 0; i < 11; i++)cout << vec[i] << " ";cout << endl << endl;return low;
}
void quick_sort(vector<int>& vec, int low, int high)
{if (low < high){int pivot = Partition(vec, low, high); // 確定樞軸的位置quick_sort(vec, low, pivot - 1); // 對 左邊子序列 遞歸排序quick_sort(vec, pivot + 1, high); // 對 右邊子序列 遞歸排序}
}
int main(int argc, const char* argv[])
{vector<int> vec = { 100, 1, 53, 5, 36, 7, 8, 109, 10, 11, 15 };cout << "待排序數(shù)組: ";for (int i = 0; i < 11; i++)cout << vec[i] << " ";cout << endl << endl;quick_sort(vec, 0, vec.size() - 1);cout << "結果: ";for (int i = 0; i < 11; i++)cout << vec[i] << " ";cout << endl << endl;return 0;
}