减治法:C#实现插入排序
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace InsertionSort
6
{
7
class Program
8
{
9
static void Main(string[] args)
10
{
11
int[] a = new int[] { 2, 1, 4, 3 , 1, -4, 7, 11, 9,3,0};
12
13
Program p = new Program();
14
15
p.Sort(a);
16
17
for (int i = 0; i < a.Length; i++)
18
{
19
System.Console.WriteLine(a[i]);
20
}
21
}
22
23
public void Sort(int[] toBeSort)
24
{
25
for (int i = 1; i < toBeSort.Length; i++)
26
{
27
int v = toBeSort[i];
28
29
for (int j = i - 1; j > -1; j--)
30
{
31
if (v < toBeSort[j])
32
{
33
toBeSort[j + 1] = toBeSort[j];
34
toBeSort[j] = v;
35
}
36
}
37
}
38
}
39
}
40
}
41
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace InsertionSort6
{7
class Program8
{9
static void Main(string[] args)10
{11
int[] a = new int[] { 2, 1, 4, 3 , 1, -4, 7, 11, 9,3,0};12

13
Program p = new Program();14

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

23
public void Sort(int[] toBeSort)24
{25
for (int i = 1; i < toBeSort.Length; i++)26
{27
int v = toBeSort[i];28

29
for (int j = i - 1; j > -1; j--)30
{31
if (v < toBeSort[j])32
{33
toBeSort[j + 1] = toBeSort[j];34
toBeSort[j] = v;35
}36
}37
}38
}39
}40
}41



浙公网安备 33010602011771号