-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.cpp
75 lines (73 loc) · 1.46 KB
/
count.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
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<time.h>
using namespace std;
int partition(int *a,int i,int j)
{
if(j-i>=3)
{
int change;
int mid=(i+j)/2;
if(a[i]>=a[mid]&&a[i]<=a[j])
{
}else if(a[mid]>=a[i]&&a[mid]<=a[j])
{
change=a[mid];
a[mid]=a[i];
a[i]=change;
} else
{
change=a[j];
a[j]=a[i];
a[i]=change;
}
}
int key=a[i];
while(i<j)
{
while(i<j&&key<=a[j])
{
j--;
}
a[i]=a[j];
while(i<j&&key>=a[i])
{
i++;
}
a[j]=a[i];
}
a[i]=key;
return i;
}
int quikSortfind(int *a,int low,int high,int order)
{
if(order>high)
{
return -1;
}
int pos=partition(a,low,high);
if(pos==order)
return a[pos];
else if(pos>order)
return quikSortfind(a,low,pos-1,order);
else
return quikSortfind(a,pos+1,high,order);
}
int main()
{
ifstream fin("E:dataset\\Sort_data\\random.txt");
int *number=new int[1000000];
int j=0;
while(fin>>number[j])
{
j++;
}
int order;
cout<<"请输入要查找的第i大的数:";
cin>>order;
clock_t start=clock();
cout<<quikSortfind(number,0,j-1,order)<<" ";
clock_t end=clock();
cout<<"时间为: "<<(double)(end - start)<<"ms";
}