大小值排序(JAVA语言)-- 番外篇,造一个排序

视频讲解地址:【手把手带你写十大排序】12.番外篇-自己也能造排序大小值排序(Java语言)_哔哩哔哩_bilibili

代码:

public class BuouSort1 {
    public void swap(int[] array, int index1, int index2) {
        array[index1] = array[index1] ^ array[index2];
        array[index2] = array[index1] ^ array[index2];
        array[index1] = array[index1] ^ array[index2];
    }
    public void sortFunction(int[] array) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i : array) {
            min = Math.min(min, i);
            max = Math.max(max, i);
        }
        int arrayIndex = 0;
        for (int i = min; i <= max; i++) {
            for (int j = arrayIndex; j < array.length; j++) {
                if (i == array[j]) {
                    if (arrayIndex != j) {
                        swap(array, arrayIndex, j);
                    }
                    arrayIndex++;
                }
            }
        }
    }
}

你可能感兴趣的:(java,python,算法)