• c++实现对输入数组进行快速排序


    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    void quickSort(vector<int> &a, int, int);
    void swap(int &a, int&b);
    vector<string> split(string s, string seperator);
    
    int main() {
        string str;
        cout << "please input your array: " << endl;
        getline(cin, str);
        vector<string> strs = split(str, " ");
        cout << "The original array is " << endl;
        for (unsigned int i = 0; i < strs.size(); i++) {
            cout << strs[i] << " ";
        }
        cout << endl;
        vector<int> array(strs.size());
        for (unsigned int i = 0; i < strs.size(); i++) {
            array[i] = atoi(strs[i].c_str());
        }
        int len = array.size();
        cout << "The ordered array is " << endl;
        quickSort(array, 0, len-1);
        for (int i = 0; i < len; i++) {
            cout << array[i] << " ";
        }
        cout << endl;
        system("pause");
        return 0;
    }
    void quickSort(vector<int> &a, int start, int base) {
        if (start >= base) {
            return;
        }
        int i = start, j = start;
        int temp = a[base];
        for (;j<base;j++) {
            if (a[j]<=temp) {
                swap(a[i], a[j]);
                i++;
            }
        }
        if (a[i] > a[base]) {
            swap(a[i], a[base]);
        }
        quickSort(a, start, i - 1);
        quickSort(a, i + 1, base);
    }
    void swap(int &a, int&b) {
        if (a == b) {
        }
        else {
            a = a + b;
            b = a - b;
            a = a - b;
        }
        
    }
    vector<string> split(string s, const string pattern) {
        string::size_type pos;
        vector<string> result;
        s += pattern;
        unsigned int size = s.size();
        for (unsigned int i = 0; i < size; i++) {
            pos = s.find(pattern, i);
            if (pos < size) {
                string str = s.substr(i, pos - i);
                if (!str.empty()){
                    result.push_back(str);
                }
                i = pos + pattern.size() - 1;
    
            }
        }
        return result;
    }
  • 相关阅读:
    12小时制时间
    sqlserver 安装和配置
    建议71:区分异步和多线程应用场景
    AVD管理器提示:PANIC:Could not open:AVD名称 解决办法
    一道看似复杂但是简单的c#面试题
    XML Schema 配置文件自动生成c#类设计案例子
    VS2010中的调试技巧 断点
    文章已被删除
    使用MONO使.net程序脱离.net框架运行
    5个很好用的.net 分析工具
  • 原文地址:https://www.cnblogs.com/kdxb/p/6934277.html
Copyright © 2020-2023  润新知