c++實(shí)現(xiàn)對輸入數(shù)組進(jìn)行快速排序的示例(推薦)
更新時(shí)間:2017年06月03日 09:43:54 投稿:jingxian
下面小編就為大家?guī)硪黄猚++實(shí)現(xiàn)對輸入數(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)對輸入數(shù)組進(jìn)行快速排序的示例(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++與Java分別解決活動(dòng)選擇問題和帶權(quán)活動(dòng)選擇問題
這篇文章介紹了C++與Java分別解決活動(dòng)選擇問題和帶權(quán)活動(dòng)選擇問題,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Sersync+Rsync實(shí)現(xiàn)觸發(fā)式文件同步實(shí)戰(zhàn)過程
sersync是使用c++編寫,而且對linux系統(tǒng)文 件系統(tǒng)產(chǎn)生的臨時(shí)文件和重復(fù)的文件操作進(jìn)行過濾。下面通過本文給大家分享Sersync+Rsync實(shí)現(xiàn)觸發(fā)式文件同步實(shí)戰(zhàn)過程,需要的朋友參考下吧2017-09-09
android studio創(chuàng)建C++項(xiàng)目的實(shí)現(xiàn)示例
本文主要介紹了android studio創(chuàng)建C++項(xiàng)目的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

