关于排序的一些常识可以问百科,以下是用C#实现的排序算法。
1.冒泡排序 BubbleSort
/// <summary>
/// Bubble sort O(n^2)
/// </summary>
/// <param name="arr"></param>
public static void BubbleSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = arr.Length - 1; j > i; j--)
{
if (arr[j - 1] > arr[j])
{
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
/// Bubble sort O(n^2)
/// </summary>
/// <param name="arr"></param>
public static void BubbleSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = arr.Length - 1; j > i; j--)
{
if (arr[j - 1] > arr[j])
{
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
2.交换排序 ExchangeSort
/// <summary>
/// Exchange sort o(n^2)
/// </summary>
/// <param name="arr"></param>
public static void ExchangeSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/// Exchange sort o(n^2)
/// </summary>
/// <param name="arr"></param>
public static void ExchangeSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
3.选择排序 SelectSort
public static void SelectSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < arr.Length; i++)
{
int pos = i;
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[pos] > arr[j])
{
pos = j;
}
}
if (pos != i)
{
int temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
}
}
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 0; i < arr.Length; i++)
{
int pos = i;
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[pos] > arr[j])
{
pos = j;
}
}
if (pos != i)
{
int temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
}
}
4. 插入排序 InsertSort
public static void InsertSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 1; i < arr.Length; i++)
{
int temp = arr[i];
int j = i;
while (j > 0 && arr[j - 1] > temp)
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}
{
if (arr == null)
{
throw new ArgumentNullException();
}
for (int i = 1; i < arr.Length; i++)
{
int temp = arr[i];
int j = i;
while (j > 0 && arr[j - 1] > temp)
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}
5.快速排序 QuickSort
public static void QuickSort(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
if (arr.Length > 1)
{
QuickSortRecursive(arr, 0, arr.Length - 1);
}
}
private static void QuickSortRecursive(int[] arr, int left, int right)
{
//Console.WriteLine("New Recursive ...Left={0},Right={1}", left, right);
int key = arr[left];
int i = left;
int j = right;
do
{
while (i < right && arr[i] < key)
{
i++;
}
while (j > 0 && arr[j] > key)
{
j--;
}
if (i < j)
{
if (arr[i] == arr[j])
{
i++;
continue;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//Console.WriteLine("Left={0},Right={1},i={2},j={3},Key={4}", left, right, i, j, key);
//ShowResult(arr);
}
} while (i < j);
if (i - 1 > left)
{
QuickSortRecursive(arr, left, i - 1);
}
if (j + 1 < right)
{
QuickSortRecursive(arr, j + 1, right);
}
}
{
if (arr == null)
{
throw new ArgumentNullException();
}
if (arr.Length > 1)
{
QuickSortRecursive(arr, 0, arr.Length - 1);
}
}
private static void QuickSortRecursive(int[] arr, int left, int right)
{
//Console.WriteLine("New Recursive ...Left={0},Right={1}", left, right);
int key = arr[left];
int i = left;
int j = right;
do
{
while (i < right && arr[i] < key)
{
i++;
}
while (j > 0 && arr[j] > key)
{
j--;
}
if (i < j)
{
if (arr[i] == arr[j])
{
i++;
continue;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//Console.WriteLine("Left={0},Right={1},i={2},j={3},Key={4}", left, right, i, j, key);
//ShowResult(arr);
}
} while (i < j);
if (i - 1 > left)
{
QuickSortRecursive(arr, left, i - 1);
}
if (j + 1 < right)
{
QuickSortRecursive(arr, j + 1, right);
}
}
public static void QuickSortByStack(int[] arr)
{
if (arr == null)
{
throw new ArgumentNullException();
}
if (arr.Length > 1)
{
Stack<int[]> limitStack = new Stack<int[]>();
limitStack.Push(new int[] { 0, arr.Length - 1 });
while (limitStack.Count > 0)
{
int[] limits = limitStack.Pop();
int left = limits[0];
int right = limits[1];
int key = arr[left];
int i = left;
int j = right;
do
{
while (i < right && arr[i] < key)
{
i++;
}
while (j > 0 && arr[j] > key)
{
j--;
}
if (i < j)
{
if (arr[i] == arr[j])
{
i++;
continue;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//Console.WriteLine("Left={0},Right={1},i={2},j={3},Key={4}", left, right, i, j, key);
//ShowResult(arr);
}
} while (i < j);
if (i - 1 > left)
{
limitStack.Push(new int[] { left, i - 1 });
}
if (j + 1 < right)
{
limitStack.Push(new int[] { j + 1, right });
}
}
}
}
{
if (arr == null)
{
throw new ArgumentNullException();
}
if (arr.Length > 1)
{
Stack<int[]> limitStack = new Stack<int[]>();
limitStack.Push(new int[] { 0, arr.Length - 1 });
while (limitStack.Count > 0)
{
int[] limits = limitStack.Pop();
int left = limits[0];
int right = limits[1];
int key = arr[left];
int i = left;
int j = right;
do
{
while (i < right && arr[i] < key)
{
i++;
}
while (j > 0 && arr[j] > key)
{
j--;
}
if (i < j)
{
if (arr[i] == arr[j])
{
i++;
continue;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
//Console.WriteLine("Left={0},Right={1},i={2},j={3},Key={4}", left, right, i, j, key);
//ShowResult(arr);
}
} while (i < j);
if (i - 1 > left)
{
limitStack.Push(new int[] { left, i - 1 });
}
if (j + 1 < right)
{
limitStack.Push(new int[] { j + 1, right });
}
}
}
}
快排这里有两种实现,即递归和栈
按一般情况,递归的速度应该比非递归的慢,但这里却相反。后者还略慢些。 这里还没有研究明白。求验证。