5

插入排序 C实现

 1 year ago
source link: http://bean-li.github.io/insertsort/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

插入排序 C实现

首页 分类 标签 留言 关于 订阅 2023-06-10

|

分类 算法 

|

标签 算法 

复习《算法:C语言实现》一书,本文实现了插入排序。插入排序的思路和人打牌的时候,摸牌,把新摸到的牌插入到指定位置很相似。

#ifndef _SORT_BASE_H_
#define _SORT_BASE_H_

typedef int Item ;

#define Key(A) (A)
#define less(A,B) (Key(A) < Key(B))
#define exch(A,B) { Item tmp = A; A=B ; B=tmp;}
#define compexch(A, B) if(less(B, A)) exch(A,B)


#endif

#ifndef _INSERTSORT_H
#define _INSERTSORT_H

#include "sort_base.h"

void insertsort_slow(Item a[], int l , int r)
{
    int i ,j ;
    for(i = l+1 ; i <=r ; i++)
        for(j = i; j > l; j--)
            compexch(a[j-1], a[j]);
}

void insertsort(Item a[], int l,  int r)
{
    int i ;

    //put minimal value to the leftmost position , as sentinel key
    for(i = r ; i  > l; i--)
        compexch(a[i-1], a[i]);

    for( i = l+2 ; i <=r ; i++)
    {
        int j = i ;
        Item v = a[i] ;

        /* no need add j-1 >= l condition, because a[0] is alreaay the most minimal value already*/
        /* use while for break immediately to skip useless compare*/
        while(less(v, a[j-1]))
        {
            a[j] = a[j-1] ;
            j-- ;
        }
        a[j] = v ;
    }
}

#endif

insertsort_slow 这个版本,更容易理解,但是效率不高。

  • 内层循环是从右往左比较,而左边的子序列实际上已经是有序的了,当碰到关键字不大于整备插入的数据项的关键字的时候,其实没有继续比较了,缺少了break机制,增加了无谓的比较。
  • j > l 这个比较,大部分情况下是无用的,只有要插入的元素是最小的并且要插入数组的最前端的时候,该条件才有真正的意义。通常的一个改进思路是把最小的元素放在a[0] ,作为哨兵,只需要对a[1]~a[N]的元素进行排序即可。这种改进思路,可以大面积减少比较。

通过上述思路分析,将insertsort改进成最终的版本。


Recommend

  • 72

    插入排序的思想就和玩扑克是的摸牌一样,摸到一张牌放手上,再摸一张和之前的比较,大的就放后面,小的就放前面。一个数列我们把它分为两个区,一个是已经排序的区,一个是乱序区,选取第一个元素出来作为排序区的元素,然后从第二个元素开始往后作为乱序区,从第...

  • 38
    • studygolang.com 4 years ago
    • Cache

    从插入排序来理解 Go 的接口

    根据插入排序的思想,我们很容易就可以完成插入排序的代码如下。 func insertionSort(data []int) { lo, hi := 0, len(data) - 1 for i := lo + 1; i < hi; i++ { for j := i; j > lo && data[j...

  • 34

    插入排序(Insertion sort)是一种简单直观且稳定的排序算法。如果有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序法,插入排序的基本...

  • 18
    • segmentfault.com 3 years ago
    • Cache

    排序算法入门之「插入排序」

  • 14
    • www.cnblogs.com 3 years ago
    • Cache

    小白懂算法之插入排序

    前言 真的,看到挺多博客的插入排序,思路大多都是对的,但是在代码实现上不严谨,甚至还有的跟冒泡排序搞混了,还是有必要分析波插入排序,希望能帮助到大家。 一.插入排序原理 插入排序原理是: 逐...

  • 10

    《算法导论》2.1 节《插入排序》:笔记、代码实现与练习解答¶ 笔记

  • 6
    • suiyia.github.io 3 years ago
    • Cache

    排序算法——直接插入排序

    排序算法——直接插入排序 2019-10-31

  • 8
    • 微信 mp.weixin.qq.com 3 years ago
    • Cache

    Dart 算法篇 | 插入排序

    1.源码中的 Sort 排序 在 sky_engine/lib/internal/sort.dart 中有一个 Sort 类,其中 sortRange 方法可以进行排序。可以看出 sortRange 方法是对 List 索引范围区间进行排序。

  • 6

    by zhangxinxu from http://www.zhangxinxu.com/wordpress/?p=5766 本文可全文转载,但需得到原作者书面许可,同时保留原作者和出处,摘要引流则随...

  • 3
    • panda843.github.io 2 years ago
    • Cache

    PHP之插入排序的实现

    PHP之插入排序的实现 发表于 2017-12-28 | 分类于 开发 | | 浏览4 次 | 字数统计: 573 | 阅读时长 ≈ 2有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK