找第k大的数(解法3:快速排序)

View Code
 1 var i,k,n:longint;
 2     a:array[1..100000]of longint;
 3 
 4 procedure qsort(l,r:longint);
 5 var i,j,m,t:longint;
 6 begin
 7   if l=r then exit;
 8   i:=l; j:=r; m:=a[k];
 9   repeat
10     while a[i]<m do inc(i);
11     while a[j]>m do dec(j);
12     if i<j then
13     begin
14       t:=a[i]; a[i]:=a[j]; a[j]:=t;
15       inc(i); dec(j);
16     end;
17   until i>=j;
18   if i=k then exit;
19   if i>k then qsort(l,j) else qsort(j+1,r);
20 end;
21 
22 begin
23   assign(input,'maxk.in'); reset(input);
24   assign(output,'maxk.out'); rewrite(output);
25   read(n,k);
26   for i:=1 to n do read(a[i]);
27   qsort(1,n);
28   writeln(a[k]);
29   close(input); close(output);
30 end.
原文地址:https://www.cnblogs.com/haipzm/p/2460624.html