C#排序案例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using System; namespace 排序案例 { class Program { static void Main( string [] args) { //定义随机数列 int a, b, c, d; Random rand = new Random(); int [] intArray = new int [10]; for ( int i = 0; i < intArray.Length; i++) { a = rand.Next(1, 100); intArray[i] = a; } foreach ( int item in intArray) { Console.Write( "{0} " , item); } Console.WriteLine( " " ); //直接使用已存在的方法对数列进行升序排序 Array.Sort(intArray); foreach ( int item in intArray) { Console.Write( "{0} " , item); } Console.WriteLine( " " ); //使用冒泡排序法手动对数列进行倒序排序 for ( int a1 = 0; a1 < intArray.Length; a1++) { for ( int a2 = a1 + 1; a2 < intArray.Length; a2++) { if (intArray[a1] < intArray[a2]) { int temp = intArray[a1]; intArray[a1] = intArray[a2]; intArray[a2] = temp; } } } foreach ( int item in intArray) { Console.Write( "{0} " , item); } Console.WriteLine( " " ); //直接使用已存在的方法对数列进行反向排序(倒转数据元素) Array.Reverse(intArray); foreach ( int item in intArray) { Console.Write( "{0} " , item); } Console.WriteLine( " " ); } } } |