zoukankan      html  css  js  c++  java
  • 归并排序


    public class 归并排序的实现
    {
        public static void main(String[] args)
        {
            int[] a =
            { 10, 32, 31, 56, 42, 38, 1, 85 };
            mergeSort(a, 0, a.length - 1);
            for (int i = 0; i < a.length; i++)
            {
                System.out.println(a[i]);
            }
        }

        /*
         * 定义:"归并"的含义是将二个或者两个以上的有序的表组合成一个有序的的表 假设两个有序表的长度分别为m和n 则可以在O(m+n)的时间量级上实现
         * 先分组然后合并
         */
        public static void mergeSort(int[] a, int begin, int end)
        {
            int mid = (begin + end) / 2;
            if (begin < end)
            {
                // 左边
                System.out.println("dfdf");
                mergeSort(a, begin, mid);
                // 右边
                mergeSort(a, mid + 1, end);
                // 合并
                mSort(a, begin, mid, end);
            }
        }

        private static void mSort(int[] a, int begin, int mid, int end)
        {
            int[] temp = new int[end - begin + 1];
            int i = begin, j = mid + 1, k = 0;
            while (i <= mid && j <= end)
            {
                if (a[i] < a[j])
                {
                    temp[k++] = a[i++];
                }
                else
                {
                    temp[k++] = a[j++];
                }
            }
            // 把左边的元素都移入数组当中
            while (i <= mid)
            {
                temp[k++] = a[i++];
            }
            // 把右边的元素都移入数组当中
            while (j <= end)
            {
                temp[k++] = a[j++];
            }
            // 覆盖原来的数组
            for (int k2 = 0; k2 < temp.length; k2++)
            {
                a[k2 + begin] = temp[k2];
            }
        }
    }

  • 相关阅读:
    Linux之ARP——种种
    Linux之内核参数——backlog/somaxconn
    CLOSE_WAIT、CLOSE_WAIT原因,危害,如何避免
    OVS——大杂烩
    Linux 网络栈——
    OpenShift——大杂烩
    协议之ICMP——种种
    Neutron 理解 (8): Neutron 是如何实现虚机防火墙的 [How Neutron Implements Security Group]
    Neutron 理解 (6): Neutron 是怎么实现虚拟三层网络的 [How Neutron implements virtual L3 network]
    CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍)
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/7139155.html
Copyright © 2011-2022 走看看