排序(C#版)
1.冒泡排序
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// 冒泡排序
/// </summary>
public class BubbleSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
BubbleSort(myArray);
}
/// <summary>
/// 冒泡算法
/// </summary>
/// <param name="myArray"></param>
public static void BubbleSort(int[] myArray)
{
for (int i = 0; i < myArray.Length; i++)//循环的趟数
{
for (int j = 0; j < myArray.Length - 1 - i; j++)//每次循环的次数
{
if (myArray[j] > myArray[j + 1])
{
Swap(ref myArray[j], ref myArray[j + 1]);
}
}
}
}
/// <summary>
/// 交换
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
BubbleSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
2.鸡尾酒排序(双冒泡排序、搅拌排序或涟漪排序)
using System;
using System.Collections.Generic;
using System.Text;
public class CockTailSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
CockTailSort(myArray);
}
public static void CockTailSort(int[] myArray)
{
int low, up, index, i;
low = 0;
up = myArray.Length - 1;
index = low;
while (up > low)
{
for (i = low; i < up; i++)//从上向下扫描
{
if (myArray[i] > myArray[i + 1])
{
Swap(ref myArray[i], ref myArray[i + 1]);
index = i;
}
}
up = index;//记录最后一个交换的位置
for (i = up; i > low; i--)//从最后一个交换位置从下往上扫描
{
if (myArray[i] < myArray[i - 1])
{
Swap(ref myArray[i], ref myArray[i - 1]);
index = i;
}
}
low = index;//记录最后一个交换的位置
}
}
/// <summary>
/// 交换
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
CockTailSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
3.选择排序
using System;
using System.Collections.Generic;
using System.Text;
public class SelectSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
SelectSort(myArray);
}
/// <summary>
///
/// </summary>
/// <param name="myArray"></param>
public static void SelectSort(int[] myArray)
{
int i, j, smallest;
for (i = 0; i < myArray.Length - 1; i++)//数据起始位置,从0到倒数第二个数据
{
smallest = i;//记录最小数据的下标
for (j = i + 1; j < myArray.Length; j++)
{
if (myArray[j] < myArray[smallest])//在剩下的数据中寻找最小数据
{
smallest = j;//如果有比它更小的,记录下标
}
}
Swap(ref myArray[i], ref myArray[smallest]);//将最小数据和未排序的第一个数据交换
}
}
/// <summary>
/// 交换
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
SelectSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
4.插入排序
using System;
using System.Collections.Generic;
using System.Text;
public class InsertSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
InsertSort(myArray);
}
/// <summary>
///
/// </summary>
/// <param name="myArray"></param>
public static void InsertSort(int[] myArray)
{
int i, j, temp;
for (i = 1; i < myArray.Length; i++)
{
temp = myArray[i];//保存当前数据
j = i - 1;
//将数据插入有序表中,如果表中的数据比该数据大
//那么就依次向后移动有序表中的数据,直到找到第一个比它小的数据
//将它放在那个数据后面
while (j >= 0 && myArray[j] > temp)
{
myArray[j + 1] = myArray[j];
j--;
}
myArray[j + 1] = temp;
}
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
InsertSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
5.希尔排序
using System;
using System.Collections.Generic;
using System.Text;
public class ShellSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
ShellSort(myArray);
}
/// <summary>
///
/// </summary>
/// <param name="myArray"></param>
public static void ShellSort(int[] myArray)
{
int i, j, increment;
int temp;
for (increment = myArray.Length / 2; increment > 0; increment /= 2)
{
for (i = increment; i < myArray.Length; i++)
{
temp = myArray[i];
for (j = i; j >= increment; j -= increment)
{
if (temp < myArray[j - increment])
myArray[j] = myArray[j - increment];
else
break;
}
myArray[j] = temp;
}
}
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
ShellSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
6.快速排序
using System;
using System.Collections.Generic;
using System.Text;
public class QuickSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
QuickSort(myArray, 0, myArray.Length - 1);
}
/// <summary>
/// 快速排序算法
/// </summary>
/// <param name="myArray"></param>
public static void QuickSort(int[] myArray, int left, int right)
{
int i, j, s;
if (left < right)
{
i = left - 1;
j = right + 1;
s = myArray[(i + j) / 2];
while (true)
{
while (myArray[++i] < s) ;
while (myArray[--j] > s) ;
if (i >= j)
break;
Swap(ref myArray[i], ref myArray[j]);
}
QuickSort(myArray, left, i - 1);
QuickSort(myArray, j + 1, right);
}
}
/// <summary>
/// 交换
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
QuickSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
7.归并排序
using System;
using System.Collections.Generic;
using System.Text;
public class MergeSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
MergeSort();
}
/// <summary>
/// 利用归并的方法排序数组,首先将序列分割
/// 然后将数列归并
/// 时间是O(nlog n)
/// </summary>
/// <param name="myArray"></param>
public static void MergeSort()
{
int[] temp = new int[arraySize];
Msort(temp, 0, arraySize - 1);
}
private static void Msort(int[] temp, int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
Msort(temp, left, mid);//分割左边的序列
Msort(temp, mid + 1, right);//分割右边的序列
Merge(temp, left, mid + 1, right);//归并序列
}
}
private static void Merge(int[] temp, int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;
while ((left <= left_end) && (mid <= right))
{
if (myArray[left] <= myArray[mid])//将左端序列归并到temp数组中
{
temp[tmp_pos] = myArray[left];
tmp_pos = tmp_pos + 1;
left = left + 1;
}
else
{
temp[tmp_pos] = myArray[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}
while (left <= left_end)//复制左边剩余的数据到temp数组中
{
temp[tmp_pos] = myArray[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}
while (mid <= right)//复制右边剩余的数据到temp数组中
{
temp[tmp_pos] = myArray[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}
for (i = 0; i < num_elements; i++)
{
myArray[right] = temp[right];
right = right - 1;
}
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
MergeSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}
8.堆排序
using System;
using System.Collections.Generic;
using System.Text;
public class HeapSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
HeapSort();
}
public static void HeapSort()
{
BuildHeap();//将原始序列建成一个堆
while (arraySize > 1)
{
arraySize--;
Exchange(0, arraySize);//将最大值放在数组最后
TraversingHeap(0);//将序列从0到n-1看成一个新的序列,重新建立堆
}
}
private static void BuildHeap()
{
for (int vNode = arraySize / 2 - 1; vNode >= 0; vNode--)
{
TraversingHeap(vNode);
}
}
//利用向下遍历子节点建立队
private static void TraversingHeap(int vNode)
{
int wNode = 2 * vNode + 1;//结点wNode是节点vNode的第一个子结点
while (wNode < arraySize)
{
if (wNode + 1 < arraySize)//如果结点vNode下面有第二个子结点
if (myArray[wNode + 1] > myArray[wNode])
wNode++;//将子结点wNode设置成节点vNode下面值最大的子结点
if (myArray[vNode] >= myArray[wNode])
return;
Exchange(vNode, wNode);//如果不是,就交换节点vNode和结点wNode的值
vNode = wNode;
wNode = 2 * vNode + 1;//继续向下找子结点
}
}
private static void Exchange(int i, int j)
{
int t = myArray[i];
myArray[i] = myArray[j];
myArray[j] = t;
}
public static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 1, 6, 3, 6, 0, -5, 1, 1 };
HeapSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
}
}