美文网首页
直接插入排序

直接插入排序

作者: SDBridge | 来源:发表于2017-03-11 12:50 被阅读0次

插入排序:对一个拥有n个记录的有序序列,插入一个记录后,得到一个记录为n+1的有序序列的过程。
其中 n>=1;
我们升序排序

65FDF4A3-8817-4137-AF52-A83F8D1956D3.png

不断将后续的每个记录插入到已经排好序的序列之中,使之成为新的有序系列

#define N 10
static int array[N] ={0};

#pragma mark--插入排序 升序排序
void insertSort(int a[],int n){
    int i = 1;
    int j = 0;                         //代价       次数
    int temp = 0;
    for (; i< n; i++) {                //c1        n-1
        
        if (a[i]<a[i-1]) {             //c2        n-1
            
            j = i-1;                   //c3        n-1
            
            //a[i]待插入的数
            temp = a[i];//存储较小的数   //c4        n-1
            
    //每一个记录都和这个待插入的记录比较                //n-1
    while (j>=0&&a[j]>temp) {          //c5      // ∑j   //∑求和符号 j的取值范围[0,n-1]
                                                 //j=0
        

                                                 //n-1
                a[j+1] =a[j];         //c6          ∑j
                                                 //j=0
        
        
                                                // n-1
                j--;                  //c7      //  ∑j
                                                // j=0
            }
            a[j+1] = temp;            //c8          n-1

        }
    }
    
}
/*
                                                        n-1
      T(n) = c1(n-1) +c2(n-1)+c3(n-1)+c4(n-1) +(c5+c6+c7)∑j + c8(n-1)
                                                        j=0
 
      当第while行代码对每个j = 0,1,2...,n-1时,始终有a[j]>=temp,则是最佳情况,∑求和公式中j=1;
 
     从而得 T(n) = (c1+c2+c3+c4+c5+c6+c7+c8)(n-1) 
 
                = (c1+c2+c3+c4+c5+c6+c7+c8)n -(c1+c2+c3+c4+c5+c6+c7+c8)
 
        可以表示为=an+b  它是n的线性函数   时间复杂度O(n)
        
        所以当序列基本有序时,应采用直接插入排序
 
        当第while行代码对每个j = 0,1,2...,n-1时,始终有a[j]<temp,则是最坏情况,∑求和公式中(c5+c6+c7)(n-1)*n/2
 
      最后整理得 T(n) = an^2 +bn+c ,它是n的二次函数  时间复杂度O(n^2)
 
 */
#pragma mark --打印数组元素
void print(int a[],int n){
    
    printf("\n");
    for (int i = 0; i<n; i++) {
        
        printf("%5d",a[i]);
    }
    printf("\n");
}
#pragma mark--产生随机数组
void randomArray(){
    
    for (int i = 0; i < N ; i++) {
        
        array[i] = arc4random()%N;
    }
}

main函数调用如下:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        randomArray();
        printf("排序前\n");
        print(array, N);
        insertSort(array, N);
        printf("排序后\n");
        print(array, N);
        
    }
    return 0;
}

相关文章

  • 插入排序

    一、直接插入排序 二、折半插入排序

  • 【数据结构】【C#】013-插入类排序:🥇直接插入排序(稳定)

    插入排序:直接插入排序(稳定) 【 算法思想 】 直接插入排序是一种最基本的插入排序方法,其基本操作是将第 i 个...

  • 排序——插入排序

    业精于勤荒于嬉 插入排序包括:直接插入排序、折半插入排序、希尔排序(缩小增量排序) 一、直接插入排序 1. 算法思...

  • 常用算法

    插入排序 包括直接插入排序和希尔插入排序 直接插入排序 将一个记录插入到已经排序好的有序表中。 sorted数组的...

  • 算法(排序)

    一、内部排序 1、插入排序—直接插入排序(Straight Insertion Sort) 2、插入排序—希尔排序...

  • 直接插入排序

    //直接插入排序

  • iOS算法

    排序方法 选择排序:直接选择排序、堆排序。 交换排序:冒泡排序、快速排序。 插入排序:直接插入排序、二分法插入排序...

  • 几种实用的简易的排序算法

    也是面试题 一、插入排序 1.插入排序—直接插入排序(Straight Insertion Sort) 思路 遍历...

  • 2.1-插入排序-直接插入

    参考链接 插入排序:直接插入排序(Straight Insertion Sort) 白话经典算法系列之二 直接插入...

  • 经典排序算法-希尔排序Shell sort

    一、希尔排序思想 希尔排序是基于插入排序的快速的排序算法,先分组后对每组进行直接插入排序,再分组再直接执行插入排序...

网友评论

      本文标题:直接插入排序

      本文链接:https://www.haomeiwen.com/subject/picggttx.html