数据结构的几种排序算法C#版
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
public static class Sort
{
#region 插入排序
private static void InsertSort(int[] array, int i)
{
int tmp = array[i];
int j = i;
while (j > 0 && tmp < array[j-1])
{
array[j] = array[j - 1];
j--;
}
array[j] = tmp;
}
public static void InsertSort(int[] array)
{
for (int i = 1; i < array.Length; i++)
InsertSort(array, i);
}
#endregion
#region 冒泡排序
public static void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
bool change;
BubbleSort(array, i, out change);
if (!change)
{
break;
}
}
}
private static void BubbleSort(int[] array, int i, out bool change)
{
change = false;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[i])
{
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
change = true;
}
}
}
#endregion
#region 快速排序
/// <summary>
/// 系统的快速排序算法
/// </summary>
/// <param name="array"></param>
public static void QuickSortSystem(int[] array)
{
Array.Sort(array);
}
public static void QuickSort(int[] array)
{
QuickSort(array, 0, array.Length - 1);
}
public static void QuickSort(int[] array, int left, int right)
{
if (left < right)
{
int piv = Partition(array, left, right);
QuickSort(array, left, piv - 1);
QuickSort(array, piv + 1, right);
}
}
private static int Partition(int[] array, int low, int high)
{
int piv = low; int pivalue = array[low]; //将第一个对象作为种子对象
for (int i = low + 1; i < =high; i++)
{
if (array[i] < pivalue && ++piv != i)
Swap(ref array[piv], ref array[i]);
}
Swap(ref array[low], ref array[piv]);
return piv;
}
private static void Swap(ref int a,ref int b)
{
int c = a;
a = b;
b = c;
}
#endregion
#region 希尔排序
public static void ShellSort(int[] array)
{
int gap = array.Length / 2;
while (gap != 0)
{
ShellSort(array, gap);
gap = gap / 2;
}
}
private static void ShellSort(int[] array, int gap)
{
for (int i = gap; i < array.Length; i++)
{
int tmp = array[i];
int j = i;
while (j >= gap && tmp < array[j - gap])
{
array[j] = array[j - gap];
j -= gap;
}
array[j] = tmp;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
public static class Sort
{
#region 插入排序
private static void InsertSort(int[] array, int i)
{
int tmp = array[i];
int j = i;
while (j > 0 && tmp < array[j-1])
{
array[j] = array[j - 1];
j--;
}
array[j] = tmp;
}
public static void InsertSort(int[] array)
{
for (int i = 1; i < array.Length; i++)
InsertSort(array, i);
}
#endregion
#region 冒泡排序
public static void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
bool change;
BubbleSort(array, i, out change);
if (!change)
{
break;
}
}
}
private static void BubbleSort(int[] array, int i, out bool change)
{
change = false;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[i])
{
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
change = true;
}
}
}
#endregion
#region 快速排序
/// <summary>
/// 系统的快速排序算法
/// </summary>
/// <param name="array"></param>
public static void QuickSortSystem(int[] array)
{
Array.Sort(array);
}
public static void QuickSort(int[] array)
{
QuickSort(array, 0, array.Length - 1);
}
public static void QuickSort(int[] array, int left, int right)
{
if (left < right)
{
int piv = Partition(array, left, right);
QuickSort(array, left, piv - 1);
QuickSort(array, piv + 1, right);
}
}
private static int Partition(int[] array, int low, int high)
{
int piv = low; int pivalue = array[low]; //将第一个对象作为种子对象
for (int i = low + 1; i < =high; i++)
{
if (array[i] < pivalue && ++piv != i)
Swap(ref array[piv], ref array[i]);
}
Swap(ref array[low], ref array[piv]);
return piv;
}
private static void Swap(ref int a,ref int b)
{
int c = a;
a = b;
b = c;
}
#endregion
#region 希尔排序
public static void ShellSort(int[] array)
{
int gap = array.Length / 2;
while (gap != 0)
{
ShellSort(array, gap);
gap = gap / 2;
}
}
private static void ShellSort(int[] array, int gap)
{
for (int i = gap; i < array.Length; i++)
{
int tmp = array[i];
int j = i;
while (j >= gap && tmp < array[j - gap])
{
array[j] = array[j - gap];
j -= gap;
}
array[j] = tmp;
}
}
#endregion
}
}