惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
Vercel News
Vercel News
D
Docker
博客园 - 聂微东
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
D
DataBreaches.Net
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
N
Netflix TechBlog - Medium
G
Google Developers Blog
腾讯CDC

博客园 - Forrest Gump

关于LINQ中数据库连接字符串的问题 项目开发经验-ASP.NET项目开发中的异常处理 关于模态窗口(showModalDialog)的专题【收藏】 C#面试题 C# 将数据导出到Excel汇总 关于Assembly.CreateInstance()与Activator.CreateInstance()方法 PowerDesigner概念设计模型(CDM)中的3种实体关系 C#基础概念二十五问 Microsoft .NET Pet Shop 4:将 ASP.NET 1.1 应用程序迁移到 2.0 用Inno Setup制作WEB程序安装包 冒泡法数组排序与 System.Array.Sort()排序性能比较 堆排序 (Heap sort) 希尔排序法 quick sort 关于switch的小技巧 C#中一些很基础但有经常导致错误的一些概念 Enterprise Library Step By Step系列(十六):使用AppSetting Application Block Enterprise Library Step By Step系列(十五):配置应用程序块——设计篇 创建基于消息队列(MSMQ)的异步日志
合并排序法(Merge Sort)
Forrest Gump · 2008-01-28 · via 博客园 - Forrest Gump

基于分治思想的合并排序,算法导论中的思考题,不加哨兵牌(sentinel card)的实现方式,实现很简单 : )

using System;
class MergeSort
{
    
public void merge(int[] A, int p, int q, int r)
    
{
        
int n1 = q-p+1;
        
int n2 = r-q;
        
int[] L = new int[n1];
        
int[] R = new int[n2];
        
for (int t=0; t<n1; t++) L[t] = A[p+t];
        
for (int t=0; t<n2; t++) R[t] = A[q+t+1];
        
int i = 0;
        
int j = 0;
        
int k = p;
        
while (i<n1 && j<n2)    //一个数组输出完毕后跳出此循环
            if (L[i] <= R[j]) 
                 A[k
++= L[i++];
            
else A[k++= R[j++];    //L或R数组输出完毕后把另一个数组余下的部分全部输出
        while (i<n1) A[k++= L[i++];    //一下两个while永远只有一个执行,即未输出完的数组继续输出剩余部分
        while (j<n2) A[k++= R[j++];
    }

    
public void sort(int[] A, int p, int r)
    
{
        
if (p<r)
        
{
            
int q = (p+r)/2;
            sort(A, p, q);
            sort(A, q
+1, r);
            merge(A, p, q, r);
        }

    }

}

class Program
{
    
public static void Main()
    
{
        Random rnd 
= new Random();
        
int[] data = new int[10];
        
for (int i=0; i<10; i++)
        
{
            data[i] 
= rnd.Next(100);
            Console.Write(
"{0}\t",data[i]);
        }

        Console.WriteLine();
        MergeSort s 
= new MergeSort();
        s.sort(data, 
09);
        
for (int i=0; i<10; i++)
            Console.Write(
"{0}\t",data[i]);
    }

}