大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang

以下用C++实现插入排序的升序和降序排序

算法来自《算法导论》

#include<iostream>
using namespace std;
 
void INSERTION_SORT(int *A, int N)
{
    for(int j = 1; j < N; ++j)
    {
        int key = A[j];
        int i = j - 1;
        while(i >= 0 && key < A[i])
        {
            A[i + 1] = A[i];
            -- i;
        }
        A[i + 1] = key;
    }
}
 
int main()
{
    int b[] = {4,3,2,1};
    INSERTION_SORT(b,4);
    for(int i = 0; i < 4; ++i)
        cout << b[i] << endl;
 
    return 0;
}
----------------------------------------------------------------------------

#include<iostream>
using namespace std;
 
void INSERTION_DESCSORT(int *A, int N)
{
    for(int j = 1; j < N; ++ j)
    {
        int key = A[j];
        int i = j - 1;
        while(i >= 0 && A[i] < key)
        {
            A[i + 1] = A[i];
            -- i;
        }
        A[i + 1] = key;
    }
}
 
int main()
{
    int b[] = {1,2,3,4,5};
    INSERTION_DESCSORT(b, 5);
    for(int i = 0; i < 5; ++i)
        cout << b[i] << endl;
 
    return 0;
}
 

INSERTION_SORT插入排序C++实现的更多相关文章

  1. 【算法】插入排序 insertion_sort

    准备写个<STL 源代码剖析>的读书笔记,开个专栏.名为<STL 的实现>,将源代码整理一遍.非常喜欢侯捷先生写在封底的八个字:天下大事.必作于细.他在书中写到:"我 ...

  2. 插入排序(insertion_sort)

    最简单的排序算法,又称插值排序,原理类似于打扑克牌时把摸到的牌插入手中已有序牌的过程. void insertion_sort(int* A ,int n){ int i,j,key; ;i < ...

  3. 插入排序(insertion_sort)——Python实现

      # 插入排序 # 作用:对给出的n个顺序不定的数进行排序 # 输入:任意数组A # 输出:按顺序排列的数组A # 时间复杂度 n(n-1) 至 (n(n-1))/2   # 插入排序过程 # 第一 ...

  4. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  5. 算法导论之python实现插入排序

    插入排序的花费时间 c*n2, c 是常数 伪代码 INSERTION-SORT(A) for i  to A.length key = A[j] //Insert A[j] into the sor ...

  6. Python学习之---冒泡,选择,插入排序

    Python学习之---冒泡,选择,插入排序 最近学习了python基础,写一下3大排序练练手: 1 ''' 2 Created on 2013-8-23 3 4 @author: codegeek ...

  7. JavaScript ,Python,java,C#,Go系列算法之【插入排序篇】

    常见的内部排序算法有:插入排序.希尔排序.选择排序.冒泡排序.归并排序.快速排序.堆排序.基数排序等.用一张图概括: 插入排序 插入排序(英语:Insertion Sort)是一种简单直观的排序算法. ...

  8. 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现

    排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...

  9. 算法 排序lowB三人组 冒泡排序 选择排序 插入排序

    参考博客:基于python的七种经典排序算法   [经典排序算法][集锦]     经典排序算法及python实现 首先明确,算法的实质 是 列表排序.具体就是操作的列表,将无序列表变成有序列表! 一 ...

随机推荐

  1. tableViewCell 的删除按钮

    - (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIn ...

  2. CloudStack cloud数据库op_host_capacity表type与控制板上的内容的对应关系

    listCapacity: type 名称 0 内存 1 CPU 3 主存储 4 公用IP地址 5 管理类IP地址 6 辅助存储 7 VLAN 9 本地存储 ViewResponseHelper.ja ...

  3. Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool

    错误如下: -- ::,-[TS] INFO http-- org.springframework.beans.factory.support.DefaultListableBeanFactory - ...

  4. LeetCode_Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  5. volatile详解

    海子的Java并发编程:volatile关键字解析讲的已经非常不错了,通俗易懂,给32个赞!

  6. UVA 10594-Date Flow(无向图的最小费用网络流+题目给的数据有误)

    题意:给一个有N个点的无向图,要求从1向N传送一定的数据,每条边的容量是一定的,如果能做到,输出最小的费用,否则输出Impossible. 解析:由于是无向图,所以每个有连接的两个点要建4条边,分别是 ...

  7. poj3299

                                                                                                         ...

  8. nyoj 234 吃土豆

    描述 Bean-eating * grid. Now you want to eat the beans and collect the qualities, but everyone must ob ...

  9. UIView 转 UIImage

    这个方法很实用,特别是在做水印相机得时候... - (UIImage*) imageWithUIView:(UIView*) view{ // 创建一个bitmap的context // 并把它设置成 ...

  10. java技术学习网址收藏

    Bootstrap:http://www.runoob.com/bootstrap/bootstrap-intro.html AngularJS : http://www.runoob.com/ang ...