冒泡排序
冒泡排序,简单好理解代码也比较容易实现。思想是这样的,k1,k2,k3,...ki....kn 算法是搜索队列中的最小值然后通过交换把整个数组中的最小的元素放到第一个位置,然后从第二个位置反复这个过程,以此类推。算法的时间复杂度为N^2.
[C#]
[C#]
1
class Program
2
{
3
static void Main(string[] args)
4
{
5
int[] a = { 7, 2, 1, 6, 21, 13, 8, 4, 33, 26 };
6
PrintArray(a);
7
BubbleSort(a);
8
PrintArray(a);
9
10
}
11
12
private static void BubbleSort(int[] array)
13
{
14
for (int i = 0; i < array.Length; i++)
15
{
16
for (int j = i + 1; j < array.Length; j++)
17
{
18
if (array[i] > array[j])
19
{
20
int temp = array[i];
21
array[i] = array[j];
22
array[j] = temp;
23
}
24
}
25
}
26
}
27
28
private static void PrintArray(int[] array)
29
{
30
string result = string.Empty;
31
for (int i = 0; i < array.Length; i++)
32
{
33
result += array[i].ToString() + " ";
34
}
35
Console.WriteLine(result.Trim());
36
}
37
}

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
