十大排序算法(Java实现)

2020-01-26 16:01:02来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

十大排序算法(Java实现)

一、冒泡排序(Bubble Sort)

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 9, 10, 15, 11, 0, 1};
        System.out.println(Arrays.toString(bubbleSort(arr)));
    }
    
    public static int[] bubbleSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            for(int j = 0; j < arr.length - 1 - i; j++){
                if(arr[j] > arr[j + 1]){
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }
}

二、选择排序(Selection Sort)

public class SelectionSort {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 9, 10, 15, 11, 0, 1};
        System.out.println(Arrays.toString(selectionSort(arr)));
    }
    
    public static int[] selectionSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[minIndex] > arr[j]){
                    minIndex = j;
                }
            }
            //交换
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
        return arr;
    }
}

三、插入排序(Insertion Sort)

public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 9, 10, 15, 11, 0, 1};
        System.out.println(Arrays.toString(insertionSort(arr)));
    }

    public static int[] insertionSort(int[] arr) {
        int current, preIndex;
        for (int i = 1; i < arr.length; i++) {
            current = arr[i];
            preIndex = i - 1;
            while(preIndex >= 0 && current < arr[preIndex]){
                arr[preIndex + 1] = arr[preIndex];
                preIndex --;
            }
            arr[preIndex + 1] = current;
        }
        return arr;
    }
}

四、希尔排序(Shell Sort)

public class ShellSort {
    public static void main(String[] args) {
        int[] arr = {80, 30, 60, 40, 20, 10, 50, 70};
        System.out.println(Arrays.toString(shellSort(arr)));
    }

    public static int[] shellSort(int[] arr){
        int len = arr.length;
        for (int gap = len / 2; gap > 0; gap /= 2) {//设置步长
            for (int i = gap; i < len ; i++) {
                int j = i;
                int temp = arr[i];
                while(j - gap >= 0 && temp < arr[j - gap]){
                    arr[j] = arr[j - gap];
                    j -= gap;
                }
                arr[j] = temp;
            }
        }
        return arr;
    }
}

五、归并排序(Merge Sort)

public class MergeSort {
    public static void main(String[] args) {
        int[] arr = {10, 8, 1, 4, 5, 3, 2, 9, 10, 22, 8};
        int[] temp = new int[arr.length];
        int[] arr2 = mergeSort(arr, 0, arr.length - 1, temp);
        System.out.println(Arrays.toString(arr2));
    }

    /**
     * 归并排序
     *
     * @param arr
     * @param left
     * @param right
     */
    public static int[] mergeSort(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = (left + right) / 2;
            //向左分解
            mergeSort(arr, left, mid, temp);
            //向右分解
            mergeSort(arr, mid + 1, right, temp);
            //合并
            merge(arr, left, mid, right, temp);
        }
        return arr;
    }

    /**
     * 合并
     *
     * @param arr   原数组
     * @param left  左指针
     * @param mid   中指针
     * @param right 右指针
     * @param temp  中间数组
     */
    public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
        int i = left, j = mid + 1, k = 0;

        //比较数据大小,从小到大放入temp中
        while (i <= mid && j <= right) {
            if (arr[i] < arr[j]) {
                temp[k] = arr[i];
                k++;
                i++;
            } else {
                temp[k] = arr[j];
                k++;
                j++;
            }
        }

        //判断arr中是否有剩余数据
        while (i <= mid) {
            temp[k] = arr[i];
            k++;
            i++;
        }
        while (j <= right) {
            temp[k] = arr[j];
            k++;
            j++;
        }

        k = 0;
        int c = left;
        //将排好序的temp赋给arr
        while (c <= right) {
            arr[c] = temp[k];
            k++;
            c++;
        }
    }
}

六、快速排序(Quick Sort)

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {4, 6, 7, 0, -1, 2, -4};
        int[] arr2 = quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr2));
    }

    private static int[] quickSort(int[] arr, int left, int right) {
        int l = left;
        int r = right;
        int pivot = arr[(l + r) / 2];
        int temp;

        while(l < r){
            while(arr[l] < pivot){
                l++;
            }
            while(arr[r] > pivot){
                r--;
            }
            if(l >= r){
                break;
            }
            //交换
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            if(arr[l] == pivot){
                r--;
            }
            if(arr[r] == pivot){
                l++;
            }
        }

        if(l == r){
            l++;
            r--;
        }

        if(r > left){
            quickSort(arr, left, r);
        }
        if(l < right){
            quickSort(arr, l, right);
        }
        return arr;
    }
}

七、堆排序(Heap Sort)

public class HeapSort {
    public static void main(String[] args) {
        int[] arr = {4, 6, 7, 0, -1, 2, -4};
        System.out.println(Arrays.toString(heapSort(arr)));
    }

