物理吧 关注:1,223,942贴子:5,253,998
  • 2回复贴,共1

【水】今天在java上编了个数组排序程序,发上来给大家吐槽

只看楼主收藏回复

结果:
{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]+" ");
}
}
}


IP属地:广东1楼2013-05-09 09:54回复
    stupid sort? = =


    IP属地:加拿大2楼2013-05-09 09:59
    收起回复