Skip to content

Commit 8e21b8b

Browse files
committed
initial commit
0 parents  commit 8e21b8b

34 files changed

+1875
-0
lines changed

Graph Generated/Heap_sort_graph.png

26.6 KB
Loading
26.8 KB
Loading
23.1 KB
Loading
20.5 KB
Loading

Graph Generated/bubblesort_graph.png

22.9 KB
Loading
23.6 KB
Loading

Graph Generated/merge_sort_graph.png

26.5 KB
Loading

Graph Generated/quick_sort_graph.png

23.5 KB
Loading
13.2 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 BinarySearch {
10+
11+
public static void main(String[] args) throws IOException {
12+
13+
File f=new File("D:\\java file handling\\binary_search_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("Case\t\t\tNumber_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+
Arrays.sort(arr);
35+
// For Best Case
36+
37+
int comparisions=binary_search(arr,arr[(arrSize-1)/2]);
38+
bw.write(String.format("Best_case\t\t\t%d\t\t\t%d\n",arrSize,comparisions));
39+
40+
//if element is not present in the array.. it will be the worst case
41+
comparisions=binary_search(arr,-1);
42+
bw.write(String.format("Worst_case\t\t\t%d\t\t\t%d\n",arrSize,comparisions));
43+
44+
//Avg case..taking a number at some index in middle...
45+
comparisions=binary_search(arr,arr[(arrSize-1)/8-1 ]);
46+
bw.write(String.format("Avg_case\t\t\t%d\t\t\t%d\n",arrSize,comparisions));
47+
48+
// //For TESTING only...
49+
//
50+
// if(k==0) {
51+
// for(int i=0;i<arrSize;i++) {
52+
// System.out.print(arr[i] +" ");
53+
// }
54+
// System.out.println();
55+
// }
56+
//
57+
k++;
58+
System.out.println("Success");
59+
}
60+
bw.close();
61+
62+
}
63+
64+
private static int binary_search(int[] arr, int var) {
65+
int comp=0;
66+
int l=0,h=arr.length-1;
67+
int mid = l + (h - l) / 2;
68+
69+
while(l<=h) {
70+
mid = l + (h - l) / 2;
71+
if(arr[mid]==var) {
72+
return comp+1;
73+
}else if(var > arr[mid]) {
74+
comp++;
75+
l=mid+1;
76+
}else {
77+
comp++;
78+
h=mid-1;
79+
}
80+
}
81+
return (int) (Math.log10(arr.length-1)/Math.log10(2));
82+
}
83+
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class BubbleSort {
5+
6+
public static void main(String[] args) throws IOException {
7+
8+
File f=new File("D:\\java file handling\\bubblesort_analysis.txt");
9+
BufferedWriter bw=new BufferedWriter(new FileWriter(f,false),2);
10+
//10000,30000,75000,100000,137000,175000
11+
List<Integer> TestCase=Arrays.asList(10000,30000,75000,100000,137000,175000);
12+
int[] best;
13+
int[] worst;
14+
int[] avg;
15+
16+
17+
int k=0;
18+
bw.write(" \tNumber_of_Input\tTime_Taken\n");
19+
20+
while(k < TestCase.size()) {
21+
22+
int arrSize=TestCase.get(k);
23+
best=new int[arrSize];
24+
worst=new int[arrSize];
25+
avg=new int[arrSize];
26+
27+
Random rand=new Random(); // To Generate Random Numbers...
28+
29+
for(int i=0;i<arrSize;i++) {
30+
avg[i]=rand.nextInt(arrSize*10); //Filling Numbers in the range of (0, arrSize*10-1) in array of size arrSize
31+
}
32+
33+
for(int i=0;i<arrSize;i++) {
34+
best[i]=avg[i];
35+
}
36+
Arrays.sort(best); // To make a sorted array... which we will use for best case..
37+
38+
for(int i=0;i<arrSize;i++) {
39+
worst[i]=best[arrSize-1-i]; // To make reverse order of the Best case ... to Check worst case..
40+
}
41+
42+
// For Best Case
43+
44+
long initialTime=System.nanoTime();
45+
46+
bubble_sort(best);
47+
48+
long TimeTaken=System.nanoTime()-initialTime;
49+
50+
bw.write(String.format("Best_Case\t%d\t%d\n",arrSize,TimeTaken));
51+
52+
// For Worst Case
53+
54+
initialTime=System.nanoTime();
55+
56+
bubble_sort(worst);
57+
58+
TimeTaken=System.nanoTime()-initialTime;
59+
60+
bw.write(String.format("Worst_Case\t%d\t%d\n",arrSize,TimeTaken));
61+
62+
// For Average Case
63+
64+
initialTime=System.nanoTime();
65+
66+
bubble_sort(avg);
67+
68+
TimeTaken=System.nanoTime()-initialTime;
69+
70+
bw.write(String.format("Avg_Case\t%d\t%d\n",arrSize,TimeTaken));
71+
72+
// For Testing Only..
73+
// if(k==0) {
74+
// for(int i=0;i<arrSize;i++) {
75+
// System.out.print(best[i] +" ");
76+
// }
77+
// System.out.println();
78+
// for(int i=0;i<arrSize;i++) {
79+
// System.out.print(worst[i] +" ");
80+
// }
81+
// System.out.println();
82+
// for(int i=0;i<arrSize;i++) {
83+
// System.out.print(avg[i] +" ");
84+
// }
85+
// System.out.println();
86+
// }
87+
//
88+
k++;
89+
System.out.println("Success");
90+
}
91+
bw.close();
92+
93+
}
94+
95+
static void bubble_sort(int[] arr) {
96+
97+
int temp;
98+
for (int i = 0; i < arr.length-1; i++) {
99+
for (int j = 0; j < arr.length-i-1; j++)
100+
if (arr[j] > arr[j+1])
101+
{
102+
temp = arr[j];
103+
arr[j] = arr[j+1];
104+
arr[j+1] = temp;
105+
}
106+
}
107+
108+
}
109+
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 HeapSort {
10+
11+
public static void main(String[] args) throws IOException {
12+
13+
File f=new File("D:\\java file handling\\heap_sort_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[] best;
19+
int[] worst;
20+
int[] avg;
21+
22+
23+
int k=0;
24+
bw.write(" \tNumber_of_Input\tTime_Taken\n");
25+
26+
while(k < TestCase.size()) {
27+
28+
int arrSize=TestCase.get(k);
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+
// For Best Case
49+
long initialTime=System.nanoTime();
50+
51+
heap_sort(best);
52+
53+
long TimeTaken=System.nanoTime()-initialTime;
54+
bw.write(String.format("Best_Case\t%d\t%d\n",arrSize,TimeTaken));
55+
56+
// For Worst case
57+
initialTime=System.nanoTime();
58+
59+
heap_sort(worst);
60+
61+
TimeTaken=System.nanoTime()-initialTime;
62+
bw.write(String.format("Worst_Case\t%d\t%d\n",arrSize,TimeTaken));
63+
64+
// For Average case
65+
initialTime=System.nanoTime();
66+
67+
heap_sort(avg);
68+
69+
TimeTaken=System.nanoTime()-initialTime;
70+
bw.write(String.format("Avg_Case\t%d\t%d\n",arrSize,TimeTaken));
71+
72+
73+
// //For TESTING only...
74+
//
75+
// if(k==0) {
76+
// for(int i=0;i<arrSize;i++) {
77+
// System.out.print(best[i] +" ");
78+
// }
79+
// System.out.println();
80+
//
81+
// for(int i=0;i<arrSize;i++) {
82+
// System.out.print(worst[i] +" ");
83+
// }
84+
// System.out.println();
85+
//
86+
// for(int i=0;i<arrSize;i++) {
87+
// System.out.print(avg[i] +" ");
88+
// }
89+
// System.out.println();
90+
// }
91+
//
92+
93+
k++;
94+
System.out.println("Success");
95+
}
96+
bw.close();
97+
98+
}
99+
100+
101+
public static void heap_sort(int arr[])
102+
{
103+
int n = arr.length;
104+
for (int i = n / 2 - 1; i >= 0; i--)
105+
heapify(arr, n, i);
106+
107+
for (int i=n-1; i>0; i--)
108+
{
109+
int temp = arr[0];
110+
arr[0] = arr[i];
111+
arr[i] = temp;
112+
113+
heapify(arr, i, 0);
114+
}
115+
}
116+
117+
118+
static void heapify(int arr[], int n, int i)
119+
{
120+
int max= i;
121+
int l = 2*i + 1;
122+
int r = 2*i + 2;
123+
124+
if (l < n && arr[l] > arr[max])
125+
max = l;
126+
127+
if (r < n && arr[r] > arr[max])
128+
max = r;
129+
130+
131+
if (max != i)
132+
{
133+
int swap = arr[i];
134+
arr[i] = arr[max];
135+
arr[max] = swap;
136+
137+
heapify(arr, n, max);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)