• 快速排序,归并排序


    参考 剑指offer的方法,比较简单。

    #include<iostream>
    #include <vector>
    #include <set>
    #include <functional>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string>
    #include <sstream>
    #include <list>
    #include <map>
    #include <stack>
    #include <algorithm>
    #include<iomanip>
    using namespace std;

    int RandomInRange(int start, int end)
    {
    return start + rand() % (end - start+1);
    }
    int partion(vector<int> &vi, int start, int end)
    {
    if (start < 0 || end >= vi.size() || start > end)
    throw new exception("Invalid Patameter ");

    int index = RandomInRange(start, end);
    std::swap(vi[index], vi[end]);
    int small = start - 1;

    for (int i = start; i < end; i++)
    {
    if (vi[i] < vi[end])
    {
    //++small;
    //if (i != small)
    // std::swap(vi[i], vi[small]);
    std::swap(vi[i], vi[++small]);
    }

    }

    std::swap(vi[end], vi[++small]);
    return small;
    }
    void QuickSort(vector<int>& vi, int start, int end)
    {
    if (start == end)
    return;

    int index = partion(vi, start, end);

    if (index > start)
    QuickSort(vi, start, index - 1);
    if (index < end)
    QuickSort(vi, index + 1, end);

    }
    void QuickSort(vector<int>& vi)
    {
    QuickSort(vi, 0, vi.size() - 1);
    }
    //***********************************************
    void Merge(vector<int>& vi, vector<int>& tmp, int leftPos, int rightPos, int rightEnd)
    {
    if (leftPos >= rightPos || rightPos>rightEnd)
    return;
    int leftEnd = rightPos - 1;
    int count = rightEnd - leftPos + 1;
    int tmpPos = leftPos;
    while (leftPos <= leftEnd && rightPos <= rightEnd)
    {
    if (vi[leftPos] <= vi[rightPos])
    tmp[tmpPos++] = vi[leftPos++];
    else
    tmp[tmpPos++] = vi[rightPos++];
    }
    while (leftPos <= leftEnd)
    tmp[tmpPos++] = vi[leftPos++];
    while (rightPos <= rightEnd)
    tmp[tmpPos++] = vi[rightPos++];

    for (int i = 0; i < count; i++,rightEnd--)
    vi[rightEnd] = tmp[rightEnd];
    }
    void MergeSort(vector<int>& vi, vector<int>& tmp, int start, int end)
    {
    if (start >= end)
    return;

    int mid = (start + end) / 2;
    MergeSort(vi, tmp, start, mid);
    MergeSort(vi, tmp, mid + 1, end);

    Merge(vi, tmp, start, mid + 1, end);
    }
    void MergeSort(vector<int> vi)
    {
    vector<int> tmp(vi.size());
    MergeSort(vi, tmp, 0, vi.size() - 1);
    for (auto i : vi)
    cout << i << " ";
    cout << endl;
    }
    int main(int argc, char** args)
    {

    vector<int> vi = { 1, 5, 3, 4, 6, 9, 2, 7, 8 };
    for (auto i : vi)
    cout << i << " ";
    cout << endl;
    MergeSort(vi);
    QuickSort(vi);
    for (auto i : vi)
    cout << i << " ";
    cout << endl;
    system("pause");
    return 0;
    }

  • 相关阅读:
    使用WCF建立起Silverlight客户端与服务端的桥梁
    Silverlight WCF RIA服务(三十三)身份验证、角色、个性化 4
    陶哲轩实分析习题 12.1.3
    Asymptote 学习记录(6) 练习用模块roundedpath画出一个图
    使用Asymptote的循环功能画出绿叶阵
    使用Asymptote的循环功能画出绿叶阵
    度量空间的一个例子:离散度量空间
    练习: 使用Asymptote 画出字母R的轮廓曲线
    练习: 使用Asymptote 画出字母R的轮廓曲线
    陶哲轩实分析命题 11.10.7
  • 原文地址:https://www.cnblogs.com/Oscar67/p/9588008.html
Copyright © 2020-2023  润新知