分治法:用C#实现归并排序
根据《算法设计与分析基础》中对归并排序的描述,写了一分C#代码实现。
具体的实现代码如下:
  1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 namespace MergeSort
namespace MergeSort
6 {
{
7 class Program
    class Program
8 {
    {
9 static void Main(string[] args)
        static void Main(string[] args)
10 {
        {
11 Program p = new Program();
            Program p = new Program();
12
13 int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
            int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
14
15 p.Sort(a);
            p.Sort(a);
16
17 for (int i = 0; i < a.Length; i++)
            for (int i = 0; i < a.Length; i++)
18 {
            {
19 System.Console.WriteLine(a[i]);
                System.Console.WriteLine(a[i]);
20 }
            }
21 }
        }
22
23 /// <summary>
        /// <summary>
24 /// 利用归并排序按由下到大的顺序排序
        /// 利用归并排序按由下到大的顺序排序
25 /// </summary>
        /// </summary>
26 /// <param name="toBeSort"></param>
        /// <param name="toBeSort"></param>
27 /// <returns></returns>
        /// <returns></returns>
28 public int[] Sort(int[] toBeSort)
        public int[] Sort(int[] toBeSort)
29 {
        {
30 if (toBeSort.Length == 0)
            if (toBeSort.Length == 0)
31 {
            {
32 throw new Exception("Sort array Error!");
                throw new Exception("Sort array Error!");
33 }
            }
34
35 if (toBeSort.Length > 1)
            if (toBeSort.Length > 1)
36 {
            {
37 int[] part1 = Sort(get1Part(toBeSort));
                int[] part1 = Sort(get1Part(toBeSort));
38 int[] part2 = Sort(get2Part(toBeSort));
                int[] part2 = Sort(get2Part(toBeSort));
39
40 merger(part1, part2, toBeSort);
                merger(part1, part2, toBeSort);
41 }
            }
42
43 return toBeSort;
            return toBeSort;
44 }
        }
45
46 /// <summary>
        /// <summary>
47 /// 将part1和part2按照由小到大的顺序归并到数组toBeSort中
        /// 将part1和part2按照由小到大的顺序归并到数组toBeSort中
48 /// </summary>
        /// </summary>
49 /// <param name="part1"></param>
        /// <param name="part1"></param>
50 /// <param name="part2"></param>
        /// <param name="part2"></param>
51 /// <param name="toBeSort"></param>
        /// <param name="toBeSort"></param>
52 private void merger(int[] part1, int[] part2, int[] toBeSort)
        private void merger(int[] part1, int[] part2, int[] toBeSort)
53 {
        {
54 int index = 0;
            int index = 0;
55 int i1 = 0;
            int i1 = 0;
56 int i2 = 0;
            int i2 = 0;
57
58 while (i1 < part1.Length && i2 < part2.Length)
            while (i1 < part1.Length && i2 < part2.Length)
59 {
            {
60 if (part1[i1] < part2[i2])
                if (part1[i1] < part2[i2])
61 {
                {
62 toBeSort[index] = part1[i1];
                    toBeSort[index] = part1[i1];
63 i1++;
                    i1++;
64 }
                }
65 else
                else
66 {
                {
67 toBeSort[index] = part2[i2];
                    toBeSort[index] = part2[i2];
68 i2++;
                    i2++;
69 }
                }
70
71 index++;
                index++;
72 }
            }
73
74 while (i1 < part1.Length)
            while (i1 < part1.Length)
75 {
            {
76 toBeSort[index] = part1[i1];
                toBeSort[index] = part1[i1];
77 index++;
                index++;
78 i1++;
                i1++;
79 }
            }
80
81 while (i2 < part2.Length)
            while (i2 < part2.Length)
82 {
            {
83 toBeSort[index] = part2[i2];
                toBeSort[index] = part2[i2];
84 index++;
                index++;
85 i2++;
                i2++;
86 }
            }
87 }
        }
88
89 /// <summary>
        /// <summary>
90 /// Get the second part of the array.
        /// Get the second part of the array.
91 /// </summary>
        /// </summary>
92 /// <param name="toBeSort">The array to be sort.</param>
        /// <param name="toBeSort">The array to be sort.</param>
93 /// <returns>The second part of the array.</returns>
        /// <returns>The second part of the array.</returns>
94 private int[] get2Part(int[] toBeSort)
        private int[] get2Part(int[] toBeSort)
95 {
        {
96 int len = toBeSort.Length - toBeSort.Length / 2;
            int len = toBeSort.Length - toBeSort.Length / 2;
97 int[] part2 = new int[len];
            int[] part2 = new int[len];
98
99 Array.Copy(toBeSort, toBeSort.Length / 2, part2, 0, len);
            Array.Copy(toBeSort, toBeSort.Length / 2, part2, 0, len);
100
101 return part2;
            return part2;
102 }
        }
103
104 /// <summary>
        /// <summary>
105 /// Get the first part of the array.
        /// Get the first part of the array.
106 /// </summary>
        /// </summary>
107 /// <param name="toBeSort">The array to be sort.</param>
        /// <param name="toBeSort">The array to be sort.</param>
108 /// <returns>The first part of the array.</returns>
        /// <returns>The first part of the array.</returns>
109 private int[] get1Part(int[] toBeSort)
        private int[] get1Part(int[] toBeSort)
110 {
        {
111 int len = toBeSort.Length / 2;
            int len = toBeSort.Length / 2;
112 int[] part1 = new int[len];
            int[] part1 = new int[len];
113 
            
114 Array.Copy(toBeSort, part1, len);
            Array.Copy(toBeSort, part1, len);
115
116 return part1;
            return part1;
117 }
        }
118 }
    }
119 }
}
120
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace MergeSort
namespace MergeSort6
 {
{7
 class Program
    class Program8
 {
    {9
 static void Main(string[] args)
        static void Main(string[] args)10
 {
        {11
 Program p = new Program();
            Program p = new Program();12

13
 int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
            int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };14

15
 p.Sort(a);
            p.Sort(a);16

17
 for (int i = 0; i < a.Length; i++)
            for (int i = 0; i < a.Length; i++)18
 {
            {19
 System.Console.WriteLine(a[i]);
                System.Console.WriteLine(a[i]);20
 }
            }21
 }
        }22

23
 /// <summary>
        /// <summary>24
 /// 利用归并排序按由下到大的顺序排序
        /// 利用归并排序按由下到大的顺序排序25
 /// </summary>
        /// </summary>26
 /// <param name="toBeSort"></param>
        /// <param name="toBeSort"></param>27
 /// <returns></returns>
        /// <returns></returns>28
 public int[] Sort(int[] toBeSort)
        public int[] Sort(int[] toBeSort)29
 {
        {30
 if (toBeSort.Length == 0)
            if (toBeSort.Length == 0)31
 {
            {32
 throw new Exception("Sort array Error!");
                throw new Exception("Sort array Error!");33
 }
            }34

35
 if (toBeSort.Length > 1)
            if (toBeSort.Length > 1)36
 {
            {37
 int[] part1 = Sort(get1Part(toBeSort));
                int[] part1 = Sort(get1Part(toBeSort));38
 int[] part2 = Sort(get2Part(toBeSort));
                int[] part2 = Sort(get2Part(toBeSort));39

40
 merger(part1, part2, toBeSort);
                merger(part1, part2, toBeSort);41
 }
            }42

43
 return toBeSort;
            return toBeSort;44
 }
        }45

46
 /// <summary>
        /// <summary>47
 /// 将part1和part2按照由小到大的顺序归并到数组toBeSort中
        /// 将part1和part2按照由小到大的顺序归并到数组toBeSort中48
 /// </summary>
        /// </summary>49
 /// <param name="part1"></param>
        /// <param name="part1"></param>50
 /// <param name="part2"></param>
        /// <param name="part2"></param>51
 /// <param name="toBeSort"></param>
        /// <param name="toBeSort"></param>52
 private void merger(int[] part1, int[] part2, int[] toBeSort)
        private void merger(int[] part1, int[] part2, int[] toBeSort)53
 {
        {54
 int index = 0;
            int index = 0;55
 int i1 = 0;
            int i1 = 0;56
 int i2 = 0;
            int i2 = 0;57

58
 while (i1 < part1.Length && i2 < part2.Length)
            while (i1 < part1.Length && i2 < part2.Length)59
 {
            {60
 if (part1[i1] < part2[i2])
                if (part1[i1] < part2[i2])61
 {
                {62
 toBeSort[index] = part1[i1];
                    toBeSort[index] = part1[i1];63
 i1++;
                    i1++;64
 }
                }65
 else
                else66
 {
                {67
 toBeSort[index] = part2[i2];
                    toBeSort[index] = part2[i2];68
 i2++;
                    i2++;69
 }
                }70

71
 index++;
                index++;72
 }
            }73

74
 while (i1 < part1.Length)
            while (i1 < part1.Length)75
 {
            {76
 toBeSort[index] = part1[i1];
                toBeSort[index] = part1[i1];77
 index++;
                index++;78
 i1++;
                i1++;79
 }
            }80

81
 while (i2 < part2.Length)
            while (i2 < part2.Length)82
 {
            {83
 toBeSort[index] = part2[i2];
                toBeSort[index] = part2[i2];84
 index++;
                index++;85
 i2++;
                i2++;86
 }
            }87
 }
        }88

89
 /// <summary>
        /// <summary>90
 /// Get the second part of the array.
        /// Get the second part of the array.91
 /// </summary>
        /// </summary>92
 /// <param name="toBeSort">The array to be sort.</param>
        /// <param name="toBeSort">The array to be sort.</param>93
 /// <returns>The second part of the array.</returns>
        /// <returns>The second part of the array.</returns>94
 private int[] get2Part(int[] toBeSort)
        private int[] get2Part(int[] toBeSort)95
 {
        {96
 int len = toBeSort.Length - toBeSort.Length / 2;
            int len = toBeSort.Length - toBeSort.Length / 2;97
 int[] part2 = new int[len];
            int[] part2 = new int[len];98

99
 Array.Copy(toBeSort, toBeSort.Length / 2, part2, 0, len);
            Array.Copy(toBeSort, toBeSort.Length / 2, part2, 0, len);100

101
 return part2;
            return part2;102
 }
        }103

104
 /// <summary>
        /// <summary>105
 /// Get the first part of the array.
        /// Get the first part of the array.106
 /// </summary>
        /// </summary>107
 /// <param name="toBeSort">The array to be sort.</param>
        /// <param name="toBeSort">The array to be sort.</param>108
 /// <returns>The first part of the array.</returns>
        /// <returns>The first part of the array.</returns>109
 private int[] get1Part(int[] toBeSort)
        private int[] get1Part(int[] toBeSort)110
 {
        {111
 int len = toBeSort.Length / 2;
            int len = toBeSort.Length / 2;112
 int[] part1 = new int[len];
            int[] part1 = new int[len];113
 
            114
 Array.Copy(toBeSort, part1, len);
            Array.Copy(toBeSort, part1, len);115

116
 return part1;
            return part1;117
 }
        }118
 }
    }119
 }
}120

对于分治法的效率分析,有一个通用的公示可以使用:通用分治递推公式。
 
                    
                     
                    
                 
                    
                 
 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号