-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
63 lines (61 loc) · 1.21 KB
/
Copy pathquickSort.cpp
File metadata and controls
63 lines (61 loc) · 1.21 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<math.h>
#include<time.h>
using namespace std;
void showNum(int *arr,int num)
{
for (int i = 0; i <= num; i++)
cout << *arr++ << "\t";
cout << endl;
}
void randinput(int *arr,int num, int maxnum)
{
for (int i = 0; i <= num; i++)
{
*arr ++ = (rand() % maxnum) + 1;
}
}
void quicksort(int *a, int low, int high)
{
int i, j, pivot;
if (low < high)
{
pivot = a[low];
i = low;
j = high;
while (i < j)
{
while (i < j && a[j] >= pivot)
j--;
if (i < j)
a[i++] = a[j];
while (i < j && a[i] <= pivot)
i++;
if (i < j)
a[j--] = a[i];
}
a[i] = pivot;
quicksort(a, low, i - 1);
quicksort(a, i + 1, high);
}
}
void main()
{
srand(time(NULL)); //time是一个函数,获取时间保存结果于ts中
clock_t start, finish; //计算运行时间
double duration;
start = clock();
int num, maxnum;
cin >> num;
maxnum = num * 4;
int *arr = new int[num + 1];
randinput(arr, num, maxnum);
showNum(arr, num);
quicksort(arr, 0, num);
showNum(arr, num);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << duration << " seconds\n";
//printf("%f seconds\n", duration);
system("pause");
}