-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain Code.txt
200 lines (175 loc) · 5.4 KB
/
Main Code.txt
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package betterbogotest;
import static java.lang.System.arraycopy;
import static java.lang.System.currentTimeMillis;
import java.util.Random;
/**
*
* @author Admin
*/
public class BetterBogoTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
int[] test1 = new int[100];
int[] test2 = new int[100];
int[] test3 = new int[100];
int[] test4 = new int[100];
addValues(test1,0,100);
arraycopy(test1,0,test2,0,test1.length);
arraycopy(test1,0,test3,0,test1.length);
arraycopy(test1,0,test4,0,test1.length);
long start = currentTimeMillis();
betterBogo(test1);
long time1 = currentTimeMillis() - start;
start = currentTimeMillis();
bubbleSort(test2);
long time2 = currentTimeMillis() - start;
start = currentTimeMillis();
selectionSort(test3);
long time3 = currentTimeMillis() - start;
start = currentTimeMillis();
insertionSort(test4);
long time4 = currentTimeMillis() - start;
System.out.print("BetterBogo: ");
display(test1);
System.out.println("Time (ms): " + time1);
System.out.print("BubbleSort: ");
display(test2);
System.out.println("Time (ms): " + time2);
System.out.print("SelectionSort ");
display(test3);
System.out.println("Time (ms): " + time3);
System.out.print("InsertionSort: ");
display(test4);
System.out.println("Time (ms): " + time4);
}
public static void copy(int[] arr1, int[] arr2){
for(int i = 0; i < arr1.length; i++){
arr2[i] = arr1[i];
}
}
public static void betterBogo(int[] arr){
int randCounter = 0;
int start = 0;
while(start < arr.length){
shuffle(arr,start);
randCounter++;
timer(1);
while(arr[start] == findLowest(arr, start)){
start++;
if(start == arr.length){
return;
}
}
}
}
public static void shuffle(int[] arr, int start){
int[] tempList = new int[arr.length - start];
int index = 0;
for(int i = start; i < arr.length; i++){
tempList[index] = arr[i];
index++;
}
for(int i = 0; i < tempList.length; i++){
int j = (int)(Math.random() * (i + 1));
int temp = tempList[i];
tempList[i] = tempList[j];
tempList[j] = temp;
}
index = 0;
for(int i = start; i < arr.length; i++){
arr[i] = tempList[index];
index++;
}
}
public static int findLowest(int[] arr, int start){
int lowest = arr[start];
for(int i = start; i < arr.length; i++){
if(arr[i] < lowest){
lowest = arr[i];
}
}
return lowest;
}
public static void bubbleSort(int a[])
{
int temp;
int swap = 1; // count
while (swap > 0)
{
swap = 0;
for (int i = 0; i <= a.length - 2; i++)
{
timer(1);
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swap++;
}
}
}
}
public static void selectionSort(int a[])
{
int min; // value
int minIndex; // index
for (int j = 0; j <= a.length - 2; j++)
{
min = a[j];
minIndex = j;
for (int i = j + 1; i < a.length; i++)
{
if (a[i] < min)
{
min = a[i];
minIndex = i;
}
}
a[minIndex] = a[j];
a[j] = min;
}
}
public static void insertionSort(int a[])
{
int key;
int j;
for (int i = 1; i < a.length; i++)
{
key = a[i] ; // new card
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j]; // shift
j--;
}
a[j + 1] = key; // insert
}
}
public static void display(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
public static void addValues(int[] arr, int min, int max){
Random r = new Random();
for(int i = 0; i < arr.length; i++){
arr[i] = min + r.nextInt(max);
}
}
public static void timer(int time) {
long start;
long end = currentTimeMillis() + time;
do
{
start = currentTimeMillis();
} while(start < end);
}
}