Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class BogoSort
- {
- public static void main(String[] args)
- {
- String m = "hello";
- String n = "lelho";
- boolean sorted = false;
- int iterationCount = 0;
- while (sorted == false)
- if (m.equals(n))
- {
- System.out.printf("Bogo Sort required %d iterations.", iterationCount);
- sorted = true;
- }
- else
- {
- char[] a = n.toCharArray();
- for (int first = 0; first < a.length; first++)
- {
- Random randomNumbers = new Random();
- int second = randomNumbers.nextInt(a.length);
- char temp = a[first];
- a[first] = a[second];
- a[second] = temp;
- }
- n = String.valueOf(a);
- ++iterationCount;
- System.out.printf("%s\n", n);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement