C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序
{
//插入排序
public void insertSort(int[] array)
{
int temp = 0;
int index = 0;
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (array[i] < array[j])//从j到i之间的数总体右移动。将i数据放到j位置
{
index = i;
temp = array[i];
while (index > j)
{
array[index] = array[index - 1];
index--;
}
array[j] = temp;
break;
}
}
}
printArray(array, "插入排序:");
}
public void printArray(int[] array, String type)
{
Console.WriteLine(type);
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + ",");
}
Console.WriteLine();
}
//冒泡排序
public void bubbleSort(int[] array)
{
int temp = 0;
bool exchanged = true;
for (int i = 0; i < array.Length; i++)
{
if (!exchanged)
break;
for (int j = array.Length - 1; j > i; j--)
{
exchanged = false;
if (array[j] < array[j - 1])//后面的数比前面的小就交换
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
exchanged = true;
}
}
}
printArray(array, "冒泡排序:");
}
//选择排序:从前到后依次选择最小的放在最前面,第二小的放在第二个位置
public void selectionSort(int[] array)
{
int minIndex = 0;
int temp = 0;
for (int i = 0; i < array.Length; i++)
{
minIndex = i;
for (int j = i; j < array.Length; j++)
{
if (array[j] < array[minIndex])
minIndex = j;
}
//将i到j之间最小的数放到位置i
temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
printArray(array, "选择排序:");
}
//高速排序
public void quickSort(int[] array)
{
quicksort1(array, 0, array.Length - 1);
printArray(array, "高速排序:");
}
public void quicksort1(int[] array, int start, int end)
{
if (start >= end)
return;
int i = start, j = end;
int k = array[i];
while (i < j)
{
while (array[j] >= k && i < j)
j--;
array[i] = array[j];
while (array[i] < k && i < j)
i++;
array[j] = array[i];
}
array[i] = k;
quicksort1(array, start, i -1);
quicksort1(array, i +1, end);
}
//堆排序
public void stackSort(int[] array)
{
MyHeap h = new MyHeap();
h.buildHeap(array);
h.HeapSort();
h.printHeap();
}
//归并排序
public void mergeSort(int[] array)
{
mergeSort1(array, 0, array.Length - 1);
printArray(array, "归并:");
}
private void mergeSort1(int[] array ,int start,int end)
{
if (start >= end)
return;
mergeSort1(array, start, (start+end) / 2);
mergeSort1(array, (start + end) / 2+1,end);
merge(array, start, (start + end) / 2, end);
}
private void merge(int[] array,int start,int mid,int end)
{
Queue<int> q = new Queue<int>();
int i = start, j = mid + 1;
while (i <=mid && j <=end)
{
if (array[i] < array[j])
{
q.Enqueue(array[i]);
i++;
}
else
{
q.Enqueue(array[j]);
j++;
}
}
while (i <= mid)
q.Enqueue(array[i++]);
while (j <= end)
q.Enqueue(array[j++]);
for (i = start; i <= end; i++)
array[i] = q.Dequeue();
}
//基数排序
public void radixSort(int[] array)
{
int maxlength=0;//数据最大位数
//申请空间用于存放数据
List<List<int>> lists=new List<List<int>>();
//申请10个桶,用于存放0-9
for (int i = 0; i < 10; i++)
lists.Add(new List<int>());
//获取数据的最大位数
for (int i = 0; i < array.Length; i++)
maxlength = maxlength < array[i].ToString().Length ?
array[i].ToString().Length : maxlength;
for (int i = 0; i < maxlength; i++)
{
//数据入桶
for (int j = 0; j < array.Length; j++)
{
lists[array[j] / (int)(Math.Pow(10, i)) - array[j] / (int)(Math.Pow(10, i+1))*10].Add(array[j]);
}
int t = 0;
//将桶里面的数据又一次放入数组
for (int k = 0; k < 10; k++)
{
foreach (int item in lists[k])
array[t++] = item;
}
//清空桶里面的数据
for (int k = 0; k < 10; k++)
{
lists[k].Clear();
}
}
printArray(array, "基数排序");
}
//希尔排序
public void shellSort(int[] array)
{
int step = array.Length / 2;
while (step > 0)
{
shellInsert(array, step);
Console.WriteLine();
printArray(array, "希尔");
step = step / 2;
}
printArray(array, "希尔");
}
private void shellInsert(int[] array,int step)
{
int temp = 0;
int index = 0;
for (int i = 0; i < array.Length; i=i+step)
{
for (int j = 0; j < i; j=j+step)
{
if (array[i] < array[j])//从j到i之间的数总体右移动,将i数据放到j位置
{
index = i;
temp = array[i];
while (index > j)
{
array[index] = array[index - 1];
index--;
}
array[j] = temp;
break;
}
}
}
}
}
{
int[] array = { 12,3,23,4,21,44,2,3,11};
Console.WriteLine("待排序数组:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]+",");
}
Console.WriteLine();
// insertSort(array);
// bubbleSort(array);
// selectionSort(array);
// (new Sortings()).quickSort(array);
// (new Sortings()).stackSort(array);
// (new Sortings()).mergeSort(array);
//(new Sortings()).shellSort(array);
(new Sortings()).radixSort(array);
Console.Read();
}
最新文章
- storm 源码笔记
- 用java代码把docx转换成pdf文件
- C# 操作鼠标移动到指定的屏幕位置方法
- (1)创建一个叫做People的类: 属性:姓名、年龄、性别、身高 行为:说话、计算加法、改名 编写能为所有属性赋值的构造方法; (2)创建主类: 创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80; 让该对象调用成员方法: 说出“你好!” 计算23+45的值 将名字改为“李四”
- JavaScript中“javascript:void(0) ”是什么意思
- Shell练习 验证号码
- core文件分析
- 已知整数m,n,p,q适合(m-p)|(mn+pq)证明:(m-p)|(mq+np)(整除理论1.1.5)
- FFmpeg源码简单分析:libswscale的sws_scale()
- 关于如何通过kali linux 攻击以及破解WPA/WPA2无线加密
- linux alias 命令 查看系统设置的命令别名
- django进阶篇
- 理解Lambda表达式和闭包
- [UE4]认识CanvasPanelSlot,Construct Object From Class动态创建UI控件
- 设置多台机器linux服务器ssh相互无密码访问
- ScreenOper
- 游戏服务器框架:Leaf/go
- luasql在Fedora20下的安装与使用示例
- Java获取Resource目录下的文件
- spring深入内容