溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Java歸并排序和快速排序怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2021-12-29 15:49:13 來(lái)源:億速云 閱讀:143 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“Java歸并排序和快速排序怎么實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

歸并排序

// 歸并排序
    public static void mergeSort (int[] arr) {
        // 建一個(gè)臨時(shí)數(shù)據(jù)來(lái)存放數(shù)據(jù)
        int[] temp = new int[arr.length];
        mergeSort(arr, 0, arr.length - 1, temp);
    }
    private static void mergeSort(int[] arr, int left, int right, int[] temp) {
        if (left < right) { // 如果起始下標(biāo)跟結(jié)束下標(biāo)差值小于1,則不進(jìn)行操作
            int mid = (left + right) / 2;
            mergeSort(arr, left, mid, temp); // 分組,將左邊分為一組,遞歸調(diào)用進(jìn)行排序
            mergeSort(arr, mid+1, right, temp); // 將右邊分為一組
            merge(arr, left, mid, right, temp); //將左右分組合并
        }
    }
    private static void merge(int[] arr, int left, int mid, int right, int[] temp) {
        int i = left; // 定義左指針
        int j = mid + 1; // 定義右指針
        int t = 0; // 給temp臨時(shí)數(shù)組用的指針
        while (i <= mid && j <= right) { // 設(shè)置左右指針的移動(dòng)邊界
            if (arr[i] <= arr[j]) { // 此處是升序,故誰(shuí)小誰(shuí)先賦給臨時(shí)數(shù)組
                temp[t++] = arr[i++];
            } else {
                temp[t++] = arr[j++];
            }
        }
        while (i <= mid) { // 如果左邊有剩余,則放在temp中
            temp[t++] = arr[i++];
        }
        while (j <= right) { // 如果右邊有剩余,依次放入temp中
            temp[t++] = arr[j++];
        }
        t = 0;
        // 此時(shí)temp中已經(jīng)是arr數(shù)組中下標(biāo)從left到right之間排好序的數(shù)據(jù)了,因?yàn)閠emp每次都是從0開(kāi)始賦值,所以需將排好序的數(shù)放回arr的對(duì)應(yīng)位置
        while (left <= right) {
            // 將left到right之間排好序的數(shù)據(jù)放回arr中,此時(shí)left到right之間的數(shù)就是最終排好序的數(shù)
            arr[left++] = temp[t++];
        }
    }

快速排序

// 快速排序
    public static void quickSort (int[] arr, int left, int right) {
        // 先將異常情況處理掉
        if (arr == null || arr.length < 2) {
            return;
        }
        if (right <= left) {
            return;
        }
        if (right - left == 1 && arr[left] <= arr[right]) {
            return;
        }
        // 取第一個(gè)數(shù)為基準(zhǔn)數(shù)(基數(shù)取哪個(gè)都行,此處是為了方便)
        int index = arr[left];
        int i = left + 1; // 左指針
        int j = right; // 右指針
        while (i < j && i < right && j > left) { // 設(shè)置指針的移動(dòng)邊界
            while (arr[j] > index && j > left) {j--;} // 找到從右邊數(shù)第一個(gè)比index小的數(shù)
            while (arr[i] < index && i < right) {i++;} // 找到從左邊數(shù)第一個(gè)比index大的數(shù)
            if (i < j) { // 交換這兩個(gè)數(shù)  如果i == j,說(shuō)明二者定位到了同一個(gè)位置,則不用交換;如果i > j,說(shuō)明二者已經(jīng)相遇然后背向而行了,也不交換
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        // 執(zhí)行完上面循環(huán)后,arr已經(jīng)是左邊比index小,右邊比index大的數(shù)組了,只是基準(zhǔn)數(shù)仍在基準(zhǔn)位置left處,需放到它應(yīng)該在的位置
        if (j != left && arr[j] != arr[left]) {
            // j最后停留位置的數(shù),肯定是一個(gè)小于等于index的值,所以如果不是同一個(gè)位置的話,直接將二者調(diào)換一下位置即可
            int temp = arr[j];
            arr[j] = arr[left];
            arr[left] = temp;
        }
        quickSort(arr, left, j-1); // 將基準(zhǔn)數(shù)左邊排序
        quickSort(arr, j+1, right); // 將基準(zhǔn)數(shù)右邊排序
    }

“Java歸并排序和快速排序怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI