//Sorts an integer array in ascending order. //Notice that the array passed must not be a null reference. //Parameters: // data[] - the integer array to sort //Postcondition: // The array is sorted in ascending order. //Warning: // Due to the finite number of random sequences used by // java.util.Random it is possible that the execution // of this code will result in an infinite loop. import java.util.Random; public class BogoSortAlgorithm extends SortAlgorithm { public void sort(int data[]) throws Exception { if (data.length > 0) runBogoSort(data); } private void runBogoSort(int data[]) throws Exception { Random generator; int tempVariable; int randomPosition; do { generator = new Random(); for (int i = 0; i < data.length; i++) { randomPosition = generator.nextInt(data.length); tempVariable = data[i]; data[i] = data[randomPosition]; data[randomPosition] = tempVariable; pause(i,randomPosition); } } while(!isSorted(data)); } private boolean isSorted(int data[]) { for (int i = 1; i < data.length; i++) if (data[i] < data[i - 1]) return false; return true; } }