-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.cs
46 lines (41 loc) · 1.17 KB
/
BubbleSort.cs
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
using System;
using System.Collections.Generic;
namespace sort
{
/// <summary>
/// 冒泡排序
/// </summary>
/// <remarks>
/// 两两比较,前一个更大时交换位置,第一轮后最大的在最后;
/// 再开启下一轮;
///
/// 时间复杂度 平均:O(n²),最好:O(n),最坏:O(n²)
/// 稳定
/// </remarks>
public class BubbleSort : ISort
{
public void Sort(int[] src)
{
var arr = src.Clone() as int[];
var swaped = true;
var count = 0;
for (int i = 0; i < arr.Length - 1 && swaped; i++)
{
swaped = false;
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
var t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
swaped = true;
}
count++;
}
}
Console.WriteLine("冒泡排序:");
Console.WriteLine(string.Join(" ", arr));
}
}
}