直接插入排序(Insertion Sort)
来源:互联网 发布:免费洗车的软件 编辑:程序博客网 时间:2024/05/22 11:40
算法描述:一种简单直观的排序算法,其通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入;插入排序在实现上,通常采用In-place排序(即只需用到
复杂度分析:最坏时间复杂度为
主要优势:
- 稳定;
- 不占用额外内存;
- 如果序列基本有序,则插入排序简单且高效,即
T(N,I)=O(N+I) ,I 为逆序对的个数。
C语言描述:
#include<stdio.h>typedef int ElementType;void Swap(ElementType *A, ElementType *B) { ElementType temp = *A; *A = *B; *B = temp;}/* 插入排序C语言描述 */void Insertion_Sort(ElementType Data[], int N) { int P, i; ElementType temp; for (P = 1; P < N; P++) { temp = Data[P]; /*摸一张牌*/ for (i = P; i > 0 && Data[i - 1] > temp; i--) Data[i] = Data[i - 1]; /*移出空位*/ Data[i] = temp; /*新牌落位*/ }}int main() { int i; ElementType Data[] = {19, 3, 10, 20, 22, 28, 33, 23, 15, 30}; Insertion_Sort(Data, 10); return 0;}
附:时间复杂度下界
- 对于下标
i<j ,如果A[i]>A[j] ,则称(i,j) 是一对逆序对(inversion); - 交换两个相邻元素正好消去一个逆序对,插入排序
T(N,I)=O(N+I) ; - 定理:任意N个不同元素组成的序列平均具有N(N-1)/4个逆序对;
- 定理:任何仅以交换相邻两元素来排序的算法,其平均时间复杂度为
Ω(N2) 。
阅读全文
0 0
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Insertion Sort)
- 直接插入排序(Insertion Sort)
- 直接插入排序(Insertion Sort)
- 直接插入排序(Insertion Sort)
- 直接插入排序(Insertion Sort):
- 排序之直接插入排序(Straight Insertion Sort)
- 排序(三)之直接插入排序Straight Insertion Sort
- 直接插入排序算法(Straight Insertion Sort) Java实现
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- 插入排序(insertion sort)
- 插入排序(insertion sort)
- 插入排序(Insertion Sort)
- 插入排序(Insertion-sort)
- HDU
- 详解Paint的setPathEffect(PathEffect effect)
- 《计算机组成原理》_第二版_唐硕飞_第一章知识点归纳
- 剑指offer-18:判断树的子结构
- logback配置
- 直接插入排序(Insertion Sort)
- 从collection中取op
- JS 字符串 常用操作
- poj 1679 The Unique MST
- 【cocos2d-x 源码剖析】启动窗口
- 【C++】G++命令简介
- eclispe 设置自动提示
- MySQL 触发器
- logback属性