-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomise.java
More file actions
82 lines (81 loc) · 1.47 KB
/
Copy pathRandomise.java
File metadata and controls
82 lines (81 loc) · 1.47 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Scanner;
import java.util.Random;
class Quickran
{
int i,j,t,pivot,c=0,H=0;
int partion(int a[],int st,int end,int n)
{
Random r=new Random();
int p=r.nextInt(n-1);
int pi=st+(p%(end-st+1));
t=a[st];
a[st]=a[pi];
a[pi]=t;
pivot=a[st];
i=st;
j=end;
while(i<j)
{
while(++c>0 && pivot>=a[i]&&i<=end)
i++;
while(++c>0 && pivot<=a[j]&&j>st)
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
t=a[st];
a[st]=a[j];
a[j]=t;
return j;
}
void divide(int a[],int st,int end,int n,int k)
{
if(st<end)
{
j=partion(a,st,end,n);
if(++H>0&&j==k)
System.out.println("k th smallest number is="+a[k]);
else if(++H>0&&k<j-1)
divide(a,st,j-1,n,k);
else
{
++H;
divide(a,j+1,end,n,k);
}
}
}
}
class Hari
{
public static void main(String []srg)
{
Quickran p=new Quickran();
int n,c=0,i;
int a[]=new int[250];
Scanner d=new Scanner(System.in);
Random r=new Random();
n=d.nextInt();
for(i=0;i<n;i++)
a[i]=r.nextInt(100);
for(i=0;i<n;i++)
System.out.println((i+1)+".-->"+a[i]);
int k=d.nextInt();
if(k<=n&&k>=0)
{
long s=System.nanoTime();
p.divide(a,0,n-1,n,k);
long h=System.nanoTime();
System.out.println("sorted array");
for(i=0;i<n;i++)
System.out.println(a[i]);
System.out.println("no of times execution:"+p.H);
System.out.println("Time Complexity:"+(h-s));
}
else
System.out.println("k is out range");
}
}