迭代合并排序的 Java 程序

原文:https://www . geesforgeks . org/Java-program-for-iterative-merge-sort/

以下是合并排序的典型递归实现,它使用最后一个元素作为轴心。

Java 语言(一种计算机语言,尤用于创建网站)

// Recursive Java Program for merge sort

import java.util.Arrays;
public class GFG
{
    public static void mergeSort(int[] array)
    {
        if(array == null)
        {
            return;
        }

        if(array.length > 1)
        {
            int mid = array.length / 2;

            // Split left part
            int[] left = new int[mid];
            for(int i = 0; i < mid; i++)
            {
                left[i] = array[i];
            }

            // Split right part
            int[] right = new int[array.length - mid];
            for(int i = mid; i < array.length; i++)
            {
                right[i - mid] = array[i];
            }
            mergeSort(left);
            mergeSort(right);

            int i = 0;
            int j = 0;
            int k = 0;

            // Merge left and right arrays
            while(i < left.length && j < right.length)
            {
                if(left[i] < right[j])
                {
                    array[k] = left[i];
                    i++;
                }
                else
                {
                    array[k] = right[j];
                    j++;
                }
                k++;
            }
            // Collect remaining elements
            while(i < left.length)
            {
                array[k] = left[i];
                i++;
                k++;
            }
            while(j < right.length)
            {
                array[k] = right[j];
                j++;
                k++;
            }
        }
    }
    // Driver program to test above functions.
    public static void main(String[] args)
    {
        int arr[] = {12, 11, 13, 5, 6, 7};
        int i=0;
        System.out.println("Given array is");

        for(i=0; i<arr.length; i++)
            System.out.print(arr[i]+" ");

        mergeSort(arr);

        System.out.println("\n");
        System.out.println("Sorted array is");

        for(i=0; i<arr.length; i++)
            System.out.print(arr[i]+" ");
    }
}

// Code Contributed by Mohit Gupta_OMG

更多详情请参考迭代合并排序整篇文章!