结果:
{1,8,1,4,2,6,21,32512,32,42,4,6}排序用了53秒。
程序代码:
public class BogoSort {
public static int[] perform(int[] input){
ArrayList<Integer> temp = new ArrayList<Integer>();
for(int i=0;i<input.length;i++){
temp.add(new Integer(input[i]));
}
int i = 0;
while(temp.size()>0){
int j = (int)(Math.random()*temp.size());
input[i] = temp.get(j);
temp.remove(j);
i++;
}
return input;
}
public static boolean ordered(int[] input){
boolean result = true;
for(int i=1;i<input.length;i++){
if(input[i-1]>input[i]){
result = false;
}
}
return result;
}
public static int[] sort(int[] input){
while(!ordered(input)){
input = perform(input);
}
return input;
}
public static void main(String args[]){
int[] a = {1,8,1,4,2,6,21,32512,32,42,4,6};
int[] b = sort(a);
for(int i=0;i<a.length;i++){
System.out.print(b[i]+" ");
}
}
}
{1,8,1,4,2,6,21,32512,32,42,4,6}排序用了53秒。
程序代码:
public class BogoSort {
public static int[] perform(int[] input){
ArrayList<Integer> temp = new ArrayList<Integer>();
for(int i=0;i<input.length;i++){
temp.add(new Integer(input[i]));
}
int i = 0;
while(temp.size()>0){
int j = (int)(Math.random()*temp.size());
input[i] = temp.get(j);
temp.remove(j);
i++;
}
return input;
}
public static boolean ordered(int[] input){
boolean result = true;
for(int i=1;i<input.length;i++){
if(input[i-1]>input[i]){
result = false;
}
}
return result;
}
public static int[] sort(int[] input){
while(!ordered(input)){
input = perform(input);
}
return input;
}
public static void main(String args[]){
int[] a = {1,8,1,4,2,6,21,32512,32,42,4,6};
int[] b = sort(a);
for(int i=0;i<a.length;i++){
System.out.print(b[i]+" ");
}
}
}