簡體   English   中英

Java中的合並排序算法

[英]Mergesort Algorithm in Java

我嘗試在 Java 中編寫合並排序算法:

static void merge(int[] sort, int l, int m, int r) {
    int[] cache_array = new int[r - l + 1];
    int l_cache = l;
    int _mid = m + 1;
    for (int i = 0; i < r - l + 1; i++) {
        if (l > m) {
            cache_array[i] = sort[_mid];
            _mid++;
        } else { if (_mid > r) {
            cache_array[i] = sort[l];
            l++;
        } else { if (sort[l] >= sort[_mid]) {
            cache_array[i] = sort[l];
            l++;
        } else { if (sort[_mid] > sort[l]) {
            cache_array[i] = sort[_mid];
            _mid++;
        }}}}
    }
    for (int i = 0; i < cache_array.length; i++) {
        sort[i + l_cache] = cache_array[i];
    }
}

static void mergeSort(int[] sort, int l, int r) {
    if (l < r) {
        int mid = (int)Math.floor((l + r - 1) / 2);
        mergeSort(sort, l, mid);
        mergeSort(sort, mid + 1, r);
        merge(sort, l, mid, r);
    }
}
    
public static void main(String[] args) {
    int[] a = { 2, 1, 4, 5, 73, 74, 7, 5, 64, 2 };
    mergeSort(a, 0, a.length - 1);
    for (int i : a) {
        System.out.println(i);
    }
}

但它只是對數組的一部分進行排序,並用零替換它的 rest。 我試圖將 cache_array 更改為 LinkedList 但沒有任何改變,在我嘗試調試后我也找不到任何東西。 如果您能幫助我和/或向我展示另一個適用於 Java 的合並排序算法,我將不勝感激。 (我使用了這個算法,因為它適用於 Python,所以我想在 Java 中使用類似的代碼)

您的代碼中的錯誤很難發現:

  • merge中的循環 function 為i0迭代到r - l + 1 l ,如果rl在左側循環中復制的時間保持不變,這將是正確的迭代。 結果,循環提前退出,將cache_array中的剩余元素保留為默認值0

代碼中有多種混淆來源:

  • 在切片中包含r的約定令人困惑:它需要+1 / -1調整來計算切片長度和中間索引。
  • 使用Math.floor()是沒用的:integer 算法在 java 中使用 integer 除法。
  • 增加lm arguments 會令人困惑,因為如果值更改,它們就會失去意義。 使用其他索引變量遍歷 arrays。
  • elseif關鍵字之間添加一個{會引入不必要的縮進級別。
  • 最后一個條件與前一個條件相反:你應該省略它。 請注意,如果數組元素是浮點值,則 NaN 值的兩個條件都可能為假,並且cache_array的某些元素將保持不變。 在這種情況下,最后一個條件會導致錯誤。

這是修改后的版本:

// merge adjacent slices of the `sort` array.
// left slice has elements from `l` included to `m` excluded
// right slice has elements from `m` included to `r` excluded
static void merge(int[] sort, int l, int m, int r) {
    int len = r - l;
    int[] cache_array = new int[len];
    for (int i = 0, ll = l, mm = m; i < len; i++) {
        if (ll >= m) {
            cache_array[i] = sort[mm];
            mm++;
        } else
        if (mm >= r) {
            cache_array[i] = sort[ll];
            ll++;
        } else
        if (sort[ll] >= sort[mm]) {
            cache_array[i] = sort[ll];
            ll++;
        } else {
            cache_array[i] = sort[mm];
            mm++;
        }
    }
    for (int i = 0; i < len; i++) {
        sort[l + i] = cache_array[i];
    }
}

static void mergeSort(int[] sort, int l, int r) {
    if (r - l > 1) {
        int mid = l + (r - l) / 2;
        mergeSort(sort, l, mid);
        mergeSort(sort, mid, r);
        merge(sort, l, mid, r);
    }
}
    
public static void main(String[] args) {
    int[] a = { 2, 1, 4, 5, 73, 74, 7, 5, 64, 2 };
    mergeSort(a, 0, a.length);
    for (int i : a) {
        System.out.println(i);
    }
}

這就是我編寫歸並排序算法的方式。

public static int[] mergeSort(int[] sort) {
    if(sort.length > 1) {
        int mid = sort.length / 2;
        int[] left = Arrays.copyOf(sort, mid);
        int[] right = Arrays.copyOfRange(sort, mid, sort.length);

        // sort the left and right arrays
        mergeSort(left);
        mergeSort(right);

        // Merge the arrays
        merge(sort, left, right);
    }
}

private static void merge(int[] sort, int[] leftArray, int[] rightArray) {
    // These values are just to keep track of our position in each of the 3 
    // arrays
    int l = 0; // left array
    int r = 0; // right array
    int o = 0; // the actual array being sorted

    while(l < leftArray.length && r < rightArray.length) {
        if(leftArray[l] < righArray[r]) {
            sort[o++] = leftArray[l++];
        }
        else {
            sort[o++] = leftArray[r++];
        }
    }

    // Now that we are out of the while loop we know that either the 
    // left or right array has all of its values in sort, so we just 
    // need to put the rest of the values in the array that doesn't have
    // all of its elements in sort with the following code.

    while(l < leftArray.length) {
        sort[o++] = leftArray[l++];
    }

    while(r < rightArray.length) {
        sort[o++] = rightArray[r++];
    }
}

我通常是這樣實現的:

/// <summary>
/// Mergesort
/// best-case: O(n* log(n))
/// average-case: O(n* log(n))
/// worst-case: O(n* log(n))
/// </summary>
/// <returns>The sorted array.</returns>
/// <param name="array">array.</param>
public static int[] MergeSort(int[] array) {
        //  Exit condition for recursion
        if (array.length <= 1) return array;
        //  Middle index of list to sort
        int m = array.length / 2;
        //  Define left and right sub-listså
        int[] left_array = new int[m];
        int[] right_array = new int[array.length - m];
        //  Initialize left list
        for (int i = 0; i < m; i++) left_array[i] = array[i];
        //  Initialize right list
        for (int i = m, x = 0; i < array.length; i++, x++) right_array[x] = array[i];
        //  Recursively sort left half of the list
        left_array = MergeSort(left_array);
        //  Recursively sort right half of the list
        right_array = MergeSort(right_array);
        //  Merge sorted sub-lists
        return Merge(left_array, right_array);
}
/// <summary>
/// Merge the specified left_array and right_array.
/// </summary>
/// <returns>The merge.</returns>
/// <param name="left_array">Left array.</param>
/// <param name="right_array">Right array.</param>
public static int[] Merge(int[] left_array, int[] right_array) {
        int[] m = new int[left_array.length + right_array.length];
        int index_l = 0;
        int nl, nr;
        nl = left_array.length - 1;
        nr = right_array.length - 1;
        for (int i = 0; i <= nl + nr + 1; i++) {
                if (index_l > nl) {
                        m[i] = (right_array[i - index_l]);
                        continue;
                }
                if (index_l < i - nr) {
                        m[i] = (left_array[index_l]);
                        index_l++;
                        continue;
                }
                if (left_array[index_l] <= (right_array[i - index_l])) {
                        m[i] = (left_array[index_l]);
                        index_l++;
                } else {
                        m[i] = (right_array[i - index_l]);
                }
        }
        return m;
}

幾個月前,我編寫了所有常見的排序算法,這就是我得到的。 有點不准確,但只是為了看看這個實現是如何執行的。其他算法在這里。

要實現降序,我認為您只需要交換比較運算符即可。

基准

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM