You are on page 1of 1

import java.sql.Timestamp; import java.util.

Random; public class bogoSort { /** * @param args */ public static void main(String[] args) { int[] A = {1,4,7,8,5,2,3,6,9,10,11,15,12,13,14,16,17,18,19,20,21 ,22,23,24,25,26,27,28,29,30}; System.out.println(stringarize(A) + " " + new Timestamp(System.c urrentTimeMillis()).toString()); bogoSort bg = new bogoSort(); System.out.println(bg.bogoSorting(A)); } public String bogoSorting(int[] A) { while(notSorted(A)) { A = shuffleArray(A); } return "" + stringarize(A) + " " + new Timestamp(System.currentT imeMillis()).toString(); } private boolean notSorted(int[] a) { for(int i=1; i<a.length;i++) { if(a[i-1]>a[i]) { return true; } } return false; } public int[] shuffleArray(int[] a) { int n = a.length; Random random = new Random(); random.nextInt(); for (int i = 0; i < n; i++) { int change = i + random.nextInt(n - i); swap(a, i, change); } return a; } private void swap(int[] a, int i, int change) { int helper = a[i]; a[i] = a[change]; a[change] = helper; } public static String stringarize(int[] A) { String output = ""; for(int a : A) { output += " " + a; } return output; } }