插入排序

将一个元素插入已经有序的序列,和要插入的元素作比较找到插入位置是关键,插入位置后面的元素后移。

//
//  InsertSort.c
//  libin
//
//  Created by 李宾 on 16/5/4.
//  Copyright © 2016年 李宾. All rights reserved.
//

#include <stdio.h>
void Insert_Sort(int *p, int len)
{
    for (int i = 1; i < len; i ++) {
        if (p[i] < p[i - 1])
        {
            int temp = p[i];
            int j = 0;
            for (j = i - 1; p[j] > temp && j >= 0; j --) {
                p[j + 1] = p[j];
            }
            p[j+1] = temp;
        }
        
    }
    
}

int main()
{
    int a[4] = { 3, 1, 2, 5};
    Insert_Sort(a, 4);
    for (int i = 0; i < 4;  i ++) {
        printf("%d	", a[i]);
    }
    printf("
");
}
原文地址:https://www.cnblogs.com/masterlibin/p/5459924.html