废话不多说,直接上代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#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++实现对输入数组进行快速排序的示例(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。