-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.cpp
98 lines (92 loc) · 1.69 KB
/
4.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
template <typename idv>
class Sorter
{
public:
Sorter(idv *A, idv n){
Array = A;
length = n;
}
void BubbleSort(int method)
{
int i,j;
idv t;
for(i=length; i>1; i--)
for(j=0; j<i-1; j++){
if (compare(Array[j],Array[j+1],method))
t = Array[j], Array[j] = Array[j+1], Array[j+1] = t;
}
}
private:
idv *Array;
int length;
int compare(idv s1,idv s2,int method)
{
if (method ==0) return s1<s2;
else return s1>s2;
}
};
template<typename idv> void Generate(idv *A,int n)
{
int i;
srand((unsigned int)time(0));
for(i=0; i<n; i++){ //Generate the array
A[i] = idv(rand()%1000/10.0);
cout<<A[i]<<' ';
}
cout<<endl<<endl;
}
template<typename idv> void Aprint(idv *A, int n)
{
int i;
for(i=0; i<n; i++){
cout<<A[i]<<' ';
}
cout<<endl;
}
template<typename idv> void Verify(idv *A, int n,int method)
{
vector<idv> v;
int i;
for(i=0; i<n; i++){
v.push_back(A[i]);
}
if (method) sort(v.begin(), v.end(),less<idv>());
else sort(v.begin(), v.end(),greater<idv>());
for(i=0; i<n; i++){
if (A[i] != v[i]){
cout<<"wrong"<<endl;
break;
}
if (i == n-1) cout<<"right"<<endl;
}
}
int main() {
int i,N=100;
int *A1;
double *A2;
A1 = new int[N];
A2 = new double[N];
cout<<"origin"<<endl;
Generate(A1,N);
Generate(A2,N);
//sort
Sorter<int> BS1(A1,N); //1 means ascend, 0 means descend
Sorter<double> BS2(A2,N);
BS1.BubbleSort(0);
BS2.BubbleSort(1);
cout<<"int type with descend"<<endl;
Aprint(A1,N);
Verify(A1,N,0);
cout<<"double type with ascend"<<endl;
Aprint(A2,N);
Verify(A2,N,1);
delete []A1;
delete []A2;
return 0;
}