#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
using namespace std;
int creatRandomNum(int a, int b)
{
return rand() % (b - a) + a;
}
void swapTwoNum(vector<int> &ivec, int a, int b)
{
trueint temp = ivec[a];
trueivec[a] = ivec[b];
trueivec[b] = temp;
}
void mergeWithSentry(vector<int> &numBox, vector<int> &priBox, int begin, int middle, int end)
{
trueint n1 = middle - begin + 1;
trueint n2 = end - middle;
truevector<int> priL, priR, numL, numR;
truefor (int i = 0; i < n1; i++) {
truetruepriL.push_back(priBox[begin + i]);
numL.push_back(numBox[begin + i]);
true}
truefor (int j = 0; j < n2; j++) {
truetruepriR.push_back(priBox[middle + j + 1]);
truetruenumR.push_back(numBox[middle + j + 1]);
true}
truepriL.push_back(INT_MAX);
truepriR.push_back(INT_MAX);
truefor (int i = 0, j = 0, k = begin; k <= end; k++) {
truetrueif (priL[i] <= priR[j]) {
truetruetruepriBox[k] = priL[i];
truetruetruenumBox[k] = numL[i++];
truetrue} else {
truetruetruepriBox[k] = priR[j];
truetruetruenumBox[k] = numR[j++];
truetrue}
true}
}
void mergeNoSentry(vector<int> &numBox, vector<int> &priBox, int begin, int middle, int end)
{
trueint n1 = middle - begin + 1;
trueint n2 = end - middle;
truevector<int> priL, priR, numL, numR;
truefor (int i = 0; i < n1; i++) {
truetruepriL.push_back(priBox[begin + i]);
truetruenumL.push_back(numBox[begin + i]);
true}
truefor (int j = 0; j < n2; j++) {
truetruepriR.push_back(priBox[middle + j + 1]);
truetruenumR.push_back(numBox[middle + j + 1]);
true}
truefor (unsigned int i = 0, j = 0, k = begin; k <= (unsigned)end; k++) {
truetrue
truetrueif (j == priR.size() && i < priL.size()) {
truetruetruepriBox[k] = priL[i];
truetruetruenumBox[k] = numL[i++];
truetrue} else if (i == priL.size() && j < priR.size()) {
truetruetruepriBox[k] = priR[j];
truetruetruenumBox[k] = numR[j++];
truetrue} else if (priL[i] < priR[j]) {
truetruetruepriBox[k] = priL[i];
truetruetruenumBox[k] = numL[i++];
truetrue} else if (priR[j] < priL[i]) {
truetruetruepriBox[k] = priR[j];
truetruetruenumBox[k] = numR[j++];
truetrue}
true}
}
void mergeSortByPri(vector<int> &numBox, vector<int> &priBox, int begin, int end)
{
trueif (begin < end) {
truetrueint middle = (begin + end) / 2;
truetruemergeSortByPri(numBox, priBox, begin, middle);
truetruemergeSortByPri(numBox, priBox, middle + 1, end);
truetrue
truetruemergeWithSentry(numBox, priBox, begin, middle, end);
truetrue
truetrue
true}
}
void insertSort(vector<int> &numBox, vector<int> &priBox)
{
truevector<int>::size_type length = priBox.size();
trueint priKey, numKey;
truefor (unsigned int i, j = 1; j < length; j++) {
truetruepriKey = priBox[j];
truetruenumKey = numBox[j];
truetrue
truetruei = j - 1;
truetrue
truetruewhile (i >= 0 && priBox[i] > priKey) {
truetruetruepriBox[i + 1] = priBox[i];
truetruetruenumBox[i + 1] = numBox[i];
truetruetruei--;
truetrue}
truetruepriBox[i + 1] = priKey;
truetruenumBox[i + 1] = numKey;
true}
}
int main()
{
truesrand((unsigned int)time(0));
truevector<int> numBox, priBox;
trueint num;
truewhile (cin >> num)
truetruenumBox.push_back(num);
true
true 随机排列数组 算法一:优先级排序 (permute-by-sorting)
true 为每个元素 numBox[i] 赋一个随机的优先级 priBox[i],然后根据优先级对数组中的元素进行排序
true*/
truevector<int>::size_type length = numBox.size();
truefor (unsigned int i = 0; i < length; i++) {
truetruenum = creatRandomNum(1, length * length * length);
truetrueint flag = 1;
truetruewhile (flag)
truetruetruefor (vector<int>::iterator iter = numBox.begin(); iter != numBox.end();iter++)
truetruetruetrueif (*iter == num) {
truetruetruetruetruenum = creatRandomNum(1, length * length * length);
truetruetruetruetruebreak;
truetruetruetrue} else if (iter + 1 == numBox.end()) {
truetruetruetruetrueflag = 0;
truetruetruetruetruebreak;
truetruetruetrue}
truetruepriBox.push_back(num);
true}
true
true 排序算法一:分治递归法 (merge-sort)
true 时间复杂度:Θ(nlgn)
true*/
truemergeSortByPri(numBox, priBox, 0, length - 1);
true
true 排序算法二:线性查找插入排序 (insertion-sort)
true 时间复杂度:Θ(n^2)
true*/
true
true
true 随机排列数组 算法二:原地排列 (randomize-in-place)
true 在第i次迭代时,元素 numBox[i] 是从元素 numBox[i] 到 numBox[length - 1] 中随机选取的
true 时间复杂度:O(n)
true*/
true
truetrueswapTwoNum(numBox, i, creatRandomNum(i, length - 1));*/
for (vector<int>::iterator iter = numBox.begin(); iter != numBox.end(); ++iter)
truetruecout << *iter << " ";
truecout << endl;
truesystem("pause");
truereturn 0;
}