以下是常见的十大排序算法(按照学习和实现的顺序排列):
这些排序算法具有不同的时间复杂度、空间复杂度和稳定性,适用于不同的排序场景。每种算法都有其独特的思想和实现方式,您可以根据具体的需求选择适合的排序算法。
C#实现的十大排序算法的示例代码如下:
1、冒泡排序(Bubble Sort):
class BubbleSort
{
public static void Sort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n – 1; i++)
{
for (int j = 0; j < n – i – 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
2、选择排序(Selection Sort):
class SelectionSort
{
public static void Sort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n – 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
3、插入排序(Insertion Sort):
class InsertionSort
{
public static void Sort(int[] arr)
{
int n = arr.Length;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i – 1;