Skip to content

Commit 85e74ec

Browse files
committed
kth smallest and MAX_MIN added
1 parent d7bdfb7 commit 85e74ec

File tree

8 files changed

+299
-12
lines changed

8 files changed

+299
-12
lines changed
25.8 KB
Loading

Graph Generated/MIN-MAX Element.png

17.3 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import java.io.BufferedWriter;
2+
import java.io.File;
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
public class KthSmallest {
10+
public static void main(String[] args) throws IOException {
11+
12+
File f=new File("D:\\Algorithm time Complexity analysis\\kthSmallest_comp_analysis.txt");
13+
BufferedWriter bw=new BufferedWriter(new FileWriter(f,false),2);
14+
15+
//50000,75000,100000,125000,150000,175000,200000
16+
List<Integer> TestCase=Arrays.asList(50000,75000,100000,125000,150000,175000,200000);
17+
int[] best;
18+
int[] worst;
19+
int[] avg;
20+
21+
22+
int t=0;
23+
bw.write("Number_of_Input\t\t\tNumber_of_comparisions\n");
24+
25+
while(t < TestCase.size()) {
26+
27+
28+
int arrSize=TestCase.get(t);
29+
best=new int[arrSize];
30+
worst=new int[arrSize];
31+
avg=new int[arrSize];
32+
33+
Random rand=new Random(); // To Generate Random Numbers...
34+
35+
for(int i=0;i<arrSize;i++) {
36+
avg[i]=rand.nextInt(arrSize*10); //Filling Numbers in the range of (0, arrSize*10-1) in array of size arrSize
37+
}
38+
39+
for(int i=0;i<arrSize;i++) {
40+
best[i]=avg[i];
41+
}
42+
Arrays.sort(best); // To make a sorted array... which we will use for best case..
43+
44+
for(int i=0;i<arrSize;i++) {
45+
worst[i]=best[arrSize-1-i]; // To make reverse order of the Best case ... to Check worst case..
46+
}
47+
48+
int k=arrSize/3;
49+
50+
CountComparisons=0;
51+
int kthSmallest=kthSmallest(best,0,best.length-1,arrSize/3);
52+
bw.write(String.format("Best_case\t\t\t%d\t\t\t%d\n",arrSize,CountComparisons));
53+
System.out.println(k+" "+kthSmallest+" "+CountComparisons);
54+
55+
CountComparisons=0;
56+
//it will be the worst case
57+
kthSmallest=kthSmallest(worst,0,worst.length-1,arrSize/3);
58+
bw.write(String.format("Worst_case\t\t\t%d\t\t\t%d\n",arrSize,CountComparisons));
59+
System.out.println(k+" "+kthSmallest+" "+CountComparisons);
60+
61+
CountComparisons=0;
62+
//Avg case.....
63+
kthSmallest=kthSmallest(avg,0,avg.length-1,arrSize/3);
64+
bw.write(String.format("Avg_case\t\t\t%d\t\t\t%d\n",arrSize,CountComparisons));
65+
System.out.println(k+" "+kthSmallest+" "+CountComparisons);
66+
67+
// For TESTING only...
68+
//
69+
// if(t==0) {
70+
// for(int i=0;i<arrSize;i++) {
71+
// System.out.print(best[i] +" ");
72+
// }
73+
// System.out.println();
74+
//
75+
// for(int i=0;i<arrSize;i++) {
76+
// System.out.print(worst[i] +" ");
77+
// }
78+
// System.out.println();
79+
//
80+
// for(int i=0;i<arrSize;i++) {
81+
// System.out.print(avg[i] +" ");
82+
// }
83+
// System.out.println();
84+
// }
85+
//
86+
t++;
87+
System.out.println("Success");
88+
}
89+
bw.close();
90+
91+
}
92+
93+
static int CountComparisons;
94+
95+
96+
static int kthSmallest(int arr[], int l, int r, int k)
97+
{
98+
if (k > 0 && k <= r - l + 1)
99+
{
100+
int n = r - l + 1 ;
101+
102+
int i;
103+
104+
int []median = new int[(n + 4) / 5];
105+
for (i = 0; i < n/5; i++)
106+
median[i] = getMedian(arr,l + i * 5, 5);
107+
108+
if (i*5 < n)
109+
{
110+
median[i] = getMedian(arr,l + i * 5, n % 5);
111+
i++;
112+
}
113+
114+
int medOfMed = (i == 1)? median[i - 1]:
115+
kthSmallest(median, 0, i - 1, i / 2);
116+
117+
int pos = partition(arr, l, r, medOfMed);
118+
119+
if (pos-l == k - 1)
120+
return arr[pos];
121+
if (pos-l > k - 1)
122+
return kthSmallest(arr, l, pos - 1, k);
123+
124+
return kthSmallest(arr, pos + 1, r, k - pos + l - 1);
125+
}
126+
return Integer.MAX_VALUE;
127+
}
128+
129+
private static int getMedian(int[] arr, int l, int r) {
130+
Arrays.sort(arr,l,l+r);
131+
CountComparisons+=r;
132+
return arr[l+(r)/2];
133+
}
134+
135+
136+
static int partition(int arr[], int low, int high,int pivot)
137+
{
138+
int i;
139+
for(i = low; i < high; i++)
140+
if (arr[i] == pivot)
141+
break;
142+
swap(arr, i, high);
143+
144+
i = low;
145+
for(int j = low; j <= high - 1; j++)
146+
{
147+
if (arr[j] <= pivot)
148+
{
149+
CountComparisons++;
150+
swap(arr, i, j);
151+
i++;
152+
}
153+
}
154+
swap(arr, i, high);
155+
return i;
156+
}
157+
158+
159+
private static void swap(int[] arr, int i, int j) {
160+
int temp=arr[i];
161+
arr[i]=arr[j];
162+
arr[j]=temp;
163+
}
164+
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import java.io.BufferedWriter;
2+
import java.io.File;
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
public class MinMax {
10+
11+
public static void main(String[] args) throws IOException {
12+
13+
File f=new File("D:\\Algorithm time Complexity analysis\\min_max_comp_analysis.txt");
14+
BufferedWriter bw=new BufferedWriter(new FileWriter(f,false),2);
15+
16+
//50000,75000,100000,125000,150000,175000,200000
17+
List<Integer> TestCase=Arrays.asList(50000,75000,100000,125000,150000,175000,200000);
18+
int[] arr;
19+
20+
21+
int k=0;
22+
bw.write("Number_of_Input\t\t\tNumber_of_comparisions\n");
23+
24+
while(k < TestCase.size()) {
25+
26+
int arrSize=TestCase.get(k);
27+
arr=new int[arrSize];
28+
29+
Random rand=new Random(); // To Generate Random Numbers...
30+
31+
for(int i=0;i<arrSize;i++) {
32+
arr[i]=rand.nextInt(arrSize*10); //Filling Numbers in the range of (0, arrSize*10-1) in array of size arrSize
33+
}
34+
35+
36+
CountComparisons=0;
37+
//Avg case.....
38+
Pair minmax=minMax(arr,0,arr.length-1);
39+
bw.write(String.format("%d\t\t\t%d\n",arrSize,CountComparisons));
40+
41+
// System.out.println(minmax.min+" "+minmax.max+" "+CountComparisons);
42+
43+
k++;
44+
System.out.println("Success");
45+
}
46+
bw.close();
47+
48+
}
49+
50+
static int CountComparisons;
51+
52+
static class Pair{
53+
int min;
54+
int max;
55+
56+
Pair(int min,int max){
57+
this.min=min;
58+
this.max=max;
59+
}
60+
}
61+
62+
// By Tournament Method
63+
private static Pair minMax(int[] arr, int l,int h) {
64+
if(l==h) {
65+
return new Pair(arr[l],arr[h]);
66+
}
67+
else if(l==h-1) {
68+
CountComparisons++;
69+
if(arr[l]>arr[h]) {
70+
return new Pair(arr[h],arr[l]);
71+
}else {
72+
return new Pair(arr[l],arr[h]);
73+
}
74+
}
75+
int mid=l+(h-l)/2;
76+
Pair minmaxl=minMax(arr,l,mid);
77+
Pair minmaxr=minMax(arr,mid+1,h);
78+
int localmin,localmax;
79+
80+
CountComparisons+=2;
81+
82+
if(minmaxl.min < minmaxr.min) {
83+
localmin=minmaxl.min;
84+
}else {
85+
localmin=minmaxr.min;
86+
}
87+
88+
if(minmaxl.max > minmaxr.max) {
89+
localmax=minmaxl.max;
90+
}else {
91+
localmax=minmaxr.max;
92+
}
93+
94+
return new Pair(localmin,localmax);
95+
}
96+
97+
98+
}
Binary file not shown.

kthSmallest_comp_analysis.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Number_of_Input Number_of_comparisions
2+
Best_case 50000 242502
3+
Worst_case 50000 244439
4+
Avg_case 50000 243101
5+
Best_case 75000 363602
6+
Worst_case 75000 364801
7+
Avg_case 75000 365326
8+
Best_case 100000 488609
9+
Worst_case 100000 490781
10+
Avg_case 100000 489150
11+
Best_case 125000 608491
12+
Worst_case 125000 614225
13+
Avg_case 125000 613707
14+
Best_case 150000 731024
15+
Worst_case 150000 736832
16+
Avg_case 150000 735973
17+
Best_case 175000 852674
18+
Worst_case 175000 856175
19+
Avg_case 175000 861939
20+
Best_case 200000 982516
21+
Worst_case 200000 985837
22+
Avg_case 200000 986009

min_max_comp_analysis.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Number_of_Input Number_of_comparisions
2+
50000 82766
3+
75000 117230
4+
100000 165534
5+
125000 190534
6+
150000 234462
7+
175000 284462
8+
200000 331070

python program for plotting graphs/CountingAndRadix.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,20 @@
99
import pandas as pd
1010
import matplotlib.pyplot as mp
1111

12-
df=pd.read_csv("D:\Algorithm time Complexity analysis\\Radix_sort_analysis.txt",sep="\t")
12+
df=pd.read_csv("D:\Algorithm time Complexity analysis\\min_max_comp_analysis.txt",sep="\t\t\t")
1313

1414
best=[]
1515
worst=[]
1616
avg=[]
17+
print(df)
1718

1819
for i in range(df.shape[0]):
19-
if i%3==0:
20-
best.append([df.iloc[i,1],df.iloc[i,2]])
21-
if i%3==1:
22-
worst.append([df.iloc[i,1],df.iloc[i,2]])
23-
if i%3==2:
24-
avg.append([df.iloc[i,1],df.iloc[i,2]])
20+
avg.append([df.iloc[i,0],df.iloc[i,1]])
2521

2622

27-
mp.plot([i[0] for i in best],[i[1] for i in best],'bs--',label="bestcase(sorted)")
28-
mp.plot([i[0] for i in worst],[i[1] for i in worst],'m^--',label="worst case(reverse)")
29-
mp.plot([i[0] for i in avg],[i[1] for i in avg],'go--',label="averagecase(random)")
23+
mp.plot([i[0] for i in avg],[i[1] for i in avg],'go--')
3024
mp.xlabel("Number of Input")
31-
mp.ylabel("Time Taken")
32-
mp.title("Radix Sort")
25+
mp.ylabel("Number of Comparsions")
26+
mp.title("MIN-MAX Element")
3327
mp.legend()
3428
mp.grid(True,color='k')

0 commit comments

Comments
 (0)