zoukankan      html  css  js  c++  java
  • 数据结构和算法基础之归并排序

            /// <summary>
            /// 归并排序
            /// </summary>
            public static  void MergetSort(int[] arry,int first,int end)
            {
                if(first<end)
                {
                    int mid = (end + first) / 2;
                    MergetSort(arry, first, mid);
                    MergetSort(arry, mid+1, end);
                    Merging(arry, first, mid, end);
                }
               
            }
    
            public static void Merging(int[] arry,int first,int mid,int end)
            {
                int[] tmp = new int[end - first + 1];
                int tmpIndex = 0;
                int start = first;
                int n = mid+1;
                while(start<=mid&&n<end)
                {
                   if(arry[start]<arry[n])
                    {
                        tmp[tmpIndex] = arry[start];
                        start++;
                        tmpIndex++;
                    }else
                    {
                        tmp[tmpIndex] = arry[n];
                        n++;
                        tmpIndex++;
                    }
                }
                while (start <=mid)
                {
                    tmp[tmpIndex] = arry[start];
                    start++;
                    tmpIndex++;
                }
                while (n<end)
                {
                    tmp[tmpIndex] = arry[n];
                    n++;
                    tmpIndex++;
                }
    
                for ( tmpIndex = 0,start=first;start<end;tmpIndex++,start++ )
                {
                    arry[start] = tmp[tmpIndex];
                }
            }

     时间复杂度:nlog(n)

  • 相关阅读:
    远程仓库
    本地仓库
    仓库
    坐标和依赖
    my24_mysql索引-使用篇
    my23_pxc其中一个节点重建记录
    1.1 Rust安装
    1.2 学习笔记之数据类型
    my22_mydumper 使用总结
    my21_myloader -o参数
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/10544817.html
Copyright © 2011-2022 走看看