forked from dharmanshu1921/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBogo_Sort.java
More file actions
Latest commit
47 lines (39 loc) · 945 Bytes
/
Copy pathBogo_Sort.java
File metadata and controls
47 lines (39 loc) · 945 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
publicclassBogoSort {
voidbogoSort(int[] a)
{
while (isSorted(a) == false)
shuffle(a);
}
voidshuffle(int[] a)
{
for (inti = 0; i < a.length; i++)
swap(a, i, (int)(Math.random() * i));
}
voidswap(int[] a, inti, intj)
{
inttemp = a[i];
a[i] = a[j];
a[j] = temp;
}
booleanisSorted(int[] a)
{
for (inti = 1; i < a.length; i++)
if (a[i] < a[i - 1])
returnfalse;
returntrue;
}
voidprintArray(int[] arr)
{
for (inti = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
publicstaticvoidmain(String[] args)
{
int[] a = { 3, 2, 5, 1, 0, 4 };
BogoSortob = newBogoSort();
ob.bogoSort(a);
System.out.print("Sorted array: ");
ob.printArray(a);
}
}