這篇文章主要介紹c++怎么實(shí)現(xiàn)對(duì)輸入數(shù)組進(jìn)行快速排序,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
河間網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,河間網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為河間數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的河間做網(wǎng)站的公司定做!
直接上代碼
#include "stdafx.h" #include#include #include using namespace std; void quickSort(vector &a, int, int); void swap(int &a, int&b); vector split(string s, string seperator); int main() { string str; cout << "please input your array: " << endl; getline(cin, str); vector strs = split(str, " "); cout << "The original array is " << endl; for (unsigned int i = 0; i < strs.size(); i++) { cout << strs[i] << " "; } cout << endl; vector 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 &a, int start, int base) { if (start >= base) { return; } int i = start, j = start; int temp = a[base]; for (;j 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 split(string s, const string pattern) { string::size_type pos; vector 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)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!