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();
}
C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序的更多相关文章
- java排序算法(八):希尔排序(shell排序)
java排序算法(八):希尔排序(shell排序) 希尔排序(缩小增量法)属于插入类排序,由shell提出,希尔排序对直接插入排序进行了简单的改进,它通过加大插入排序中元素之间的间隔,并在这些有间隔的 ...
- javascript数据结构与算法--高级排序算法(快速排序法,希尔排序法)
javascript数据结构与算法--高级排序算法(快速排序法,希尔排序法) 一.快速排序算法 /* * 这个函数首先检查数组的长度是否为0.如果是,那么这个数组就不需要任何排序,函数直接返回. * ...
- 几种排序方式的java实现(02:希尔排序,归并排序,堆排序)
代码(部分为别人代码): 1.希尔排序(ShellSort) /* * 希尔排序:先取一个小于n的整数d1作为第一个增量, * 把文件的全部记录分成(n除以d1)个组.所有距离为d1的倍数的记录放在同 ...
- [Swift]八大排序算法(六):希尔排序
排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...
- 【算法】【排序】【插入类】希尔排序 ShellSort
#include<stdio.h> #include <time.h> #include<stdlib.h> int main(){ ]; //设立随机数 sran ...
- 9, java数据结构和算法: 直接插入排序, 希尔排序, 简单选择排序, 堆排序, 冒泡排序,快速排序, 归并排序, 基数排序的分析和代码实现
内部排序: 就是使用内存空间来排序 外部排序: 就是数据量很大,需要借助外部存储(文件)来排序. 直接上代码: package com.lvcai; public class Sort { publi ...
- 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)
本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...
- 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较
2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...
- JS快速排序 希尔排序 归并排序 选择排序
/* 快速排序 1.1 算法描述 快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常被采用,再加上快速排序思想----分治法也确实实用.快速排序是一种既不浪费空间又可以快一 ...
随机推荐
- django 笔记16 文件上传笔记
views.py文件 def upload_file(request): username = request.POST.get('username') fafafa = request.FILES. ...
- [洛谷P1169] [ZJOI2007] 棋盘制作 解题报告(悬线法+最大正方形)
题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个 8×8 大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我 ...
- hive安装用mysql作为元数据库,mysql的设置
mysql的设置 在要作为元数据库的mysql服务器上建立hive数据库: #建立数据库 create database if not exists hive; #设置远程登录的权限 GRANT AL ...
- 1.CMD命令
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr. ...
- mysql读写分离的解决方案
来源于网上整理 http://yanwt.iteye.com/blog/1460780 现有三种解决方式实现mysql读写分离 1 程序修改mysql操作类 优点:直接和数据库通信,简单快捷的读写分离 ...
- PostgreSQL Replication之第九章 与pgpool一起工作(2)
9.2 理解pgpool的功能 pgpool提供了如下功能: •连接池 •语句级别的复制 •负载均衡 •限制连接 •内存缓存 •并行查询 [当决定使用那些功能的时候,记住并非所有的功能可以在同一时间使 ...
- php八大设计模式之职责链模式
当发生一种事情时,我们需要要对应职责的事物去处理对应的事情. 或者去找最近的类(就是级别最低的)去解决,如果解决不了就顺着往上找职责更高的,直到解决为止. 注意:一定是要有一个职责最高的类,否则会有问 ...
- 苹果下一代iPhone曝光
3月23日消息,据9TO5Mac报道,苹果下一代iPhone(以下暂命名为iPhone 11)可能会提供像三星Galaxy S10的PowerShare功能,能为Apple Watch和AirPods ...
- poj2031-Building a Space Station(最小生成树,kruskal,prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5874 Accepte ...
- 关于es6中对象的扩展
1.Object.is() es5比较两个值是否相等,只有两个运算符,相等(==) 和 严格相等(===),他们都有缺点,前者会自动转换数据类型,后者的NaN不等于自身,以及+0 等于 -0.es6提 ...