这里写自定义目录标题
冒泡排序实现:
/// <summary>
/// 冒泡排序(时间复杂度为n²)
/// </summary>
/// <param name="array">要进行排序的数组</param>
void MaoPao(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
归并排序实现方法:
/// <summary>
/// 归并排序(将一个数组平均等分,直至分到一个数组中只有一个元素,让两个数组从第一个元素开始比较,小的数放到新数组之中(时间复杂度n*logn))
/// </summary>
/// <param name="array">要进行排序的数组</param>
/// <param name="left">数组的最小索引</param>
/// <param name="right">数组的长度</param>
void Guibing(int[] array, int left, int right)
{
if (right > 1)
{
int leg = (left + right) / 2;
int[] leftli = new int[leg];
int[] rightli = new int[right - leg];
for (int i = 0; i < leftli.Length; i++)
{
leftli[i] = array[i];
}
for (int i = 0; i < rightli.Length; i++)
{
rightli[i] = array[leg + i];
}
Guibing(leftli, 0, leftli.Length);
GuiBing(rightli, 0, rightli.Length);
int l = 0;
int r = 0;
int z = 0;
while (l < leftli.Length && r < rightli.Length)
{
if (leftli[l] <= rightli[r])
{
array[z++] = leftli[l++];
}
else
{
array[z++] = rightli[r++];
}
}
while (l >= leftli.Length && r < rightli.Length)
{
array[z++] = rightli[r++];
}
while (r >= rightli.Length && l < leftli.Length)
{
array[z++] = leftli[l++];
}
}
}
快速排序实现方法:
/// <summary>
/// 快速排序(n*logn)
/// </summary>
/// <param name="array">要进行排序的数组</param>
/// <param name="left">数组的最小(最左边)索引</param>
/// <param name="right">数组的最大(最右边)索引</param>
public void KuaiSu(int[] array, int left, int right)
{
if (left > right)
{
return;
}
int pivot = (left + right) / 2;
int temp = array[pivot];
array[pivot] = array[right];
array[right] = temp;
int l = left;
int r = right;
int tempPivot = array[r];
while (l != r)
{
Console.WriteLine(11);
while (array[l] <= tempPivot && l < r)
{
l++;
}
if (l < r)
{
array[r] = array[l];
r--;
}
while (array[r] >= tempPivot && r > l)
{
r--;
}
if (l < r)
{
array[l] = array[r];
l++;
}
}
array[l] = tempPivot;
pivot = l;
KuaiSu(array, left, pivot - 1);
KuaiSu(array, pivot + 1, right);
}
希尔排序实现方法:
/// <summary>
/// 希尔排序(按照间隔每两个元素进行比较小的前移)(时间复杂度n的四分之五次方~n的二分之三次方)
/// </summary>
/// <param name="array">要进行排序的数组</param>
void XiEr(int[] array)
{
int lenght = array.Length;
for (int d = lenght / 2; d > 0; d /= 2)
{
for (int i = 0; i < d; i++)
{
for (int j = i; j < lenght; j += d)
{
for (int k = j; k >= d; k -= d)
{
if (array[k] < array[k - d])
{
int temp;
temp = array[k];
array[k] = array[k - d];
array[k - d] = temp;
}
}
}
}
}
}
直接插入排序实现方法:
/// <summary>
/// 直接插入排序(temp接收第二个值开始遍历,使数组中每一个temp前面的值都与temp比较,如果比他大就往后移动,temp继续比较)时间复杂度为n²
///
/// </summary>
/// <param name="array">要进行排序的数组</param>
void ZhiJieChaRu(int[] array)
{
int temp;
int j;
for (int k = 1; k < array.Length; k++)
{
temp = array[k];
j = k - 1;
while (j >= 0 && temp < array[j])
{
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = temp;
}
}
直接选择排序实现方法:
/// <summary>
/// 直接选择排序(找到数组中最小的数移动到最前面)
/// </summary>
/// <param name="array">要进行排序的数组</param>
void ZhiJieXuanZe(int[] array)
{
int i, j, Small;
for (i = 0; i < array.Length - 1; i++)
{
Small = i;
for (j = i + 1; j < array.Length; j++)
{
if (array[j] < array[Small])
{
Small = j;
}
}
int temp;
temp = array[i];
array[i] = array[Small];
array[Small] = temp;
}
}
折半查找实现方法(折半查找需要在有序数组中实现):
public int BinarySearch(int[] arr, int key, int n)
{
int low = 0;
int high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == key)
{
return mid;
}
if (arr[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}