C#排序算法大全
冒泡排序
选择排序
插入排序
希尔排序
1
using System;
2
3
namespace BubbleSorter
4
{
5
public class BubbleSorter
6
{
7
public void Sort(int [] list)
8
{
9
int i,j,temp;
10
bool done=false;
11
j=1;
12
while((j<list.Length)&&(!done))
13
{
14
done=true;
15
for(i=0;i<list.Length-j;i++)
16
{
17
if(list[i]>list[i+1])
18
{
19
done=false;
20
temp=list[i];
21
list[i]=list[i+1];
22
list[i+1]=temp;
23
}
24
}
25
j++;
26
}
27
28
29
}
30
}
31
public class MainClass
32
{
33
public static void Main()
34
{
35
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
36
BubbleSorter sh=new BubbleSorter();
37
sh.Sort(iArrary);
38
for(int m=0;m<iArrary.Length;m++)
39
Console.Write("{0} ",iArrary[m]);
40
Console.WriteLine();
41
}
42
}
43
}
using System; 2

3
namespace BubbleSorter4
{5
public class BubbleSorter6
{7
public void Sort(int [] list)8
{9
int i,j,temp;10
bool done=false;11
j=1;12
while((j<list.Length)&&(!done))13
{14
done=true;15
for(i=0;i<list.Length-j;i++)16
{17
if(list[i]>list[i+1])18
{19
done=false;20
temp=list[i];21
list[i]=list[i+1];22
list[i+1]=temp;23
}24
}25
j++;26
}27

28

29
}30
}31
public class MainClass32
{ 33
public static void Main()34
{35
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};36
BubbleSorter sh=new BubbleSorter();37
sh.Sort(iArrary);38
for(int m=0;m<iArrary.Length;m++)39
Console.Write("{0} ",iArrary[m]); 40
Console.WriteLine();41
}42
}43
}选择排序
1
using System;
2
3
4
namespace SelectionSorter
5
{
6
public class SelectionSorter
7
{
8
private int min;
9
public void Sort(int [] list)
10
{
11
for(int i=0;i<list.Length-1;i++)
12
{
13
min=i;
14
for(int j=i+1;j<list.Length;j++)
15
{
16
if(list[j]<list[min])
17
min=j;
18
}
19
int t=list[min];
20
list[min]=list[i];
21
list[i]=t;
22
}
23
24
25
}
26
}
27
public class MainClass
28
{
29
public static void Main()
30
{
31
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
32
SelectionSorter ss=new SelectionSorter();
33
ss.Sort(iArrary);
34
for(int m=0;m<iArrary.Length;m++)
35
Console.Write("{0} ",iArrary[m]);
36
Console.WriteLine();
37
38
39
}
40
}
41
}
using System;2

3

4
namespace SelectionSorter5
{6
public class SelectionSorter7
{ 8
private int min;9
public void Sort(int [] list)10
{11
for(int i=0;i<list.Length-1;i++)12
{13
min=i;14
for(int j=i+1;j<list.Length;j++)15
{16
if(list[j]<list[min])17
min=j;18
}19
int t=list[min];20
list[min]=list[i];21
list[i]=t;22
}23

24

25
}26
}27
public class MainClass28
{ 29
public static void Main()30
{31
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};32
SelectionSorter ss=new SelectionSorter();33
ss.Sort(iArrary);34
for(int m=0;m<iArrary.Length;m++)35
Console.Write("{0} ",iArrary[m]); 36
Console.WriteLine();37

38

39
}40
}41
}插入排序
1
using System;
2
3
4
namespace InsertionSorter
5
{
6
public class InsertionSorter
7
{
8
public void Sort(int [] list)
9
{
10
for(int i=1;i<list.Length;i++)
11
{
12
int t=list[i];
13
int j=i;
14
while((j>0)&&(list[j-1]>t))
15
{
16
list[j]=list[j-1];
17
--j;
18
}
19
list[j]=t;
20
}
21
22
23
}
24
}
25
public class MainClass
26
{
27
public static void Main()
28
{
29
int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
30
InsertionSorter ii=new InsertionSorter();
31
ii.Sort(iArrary);
32
for(int m=0;m<iArrary.Length;m++)
33
Console.Write("{0}",iArrary[m]);
34
Console.WriteLine();
35
}
36
}
37
}
using System;2

3

4
namespace InsertionSorter5
{6
public class InsertionSorter7
{8
public void Sort(int [] list)9
{10
for(int i=1;i<list.Length;i++)11
{12
int t=list[i];13
int j=i;14
while((j>0)&&(list[j-1]>t))15
{16
list[j]=list[j-1];17
--j;18
}19
list[j]=t;20
}21

22

23
}24
}25
public class MainClass26
{ 27
public static void Main()28
{29
int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};30
InsertionSorter ii=new InsertionSorter();31
ii.Sort(iArrary);32
for(int m=0;m<iArrary.Length;m++)33
Console.Write("{0}",iArrary[m]); 34
Console.WriteLine();35
}36
}37
}希尔排序
1
using System;
2
3
4
namespace ShellSorter
5
{
6
public class ShellSorter
7
{
8
public void Sort(int [] list)
9
{
10
int inc;
11
for(inc=1;inc<=list.Length/9;inc=3*inc+1);
12
for(;inc>0;inc/=3)
13
{
14
for(int i=inc+1;i<=list.Length;i+=inc)
15
{
16
int t=list[i-1];
17
int j=i;
18
while((j>inc)&&(list[j-inc-1]>t))
19
{
20
list[j-1]=list[j-inc-1];
21
j-=inc;
22
}
23
list[j-1]=t;
24
}
25
}
26
}
27
}
28
public class MainClass
29
{
30
public static void Main()
31
{
32
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
33
ShellSorter sh=new ShellSorter();
34
sh.Sort(iArrary);
35
for(int m=0;m<iArrary.Length;m++)
36
Console.Write("{0} ",iArrary[m]);
37
Console.WriteLine();
38
}
39
}
40
}
using System; 2

3

4
namespace ShellSorter5
{6
public class ShellSorter7
{8
public void Sort(int [] list)9
{10
int inc;11
for(inc=1;inc<=list.Length/9;inc=3*inc+1);12
for(;inc>0;inc/=3)13
{14
for(int i=inc+1;i<=list.Length;i+=inc)15
{16
int t=list[i-1];17
int j=i;18
while((j>inc)&&(list[j-inc-1]>t))19
{20
list[j-1]=list[j-inc-1];21
j-=inc;22
}23
list[j-1]=t;24
}25
}26
}27
}28
public class MainClass29
{ 30
public static void Main()31
{32
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};33
ShellSorter sh=new ShellSorter();34
sh.Sort(iArrary);35
for(int m=0;m<iArrary.Length;m++)36
Console.Write("{0} ",iArrary[m]); 37
Console.WriteLine();38
}39
}40
}




浙公网安备 33010602011771号