最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++實(shí)現(xiàn)對(duì)輸入數(shù)組進(jìn)行快速排序的示例(推薦)

 更新時(shí)間:2017年06月03日 09:43:54   投稿:jingxian  
下面小編就為大家?guī)硪黄猚++實(shí)現(xiàn)對(duì)輸入數(shù)組進(jìn)行快速排序的示例(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

廢話不多說,直接上代碼

#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;
}

以上這篇c++實(shí)現(xiàn)對(duì)輸入數(shù)組進(jìn)行快速排序的示例(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

祁门县| 三穗县| 闽清县| 辽宁省| 宜良县| 祁东县| 闻喜县| 桐庐县| 萨嘎县| 固安县| 威宁| 南平市| 乌鲁木齐县| 疏勒县| 含山县| 当阳市| 定南县| 莲花县| 周口市| 岳阳县| 阳原县| 英吉沙县| 抚顺市| 乌鲁木齐县| 冀州市| 林甸县| 临武县| 灌阳县| 丘北县| 徐闻县| 北海市| 东莞市| 张家港市| 吴旗县| 大丰市| 庄河市| 乐至县| 冕宁县| 太保市| 肃南| 门源|