沙雕排序算法
猴子排序
private static void monkeySort(int[] nums) {
List<Integer> temp = Arrays.stream(nums).boxed().collect(Collectors.toList());
out:while (true) {
Collections.shuffle(temp);
int[] result = temp.stream().mapToInt(Integer::intValue).toArray();
for (int i = 0; i < result.length - 1; i++) {
if (result[i] > result[i + 1]) {
continue out;
}
}
System.arraycopy(result, 0, nums, 0, result.length);
break;
}
}
测试
public static void main(String[] args) {
int[] nums = new int[]{1, 22, 33, 44, 12, 333, 444, 99, 222};
monkeySort(nums);
System.out.println(Arrays.toString(nums));
}
老牛逼之多线程排序
private static void sleepSort(int[] nums) {
for (final int num : nums) {
new Thread(() -> {
try {
Thread.sleep(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(num +" ");
}).start();
}
}
测试
public static void main(String[] args) {
int[] nums = new int[]{1, 22, 33, 44, 12, 333, 444, 99, 222};
sleepSort(nums);
}