c#实现的3种排序算法
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
namespace ConsoleApplication1
5
{
6
class Program
7
{
8
static void Main(string[] args)
9
{
10
int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
11
//mpsort(iarrary);
12
// xzsort(iarrary);
13
crsort(iarrary);
14
for (int n = 0; n < iarrary.Length; n++)
15
{
16
Console.WriteLine("{0}",iarrary[n]);
17
}
18
Console.Read();
19
}
20
public static void mpsort(int [] list)//冒泡排序
21
{
22
int i, j, temp;
23
bool exchange;
24
for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
25
{
26
exchange = false;
27
for (j = 0; j < list.Length-i;j++ )
28
{
29
if(list[j]>list[j+1])
30
{
31
temp = list[j];
32
list[j] = list[j + 1];
33
list[j + 1] = temp;
34
exchange = true;
35
}
36
}
37
if (!exchange)
38
{
39
break;
40
}
41
}
42
}
43
public static void xzsort(int[] list)//选择排序
44
{
45
int i,j,temp,min;
46
for (i = 0; i < list.Length; i++)
47
{
48
min = i;
49
for (j = i + 1; j < list.Length;j++ )
50
{
51
if (list[min] > list[j])
52
{
53
min = j;
54
}
55
}
56
temp = list[min];
57
list[min] = list[i];
58
list[i] = temp;
59
}
60
}
61
public static void crsort(int[] list)//插入排序
62
{
63
int i,j,temp;
64
for (i = 1; i < list.Length; i++)
65
{
66
temp = list[i];
67
j = i;
68
while((j > 0)&&(list[j-1]>temp))
69
{
70
list[j]=list[j-1];
71
--j;
72
}
73
list[j]=temp;
74
}
75
}
76
77
}
78
}
79
using System;2
using System.Collections.Generic;3
using System.Text;4
namespace ConsoleApplication15
{6
class Program7
{8
static void Main(string[] args)9
{10
int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};11
//mpsort(iarrary);12
// xzsort(iarrary);13
crsort(iarrary); 14
for (int n = 0; n < iarrary.Length; n++)15
{16
Console.WriteLine("{0}",iarrary[n]);17
}18
Console.Read();19
}20
public static void mpsort(int [] list)//冒泡排序21
{22
int i, j, temp;23
bool exchange;24
for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序25
{26
exchange = false;27
for (j = 0; j < list.Length-i;j++ )28
{29
if(list[j]>list[j+1])30
{31
temp = list[j];32
list[j] = list[j + 1];33
list[j + 1] = temp;34
exchange = true;35
}36
}37
if (!exchange)38
{39
break;40
}41
}42
}43
public static void xzsort(int[] list)//选择排序44
{45
int i,j,temp,min;46
for (i = 0; i < list.Length; i++)47
{48
min = i;49
for (j = i + 1; j < list.Length;j++ )50
{51
if (list[min] > list[j])52
{53
min = j;54
}55
}56
temp = list[min];57
list[min] = list[i];58
list[i] = temp;59
}60
}61
public static void crsort(int[] list)//插入排序62
{ 63
int i,j,temp;64
for (i = 1; i < list.Length; i++)65
{66
temp = list[i];67
j = i;68
while((j > 0)&&(list[j-1]>temp))69
{70
list[j]=list[j-1];71
--j;72
} 73
list[j]=temp;74
}75
}76
77
}78
}79



浙公网安备 33010602011771号