C#所有经典排序算法汇总
程序员文章站
2022-06-28 13:22:02
1、选择排序 选择排序 class SelectionSorter { private int min; public void Sort(int[] arr) { for (int i = 0; i arr[i + 1]) { ... ......
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[i];
arr[i] = t;
}
}
}
2、冒泡排序
冒泡排序
class ebullitionsorter
{
public void sort(int[] arr)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < arr.length) && (!done))//判断长度
{
done = true;
for (i = 0; i < arr.length - j; i++)
{
if (arr[i] > arr[i + 1])
{
done = false;
temp = arr[i];
arr[i] = arr[i + 1];//交换数据
arr[i + 1] = 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;//存储分支点
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[i];
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[i - 1];
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">无序的数组</param>
/// <returns>有序数组</returns>
/// <author>lihua(www.zivsoft.com)</author>
int[] sort(int[] data)
{
//取数组中间下标
int middle = data.length / 2;
//初始化临时数组let,right,并定义result作为最终有序数组
int[] left = new int[middle], right = new int[middle], result = new int[data.length];
if (data.length % 2 != 0)//若数组元素奇数个,重新初始化右临时数组
{
right = new int[middle + 1];
}
if (data.length <= 1)//只剩下1 or 0个元数,返回,不排序
{
return data;
}
int i = 0, j = 0;
foreach (int x in data)//开始排序
{
if (i < middle)//填充左数组
{
left[i] = x;
i++;
}
else//填充右数组
{
right[j] = x;
j++;
}
}
left = sort(left);//递归左数组
right = sort(right);//递归右数组
result = merge(left, right);//开始排序
//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)
{
//定义结果数组,用来存储最终结果
int[] result = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
{
if (a[i] < b[j])//左数组中元素小于右数组中元素
{
result[k++] = a[i++];//将小的那个放到结果数组
}
else//左数组中元素大于右数组中元素
{
result[k++] = b[j++];//将小的那个放到结果数组
}
}
while (i < a.length)//这里其实是还有左元素,但没有右元素
{
result[k++] = a[i++];
}
while (j < b.length)//右右元素,无左元素
{
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[i]/(int)math.pow(10,k-1) - (arraytosort[i]/(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[i] = 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;
//比较左子树和右子树,记录最小值的index
if (2 * i + 2 < dblarray.length)
{
if (dblarray[2 * i + 1] > dblarray[2 * i + 2])
minchildrenindex = 2 * i + 2;
}
if (dblarray[i] > dblarray[minchildrenindex])
{
exchagevalue(ref dblarray[i], ref dblarray[minchildrenindex]);
nodesort(ref dblarray, minchildrenindex);
}
}
}
}
/// <summary>
/// 节点排序
/// </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;
}
上一篇: Oracle数据库基础(二)