C#实现所有经典排序算法汇总
2024-10-01 04:25:31
C#实现所有经典排序算法
1、选择排序
class SelectionSorter
{
private int min;
public void Sort(int[] arr)
{
for (int i = ; i < arr.Length - ; ++i)
{
min = i;
for (int j = i + ; 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 = ;
while ((j < arr.Length) && (!done))//判断长度
{
done = true;
for (i = ; i < arr.Length - j; i++)
{
if (arr[i] > arr[i + ])
{
done = false;
temp = arr[i];
arr[i] = arr[i + ];//交换数据
arr[i + ] = 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 + )
{
if (list[low] > list[high])
swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> ;
pivot = list[mid];
swap(ref list[low], ref list[mid]);
l = low + ;
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 + < r)
Sort(list, low, r - );
if (r + < high)
Sort(list, r + , high);
}
}
4、插入排序
public class InsertionSorter
{
public void Sort(int[] arr)
{
for (int i = ; i < arr.Length; i++)
{
int t = arr[i];
int j = i;
while ((j > ) && (arr[j - ] > t))
{
arr[j] = arr[j - ];//交换顺序
--j;
}
arr[j] = t;
}
}
}
5、希尔排序
public class ShellSorter
{
public void Sort(int[] arr)
{
int inc;
for (inc = ; inc <= arr.Length / ; inc = * inc + ) ;
for (; inc > ; inc /= )
{
for (int i = inc + ; i <= arr.Length; i += inc)
{
int t = arr[i - ];
int j = i;
while ((j > inc) && (arr[j - inc - ] > t))
{
arr[j - ] = arr[j - inc - ];//交换数据
j -= inc;
}
arr[j - ] = t;
}
}
}
}
6、归并排序
/// <summary>
/// 归并排序之归:归并排序入口
/// </summary>
/// <param name="data">无序的数组</param>
/// <returns>有序数组</returns>
/// <author>Lihua(www.zivsoft.com)</author>
int[] Sort(int[] data)
{
//取数组中间下标
int middle = data.Length / ;
//初始化临时数组let,right,并定义result作为最终有序数组
int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
if (data.Length % != )//若数组元素奇数个,重新初始化右临时数组
{
right = new int[middle + ];
}
if (data.Length <= )//只剩下1 or 0个元数,返回,不排序
{
return data;
}
int i = , j = ;
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 = , j = , k = ;
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 = ; 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[]{,,,,,,,,,};
//CountingSort
for (int i = ; i < ArrayToSort.Length; i++)
{
//split the specified digit from the element
int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(,k-) - (ArrayToSort[i]/(int)Math.Pow(,k))*;
tmpCountingSortArray[tmpSplitDigit] += ;
}
for (int m = ; m < ; m++)
{
tmpCountingSortArray[m] += tmpCountingSortArray[m - ];
}
//output the value to result
for (int n = ArrayToSort.Length - ; n >= ; n--)
{
int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(,k - ) - (ArrayToSort[n]/(int)Math.Pow(,k)) * ;
tmpArray[tmpCountingSortArray[tmpSplitDigit]-] = ArrayToSort[n];
tmpCountingSortArray[tmpSplitDigit] -= ;
}
//copy the digit-inside sort result to source array
for (int p = ; 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+];
//clear up the temp array
for(int i = ; i <= arrange; i++)
{
arrayTemp[i] = ;
}
//now temp array stores the count of value equal
for(int j = ; j < arrayA.Length; j++)
{
arrayTemp[arrayA[j]] += ;
}
//now temp array stores the count of value lower and equal
for(int k = ; k <= arrange; k++)
{
arrayTemp[k] += arrayTemp[k - ];
}
//output the value to result
for (int m = arrayA.Length-; m >= ; m--)
{
arrayResult[arrayTemp[arrayA[m]] - ] = arrayA[m];
arrayTemp[arrayA[m]] -= ;
}
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 - ; i >= ; i--)
{
if ( * i + < dblArray.Length)
{
int MinChildrenIndex = * i + ;
//比较左子树和右子树,记录最小值的Index
if ( * i + < dblArray.Length)
{
if (dblArray[ * i + ] > dblArray[ * i + ])
MinChildrenIndex = * i + ;
}
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 ( * StartIndex + < dblArray.Length)
{
int MinChildrenIndex = * StartIndex + ;
if ( * StartIndex + < dblArray.Length)
{
if (dblArray[ * StartIndex + ] > dblArray[ * StartIndex + ])
{
MinChildrenIndex = * StartIndex + ;
}
}
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;
}
最新文章
- 树莓派3B初始化后一些必须的设置
- 机器学习——利用K-均值聚类算法对未标注数据分组
- MSP430FR4133/4131/4132单片机破解芯片解密多少钱?
- wpf直接绑定xml生成应用程序
- js正则表达式学习
- Centos7.X 源码编译安装subversion svn1.8.x
- JS魔法堂:IE5~9的Drag&;Drop API
- Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses
- 如何编写Linux设备驱动程序
- javascript RegExp类型 学习小记
- ios使用openUrl进行应用跳转
- VS2010类模板修改——添加版权、说明
- 1.3. 创建 Grocery Dude 项目(Core Data 应用程序实践指南)
- Linux下添加源的几种方法
- 使用dd备份和恢复ASM中的数据文件头
- [LightOJ1038] Race to 1 Again
- MVC源码分析 - Action查找和过滤器的执行时机
- PHP: Short URL Algorithm Implementation
- js 处理URL实用技巧
- codevs 3083 二叉树
热门文章
- [Swift通天遁地]五、高级扩展-(8)ImageView(图像视图)的各种扩展方法
- [Swift通天遁地]五、高级扩展-(11)图像加载Loading动画效果的自定义和缓存
- Java 删除List元素的正确方式
- Python/Django 下载Excel2003
- linux,apache,mysql,php常用查看版本信息的方法
- mysql中返回当前时间的函数或者常量
- 学习随笔-Java WebService
- VHDL之Serial-Parallel Multiplier
- Spring学习_day03_事务
- (转)Java任务调度框架Quartz入门教程指南(四)Quartz任务调度框架之触发器精讲SimpleTrigger和CronTrigger、最详细的Cron表达式范例