漫画:三种 “奇葩” 的排序算法代码实现
1: 睡眠排序
public class Main2 { public static void sleepSort(int[] array) { for (int i : array) { new Thread(()->{ try { Thread.sleep(i); } catch (Exception e) { e.printStackTrace(); } System.out.println(i); }).start(); } } public static void main(String[] args) { int[] array = { 10, 30, 50, 60, 100, 40, 150, 200, 70 }; sleepSort(array); } }
2:猴子排序
package app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class randSort{ public static void randSortX(int [] array){ List<Integer> list=new ArrayList<>(); for (Integer integer : array) { list.add(integer); } int pre=0; int index=0; while(true){ pre=0; for (index = 1; index < list.size(); index++) { if(list.get(index)>list.get(pre)){ pre++; }else{ break; } } if(pre+1==list.size()){ break; } Collections.shuffle(list); } System.out.println(list.toString()); } public static void main(String[] args) { int[] array = { 10, 30, 50, 60, 100, 40, 150, 200, 70 }; randSortX(array); } }
3:3.珠排序