经典排序算法 – 插入排序Insertion sort
来源:互联网 发布:康得新裸眼3d知乎 编辑:程序博客网 时间:2024/05/16 23:33
经典排序算法 – 插入排序Insertion sort
插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。
插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内容中进行。
图1演示了对4个元素进行直接插入排序的过程,共需要(a),(b),(c)三次插入。
以下代码仅供参考,欢迎指正
/// <summary> /// 插入排序 /// </summary> /// <param name="unsorted"></param> static void insertion_sort(int[] unsorted) { for (int i = 1; i < unsorted.Length; i++) { if (unsorted[i - 1] > unsorted[i]) { int temp = unsorted[i]; int j = i; while (j > 0 && unsorted[j - 1] > temp) { unsorted[j] = unsorted[j - 1]; j--; } unsorted[j] = temp; } } } static void Main(string[] args) { int[] x = { 6, 2, 4, 1, 5, 9 }; insertion_sort(x); foreach (var item in x) { if (item > 0) Console.WriteLine(item + ","); } Console.ReadLine(); }
转自:http://www.cnblogs.com/kkun/archive/2011/11/23/2260265.html
0 0
- 经典排序算法:插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 经典排序算法 – 插入排序Insertion sort
- 插入排序算法Insertion-Sort
- 算法排序--插入排序(insertion sort)
- 排序算法---插入排序(Insertion Sort)
- 基础排序算法 – 插入排序Insertion sort
- 插入排序(insertion sort)
- insertion sort 插入排序
- 插入排序(Insertion Sort)
- 插入排序 Insertion Sort
- 插入排序Insertion sort
- 编程题-最小向量内积-(1)
- 如何实现泛型<T>带参数实例化
- linux Ext4 s_flex_groups
- IIS中的 MIME类型 HTTP 错误 404 - 文件或目录未找到。
- 使用commons-email-1.3.2.jar包发送邮件
- 经典排序算法 – 插入排序Insertion sort
- Python根据数量生成正确顺序的ip地址
- 打造一个精简的BaseActivity
- Android应用的调试
- Using opencv to process the video stream from camera
- oracle 如何给新加字段赋默认值
- poj1273 网络流 Edmonds_Karp
- JavaScript之apply,call以及bind方法
- Core Animation - 过渡动画 CATransition