C#實(shí)現所有經(jīng)典排序算法
1、選擇排序
選擇排序
class SelectionSorter
{
private int min;
public void Sort(int[] arr)
{
for (int i = 0; i < arr.Length - 1; ++i)
{
min = i;
for (int j = i + 1; j < arr.Length; ++j)
{
if (arr[j] < arr[min])
min = j;
}
int t = arr[min];
arr[min] = arr
;
arr = t;
}
}
}
2、冒泡排序
冒泡排序
class EbullitionSorter
{
public void Sort(int[] arr)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < arr.Length) && (!done))//判斷長(cháng)度
{
done = true;
for (i = 0; i < arr.Length - j; i++)
{
if (arr > arr)
{
done = false;
temp = arr;
arr = arr;//交換數據
arr = temp;
}
}
j++;
}
}
}
3、快速排序
快速排序
class QuickSorter
{
private void swap(ref int l, ref int r)
{
int temp;
temp = l;
l = r;
r = temp;
}
public void Sort(int[] list, int low, int high)
{
int pivot;//存儲分支點(diǎn)
int l, r;
int mid;
if (high <= low)
return;
else if (high == low + 1)
{
if (list[low] > list[high])
swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> 1;
pivot = list[mid];
swap(ref list[low], ref list[mid]);
l = low + 1;
r = high;
do
{
while (l <= r && list[l] < pivot)
l++;
while (list[r] >= pivot)
r--;
if (l < r)
swap(ref list[l], ref list[r]);
} while (l < r);
list[low] = list[r];
list[r] = pivot;
if (low + 1 < r)
Sort(list, low, r - 1);
if (r + 1 < high)
Sort(list, r + 1, high);
}
}
4、插入排序
插入排序
public class InsertionSorter
{
public void Sort(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
int t = arr;
int j = i;
while ((j > 0) && (arr[j - 1] > t))
{
arr[j] = arr[j - 1];//交換順序
--j;
}
arr[j] = t;
}
}
}
5、希爾排序
希爾排序
public class ShellSorter
{
public void Sort(int[] arr)
{
int inc;
for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= arr.Length; i += inc)
{
int t = arr;
int j = i;
while ((j > inc) && (arr[j - inc - 1] > t))
{
arr[j - 1] = arr[j - inc - 1];//交換數據
j -= inc;
}
arr[j - 1] = t;
}
}
}
}
6、歸并排序
歸并排序
/// <summary>
/// 歸并排序之歸:歸并排序入口
/// </summary>
/// <param name="data">無(wú)序的數組</param>
/// <returns>有序數組</returns>
/// <author>Lihua(www.zivsoft.com)</author>
int[] Sort(int[] data)
{
//取數組中間下標
int middle = data.Length / 2;
//初始化臨時(shí)數組let,right,并定義result作為最終有序數組
int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
if (data.Length % 2 != 0)//若數組元素奇數個(gè),重新初始化右臨時(shí)數組
{
right = new int[middle + 1];
}
if (data.Length <= 1)//只剩下1 or 0個(gè)元數,返回,不排序
{
return data;
}
int i = 0, j = 0;
foreach (int x in data)//開(kāi)始排序
{
if (i < middle)//填充左數組
{
left = x;
i++;
}
else//填充右數組
{
right[j] = x;
j++;
}
}
left = Sort(left);//遞歸左數組
right = Sort(right);//遞歸右數組
result = Merge(left, right);//開(kāi)始排序
//this.Write(result);//輸出排序,測試用(lihua debug)
return result;
}
/// <summary>
/// 歸并排序之并:排序在這一步
/// </summary>
/// <param name="a">左數組</param>
/// <param name="b">右數組</param>
/// <returns>合并左右數組排序后返回</returns>
int[] Merge(int[] a, int[] b)
{
//定義結果數組,用來(lái)存儲最終結果
int[] result = new int[a.Length + b.Length];
int i = 0, j = 0, k = 0;
while (i < a.Length && j < b.Length)
{
if (a < b[j])//左數組中元素小于右數組中元素
{
result[k++] = a;//將小的那個(gè)放到結果數組
}
else//左數組中元素大于右數組中元素
{
result[k++] = b[j++];//將小的那個(gè)放到結果數組
}
}
while (i < a.Length)//這里其實(shí)是還有左元素,但沒(méi)有右元素
{
result[k++] = a;
}
while (j < b.Length)//右右元素,無(wú)左元素
{
result[k++] = b[j++];
}
return result;//返回結果數組
}
注:此算法由周利華提供(http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html
)
7、基數排序
基數排序
//基數排序
public int[] RadixSort(int[] ArrayToSort, int digit)
{
//low to high digit
for (int k = 1; k <= digit; k++)
{
//temp array to store the sort result inside digit
int[] tmpArray = new int[ArrayToSort.Length];
//temp array for countingsort
int[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};
//CountingSort
for (int i = 0; i < ArrayToSort.Length; i++)
{
//split the specified digit from the element
int tmpSplitDigit = ArrayToSort/(int)Math.Pow(10,k-1) - (ArrayToSort/(int)Math.Pow(10,k))*10;
tmpCountingSortArray[tmpSplitDigit] += 1;
}
for (int m = 1; m < 10; m++)
{
tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];
}
//output the value to result
for (int n = ArrayToSort.Length - 1; n >= 0; n--)
{
int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k - 1) - (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;
tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];
tmpCountingSortArray[tmpSplitDigit] -= 1;
}
//copy the digit-inside sort result to source array
for (int p = 0; p < ArrayToSort.Length; p++)
{
ArrayToSort[p] = tmpArray[p];
}
}
return ArrayToSort;
}
8、計數排序
計數排序
//計數排序
/// <summary>
/// counting sort
/// </summary>
/// <param name="arrayA">input array</param>
/// <param name="arrange">the value arrange in input array</param>
/// <returns></returns>
public int[] CountingSort(int[] arrayA, int arrange)
{
//array to store the sorted result,
//size is the same with input array.
int[] arrayResult = new int[arrayA.Length];
//array to store the direct value in sorting process
//include index 0;
//size is arrange+1;
int[] arrayTemp = new int[arrange+1];
//clear up the temp array
for(int i = 0; i <= arrange; i++)
{
arrayTemp = 0;
}
//now temp array stores the count of value equal
for(int j = 0; j < arrayA.Length; j++)
{
arrayTemp[arrayA[j]] += 1;
}
//now temp array stores the count of value lower and equal
for(int k = 1; k <= arrange; k++)
{
arrayTemp[k] += arrayTemp[k - 1];
}
//output the value to result
for (int m = arrayA.Length-1; m >= 0; m--)
{
arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];
arrayTemp[arrayA[m]] -= 1;
}
return arrayResult;
}
9、小根堆排序
小根堆排序
/// <summary>
/// 小根堆排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
/// <returns></returns>
private void HeapSort(ref double[] dblArray)
{
for (int i = dblArray.Length - 1; i >= 0; i--)
{
if (2 * i + 1 < dblArray.Length)
{
int MinChildrenIndex = 2 * i + 1;
//比較左子樹(shù)和右子樹(shù),記錄最小值的Index
if (2 * i + 2 < dblArray.Length)
{
if (dblArray[2 * i + 1] > dblArray[2 * i + 2])
MinChildrenIndex = 2 * i + 2;
}
if (dblArray > dblArray[MinChildrenIndex])
{
ExchageValue(ref dblArray, ref dblArray[MinChildrenIndex]);
NodeSort(ref dblArray, MinChildrenIndex);
}
}
}
}
/// <summary>
/// 節點(diǎn)排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
private void NodeSort(ref double[] dblArray, int StartIndex)
{
while (2 * StartIndex + 1 < dblArray.Length)
{
int MinChildrenIndex = 2 * StartIndex + 1;
if (2 * StartIndex + 2 < dblArray.Length)
{
if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2])
{
MinChildrenIndex = 2 * StartIndex + 2;
}
}
if (dblArray[StartIndex] > dblArray[MinChildrenIndex])
{
ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);
StartIndex = MinChildrenIndex;
}
}
}
/// <summary>
/// 交換值
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
private void ExchageValue(ref double A, ref double B)
{
double Temp = A;
A = B;
B = Temp;
}
C#中的集合對象已經(jīng)都實(shí)現了各種排序方法,實(shí)際應用中根本不需要自己去編寫(xiě)這些“很底層”的代碼了。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。