经典排序算法 – 插入排序Insertion sort
2013-09-12 14:35
555 查看
经典排序算法 – 插入排序Insertion sort
插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。
插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内容中进行。
图1演示了对4个元素进行直接插入排序的过程,共需要(a),(b),(c)三次插入。
以下代码仅供参考,欢迎指正
插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。
插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内容中进行。
图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(); }
相关文章推荐
- 经典排序算法 – 插入排序Insertion sort
- 经典算法(8)- 插入排序(Insertion Sort) 及三个基本排序算法的比较
- 【LeetCode-面试算法经典-Java实现】【147-Insertion Sort List(链表插入排序)】
- 排序算法---插入排序(Insertion Sort)
- 排序算法——插入排序(Insertion Sort)
- 排序算法 之 插入排序InsertionSort
- 排序算法-插入排序 Insertion Sort Θ(n-n)
- 基础排序算法 – 插入排序Insertion sort
- 排序算法总结之插入排序 Insertion Sort
- 排序算法--插入排序(Insertion Sort)_C#程序实现
- 基础排序算法 – 插入排序Insertion sort
- 排序算法——插入排序(Insertion Sort)
- 排序算法(一)—插入排序(Insertion sort)
- C#实现所有经典排序算法(选择排序,冒泡排序,快速排序,插入排序,希尔排序)
- 插入排序(Insertion Sort)
- 经典排序算法 - 基数排序Radix sort
- 插入排序 Insertion Sort
- 插入排序(Insertion Sort)
- LeetCode Insertion Sort List 链表的插入排序
- 经典排序算法之冒泡、选择和插入排序