其他分享
首页 > 其他分享> > 八大排序汇总

八大排序汇总

作者:互联网

八大排序汇总

  摘要:在详细的学习完八大排序之后,我们就要进行反复的练习了,我打算每天都写一遍,这样天天练习必然熟能生巧,还能练习打字速度,在这里我先来一次八大排序的代码汇编,以便有需要时直接查阅参考

目录

1.冒泡排序

    public static void bubbleSort(int[] arr) {// 冒泡排序
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

2.选择排序

    public static void selectionSort(int[] arr) {//选择排序
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    minIndex = j;
                    min = arr[j];
                }
            }
            arr[minIndex] = arr[i];
            arr[i] = min;
        }
    }

3.插入排序

    public static void insertSort(int[] arr) {//插入排序
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0; j--) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                } else {
                    break;
                }
            }
        }
    }

4.希尔排序

    public static void shellSort(int[] arr) {//希尔排序
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i += gap) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (arr[j] > arr[j + gap]) {
                        int temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    } else {
                        break;
                    }
                }
            }
        }
    }

5.基数排序

    public static void ridix(int[] arr) {//基数排序
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i])
                max = arr[i];
        }
        int maxSize = (max + "").length();
        for (int i = 1, n = 1; i <= maxSize; i++, n *= 10) {
            int[][] bucket = new int[10][arr.length];
            int[] bucketElement = new int[10];
            for (int j = 0; j < arr.length; j++) {
                int element = arr[j] / n % 10;
                bucket[element][bucketElement[element]] = arr[j];
                bucketElement[element]++;
            }

            for (int x = 0, index = 0; x < bucketElement.length; x++) {
                if (bucketElement[x] != 0) {
                    for (int z = 0; z < bucketElement[x]; z++) {
                        arr[index] = bucket[x][z];
                        index++;
                    }
                }
                bucketElement[x] = 0;
            }

        }
        System.out.println(Arrays.toString(arr));
    }

6.快速排序

    public static void quickSort(int arr[], int left, int right) {//快速排序
        if (left >= right) {
            return;
        }
        int i = left;
        int j = right;
        int base = arr[i];
        while (i != j) {
            while (i < j && arr[j] >= base) {
                j--;
            }
            while (i < j && arr[i] <= base) {
                i++;
            }
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
        arr[left] = arr[i];
        arr[i] = base;

        quickSort(arr, left, i - 1);
        quickSort(arr, i + 1, right);
    }

7.归并排序

    public static void emergeSort(int[] arr, int left, int right) {//归并排序
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        emergeSort(arr, left, mid);
        emergeSort(arr, mid + 1, right);

        emerge(arr, left, mid, right);

    }

    public static void emerge(int[] arr, int left, int mid, int right) {
        int trv1 = left;
        int trv2 = mid + 1;
        int[] tempArr = new int[right - left + 1];
        int tempTrv = 0;
        while (trv1 <= mid && trv2 <= right) {
            if (arr[trv1] < arr[trv2]) {
                tempArr[tempTrv++] = arr[trv1++];
            } else {
                tempArr[tempTrv++] = arr[trv2++];
            }
        }
        while (trv1 <= mid) {
            tempArr[tempTrv++] = arr[trv1++];
        }
        while (trv2 <= right) {
            tempArr[tempTrv++] = arr[trv2++];
        }
        for (int i = 0; i < tempArr.length; i++) {
            arr[left + i] = tempArr[i];
        }
    }

8.堆排序

    public static void heapSort(int[] arr) {//堆排序
        for (int i = arr.length - 1; i >= 0; i--) {
            heapAdjest(arr, i, arr.length);
        }

        for (int i = 1; i < arr.length; i++) {
            int temp = arr[0];
            arr[0] = arr[arr.length - i];
            arr[arr.length - i] = temp;
            heapAdjest(arr, 0, arr.length - i);
        }

    }

    public static void heapAdjest(int[] arr, int parent, int length) {
        int child = parent * 2 + 1;
        int temp = arr[parent];
        while (child < length) {
            if (child + 1 < length && arr[child] < arr[child + 1]) {
                child++;
            }
            if (temp >= arr[child]) {
                break;
            }
            arr[parent] = arr[child];

            parent = child;
            child = parent * 2 + 1;
        }
        arr[parent] = temp;
    }

标签:arr,八大,temp,int,汇总,length,排序,left
来源: https://www.cnblogs.com/B307-NCAI/p/16170969.html