直接插入排序(insert sort)
2013-12-27 13:43
344 查看
功能:对一组数进行排序。
基本思想:把待排数据中的数据一个一个插入以排好序的序列中,使整个数据成有序序列,如下图所示。
(图片来源于维基百科)
算法实现思想:有两个已知序列A,B,A为一组以排好序的数据,B为一组未排序数据。首先让A中只有一个数据,那么A必定为有序的,然后从B中逐个取出数据,插入A中,让A始终保持有序,知道B中的数据被取完,则A中的数据必定为有序的。
时间复杂度:O(n2)
用途:数据量较小。
基本思想:把待排数据中的数据一个一个插入以排好序的序列中,使整个数据成有序序列,如下图所示。
(图片来源于维基百科)
算法实现思想:有两个已知序列A,B,A为一组以排好序的数据,B为一组未排序数据。首先让A中只有一个数据,那么A必定为有序的,然后从B中逐个取出数据,插入A中,让A始终保持有序,知道B中的数据被取完,则A中的数据必定为有序的。
#include <stdio.h> void insertion_sort(int array[], int first, int last) { int i, j , k , x; int z; for(i = 1; i < last; i++) { k = array[i]; z = i; for(j = i - 1; j >= 0; j --) { if(array[j] >= k) { for(x = z-1; x >= j; x--) { array[x + 1] = array[x]; } array[j] = k; z --; } else { break; } } } } void main() { int a[20]; int i = 0; int j; int k = 0; printf("请输入要排序的数"); while(1) { scanf("%d", &j); if(j == 0) { break; } a[i] = j; k++; i++; } insertion_sort(a, 0, i); int z; for(z = 0;z < i; z++) { printf("%d ", a[z]); } printf("\n"); }
时间复杂度:O(n2)
用途:数据量较小。
相关文章推荐
- Java排序算法(四)--直接插入排序(InsertSort)
- C:C的排序算法:直接插入排序(InsertSort)
- 插入排序(1)——直接插入排序(insert sort)
- 插入排序之直接插入(InsertSort)
- 直接插入排序(Straight Insert Sort)
- 算法不会,尚能饭否之排序——直接插入排序(Insert sort)
- (直接)插入排序INSERT_SORT
- 直接插入排序(Insertion Sort)
- 直接插入排序(Straight Insertion Sort)代码及分析注释
- 冒泡排序(bubble sort)、直接插入排序(straight insertion sort)、简单选择排序(simple selection sort)
- 1.插入排序—直接插入排序(Straight Insertion Sort)
- PHP实现排序算法----直接插入排序(Straight Insertion Sort)
- 插入排序---直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- 插入排序:直接插入排序-Direct insertion sort
- 直接插入排序(Straight Insertion Sort)
- 算法笔记:(一)插入排序:直接插入排序(Straight Insertion Sort)
- 1、直接插入排序(Straight Insertion Sort)
- 插入排序—直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)