-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_sort.cpp~
More file actions
executable file
·45 lines (44 loc) · 1.09 KB
/
quick_sort.cpp~
File metadata and controls
executable file
·45 lines (44 loc) · 1.09 KB
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
/*************************************************************************
> File Name: quick_sort.cpp
> Author: Royecode
> Mail: Royecode@163.com
> Created Time: 2015年07月20日 星期一 10时08分44秒
************************************************************************/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void quick_sort(int l, int r, int *arr)
{
if(l < r)
{
cout << "!" << endl;
int begin = l, end = r, val = arr[l];
while(begin < end)
{
cout << "!!" << endl;
while(begin < end && arr[end] > val) end--;
while(begin < end && arr[begin] < val) begin++;
swap(arr[begin], arr[end]);
}
arr[begin] = val;
quick_sort(l, begin - 1, arr);
quick_sort(begin + 1, r, arr);
}
}
int main()
{
srand(time(NULL));
const int size = 10;
int arr[size];// = {4,5,0,1,-2,3,6,8,7,-9};
for(int i = 0; i < size; ++i)
arr[i] = rand() % size;
for(int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout << endl;
quick_sort(0, size - 1, arr);
for(int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout << endl;
return 0;
}