    public static int[] heapSort(int[] arr) {
        //以最后一个非叶子结点构建大顶堆
        for (int i = arr.length / 2 - 1; i >= 0; i--) {
            adjustHeap(arr, i, arr.length);
        }
        //此时顶部元素是最大的,交换顶部元素和末端元素
        for (int i = arr.length - 1; i > 0; i--) {
            swap(arr, 0, i);
            //末端元素已经是最大的了,无需考虑排序
            adjustHeap(arr, 0, i);
        }
        return arr;
    }

    public static void adjustHeap(int[] arr, int i, int len) {
        //保存当前结点
        int temp = arr[i];
        //遍历当前结点的左子结点
        for (int k = 2 * i + 1; k < len; k = 2 * k + 1) {
            //如果右结点存在 且 右结点比左结点大,指向右结点
            if (k + 1 < len && arr[k] < arr[k + 1]) {
                k++;
            }
            //判断当前结点和左(右)结点哪个大
            if (temp < arr[k]) {
                //交换
                swap(arr, k, i);
                //交换后,下次遍历以该子结点作为根节点的子树就会受到影响,因此需要重新指定下次的根节点
                i = k;
            } else {
                //不用交换,直接终止循环
                break;
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

八、计数排序(Counting Sort)

public class CountingSort {
    public static void main(String[] args) {
        int[] arr = {4, 6, 7, 0, 1, 2, 4, 1, 7};
        System.out.println(Arrays.toString(countingSort(arr)));
    }

    public static int[] countingSort(int[] arr) {
        int max = arr[0];
        int sortIndex = 0;
        //找到最大值
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }

        int[] bucket = new int[max + 1];

        //将待排序数组的值作为新数组的下标,新数组的下标对应的值就是这个数的个数,
        // 因此待排序数组不能有负数
        for (int i = 0; i < arr.length; i++) {
            bucket[arr[i]]++;
        }

//        System.out.println("新数组:"+Arrays.toString(bucket));

        //将新数组的值重新赋给待排序数组
        for (int i = 0; i < bucket.length; i++) {
            while (bucket[i] > 0) {
                arr[sortIndex++] = i;
                bucket[i]--;
            }
        }

        return arr;
    }
}

九、桶排序(Bucket Sort)

public class BucketSort {
    public static void main(String[] args) {
        int[] arr = {4, 6, 7, 0, 1, 2, 4, 1, 7};
        System.out.println(Arrays.toString(bucketSort(arr)));
    }
    
    public static int[] bucketSort(int[] arr) {
        int min = arr[0], max= arr[0];
        //找到最大值最小值
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] < min){
                min = arr[i];
            }
            if(arr[i] > max){
                max = arr[i];
            }
        }
        int d = max - min;
        int bucketCount = d / 5 + 1; //桶的个数
        ArrayList<LinkedList<Integer>> bucket = new ArrayList<>();

        //初始化
        for (int i = 0; i < bucketCount; i++) {
            bucket.add(new LinkedList<Integer>());
        }

        //遍历待排序数组,将数据放入集合中
        for (int i = 0; i < arr.length; i++) {
            bucket.get((arr[i] - min) / d).add(arr[i] - min);
        }

        //对每个桶里的元素排序
        for (int i = 0; i < bucketCount; i++) {
            Collections.sort(bucket.get(i));
        }

        //将集合的数据放到原数组里
        int k = 0;
        for (int i = 0; i < bucketCount; i++) {
            for (Integer num : bucket.get(i)) {
                arr[k++] = num + min;
            }
        }
        return arr;
    }
}

十、基数排序(Radix Sort)

public class RadixSort {
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 9, 10, 15, 11, 0, 1};
        System.out.println(Arrays.toString(radixSort(arr)));
    }

    public static int[] radixSort(int[] arr) {
        int max = arr[0], n = arr.length;
        //找到最大数
        for (int i = 1; i < n; i++) {
            if(max < arr[i]){
                max = arr[i];
            }
        }
        //判断max是几位数
        int num = 1;
        while(max / 10 > 0){
            num++;
            max /= 10;
        }

        //创建10个桶
        ArrayList<LinkedList<Integer>> bucket = new ArrayList<>(10);

        //初始化
        for (int i = 0; i < 10; i++) {
            bucket.add(new LinkedList<>());
        }

        //遍历原数组,将数据按规则放到桶中
        //从个位开始
        for (int i = 1; i <= num; i++) {
            
            //按照数据位数的数字放到桶中
            for (int j = 0; j < n; j++) {
                //获取数字个位、十位....上的数字
                int radix = (arr[j] / (int) Math.pow(10, i - 1)) % 10;
                bucket.get(radix).add(arr[j]);
            }
            
            //将数据放回原数组
            int k = 0;
            for (int j = 0; j < 10; j++) {
                for(int t : bucket.get(j)){
                    arr[k++] = t;
                }
                //清空桶的数据
                bucket.get(j).clear();
            }
        }

        return arr;
    }
}

原文链接:https://www.cnblogs.com/songjilong/p/12234856.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:到头来还是逃不过Java - 流程控制

下一篇:七、索引优化分析