Soon graduated, review the C # data structure of the sorting algorithm, which mainly has bubble sort, direct insertion sort, simple choice of sorting and quick sort, in which reference to the old Zhao's Codetimer and eaglet performance counters, hereby thank you ~ ~
OK, let's start with our sorting algorithm.
Before sorting algorithm, we first define a 100-bit random sequence to perform the performance test of various sorting algorithms.
The code is as follows:
/// <summary>
/// 随机生成100位的数组
/// </summary>
/// <returns>返回生成数组</returns>
public static int[] RandomArray()
{
Random ran = new Random();
int[] arr = new int[100];
int tem;
for (int i = 0; i < 100; i++)
{
tem = ran.Next(1, 100);
arr[i] = tem;
}
return arr;
}
1. Bubble sort (Bubble sort)
Basic idea: Compare the key codes of the adjacent records, if the key code in the preceding record is greater than the key code of the following record, swap them, otherwise it will not be exchanged.
The code is as follows:
/// <summary>
/// 冒泡排序算法
/// </summary>
public class BubbleSort : IAction
{
#region IAction 成员
public void Action()
{
int[] array = Program.RandomArray();
for (int a = 0; a < array.Length; a++)
{
int item = 0;
for (int b = array.Length - 1; b > a; b--)
{
if (array[b] < array[b - 1])
{
item = array[b];
array[b] = array[b - 1];
array[b - 1] = item;
}
}
}
}
#endregion
}