Advertisement
DrewKestell

Bogo Sort

Sep 10th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class BogoSort
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         String m = "hello";
  8.         String n = "lelho";
  9.         boolean sorted = false;
  10.         int iterationCount = 0;
  11.        
  12.         while (sorted == false)
  13.             if (m.equals(n))
  14.             {
  15.                 System.out.printf("Bogo Sort required %d iterations.", iterationCount);
  16.                 sorted = true;
  17.             }
  18.             else
  19.             {
  20.                 char[] a = n.toCharArray();
  21.                
  22.                 for (int first = 0; first < a.length; first++)
  23.                 {
  24.                     Random randomNumbers = new Random();
  25.                     int second = randomNumbers.nextInt(a.length);              
  26.                     char temp = a[first];
  27.                     a[first] = a[second];
  28.                     a[second] = temp;
  29.                 }  
  30.                 n = String.valueOf(a);
  31.                 ++iterationCount;
  32.                 System.out.printf("%s\n", n);
  33.             }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement