-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathBogoSort.java
More file actions
50 lines (46 loc) · 882 Bytes
/
BogoSort.java
File metadata and controls
50 lines (46 loc) · 882 Bytes
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
public class BogoSort
{
public static void main(String[] args)
{
int[] arr={4,5,6,0,7,8,9,1,2,3};
BogoSort now=new BogoSort();
System.out.print("Unsorted is ");
now.display1D(arr);
now.bogo(arr);
System.out.print("Sorted is ");
now.display1D(arr);
}
void bogo(int[] arr)
{
int shuffle=1;
for(;!isSorted(arr);shuffle++)
shuffle(arr);
//Boast
System.out.println("This took "+shuffle+" shuffles");
}
void shuffle(int[] arr)
{
int i=arr.length-1;
while(i>0)
swap(arr,i--,(int)(Math.random()*i));
}
void swap(int[] arr,int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
boolean isSorted(int[] arr)
{
for(int i=1;i<arr.length;i++)
if(arr[i]<arr[i-1])
return false;
return true;
}
void display1D(int[] arr)
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}