fork download
  1. // Java Program to implement BogoSort
  2. public class Main
  3. {
  4. // Sorts array a[0..n-1] using Bogo sort
  5. void bogoSort(int[] a)
  6. {
  7. // if array is not sorted then shuffle the
  8. // array again
  9. while (isSorted(a) == false)
  10. shuffle(a);
  11. }
  12.  
  13. // To generate permuatation of the array
  14. void shuffle(int[] a)
  15. {
  16. // Math.random() returns a double positive
  17. // value, greater than or equal to 0.0 and
  18. // less than 1.0.
  19. for (int i=1; i <= n; i++)
  20. swap(a, i, (int)(Math.random()*i));
  21. }
  22.  
  23. // Swapping 2 elements
  24. void swap(int[] a, int i, int j)
  25. {
  26. int temp = a[i];
  27. a[i] = a[j];
  28. a[j] = temp;
  29. }
  30.  
  31. // To check if array is sorted or not
  32. boolean isSorted(int[] a)
  33. {
  34. for (int i=1; i<a.length; i++)
  35. if (a[i] < a[i-1])
  36. return false;
  37. return true;
  38. }
  39.  
  40. // Prints the array
  41. void printArray(int[] arr)
  42. {
  43. for (int i=0; i<arr.length; i++)
  44. System.out.print(arr[i] + " ");
  45. System.out.println();
  46. }
  47.  
  48. public static void main(String[] args)
  49. {
  50. //Enter array to be sorted here
  51. int[] a = {3, 2, 5, 1, 0, 4};
  52. BogoSort ob = new BogoSort();
  53.  
  54. ob.bogoSort(a);
  55.  
  56. System.out.print("Sorted array: ");
  57. ob.printArray(a);
  58. }
  59. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
2
10
42
11
compilation info
Main.java:19: error: cannot find symbol
         for (int i=1; i <= n; i++) 
                            ^
  symbol:   variable n
  location: class Main
Main.java:52: error: cannot find symbol
        BogoSort ob = new BogoSort(); 
        ^
  symbol:   class BogoSort
  location: class Main
Main.java:52: error: cannot find symbol
        BogoSort ob = new BogoSort(); 
                          ^
  symbol:   class BogoSort
  location: class Main
3 errors
stdout
Standard output is empty