-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.h
More file actions
59 lines (46 loc) · 935 Bytes
/
quick_sort.h
File metadata and controls
59 lines (46 loc) · 935 Bytes
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
#ifndef _CLASS_QUICK_SORT_
#define _CLASS_QUICK_SORT_
#include "sort.h"
class quick_sort: public sort{
public:
explicit quick_sort(int num):sort(num){
cout<<"create quick_sort successfully"<<endl;
}
explicit quick_sort(const int *array, int num):sort(array, num){
cout<<"create quick_sort successfully"<<endl;
}
~quick_sort(){
cout<<"free quick_sort successfully, ";
}
void sort_l2r(int *a, int l, int r){
if (l >= r)
return;
int tmp;
int pos = l;
for (int i = l; i < r; i++){
if (a[i] < a[r]){
//swap a[pos] and p[i]
tmp = a[pos];
a[pos] = a[i];
a[i] = tmp;
pos++;
}
}
//swap a[pos] and a[r]
tmp = a[pos];
a[pos] = a[r];
a[r] = tmp;
sort_l2r(a, l, pos-1);
sort_l2r(a, pos+1, r);
}
void quicksort(){
sort_l2r(a_, 0, n_-1);
}
void print_a_(){
cout<<"quick sorted array: ";
for (int i = 0; i < n_; i++)
cout<<a_[i]<<" ";
cout<<endl;
}
};
#endif