c语言描述的直接插入排序法


#include<stdio.h>
#include<stdlib.h>
#define SIZE 6
typedef int Type;
//直接插入排序法
void InsertSort(Type a[],Type n){
    int i,j;
    int temp;
    for(i=1;i<n;i++){
        j=i-1;
        temp=a[i];//如果不使用中间变量,那么在移动后的a[i]会改变!
        while((j>=0)&&(temp<a[j])){//后移比当前插入的值要大的
            a[j+1]=a[j];
            j--;
        }
        a[j+1]=temp;//把当前插入的值放到j+1的位置,他前面的都要比他小,后面的都比他大
    }
    for(i=0;i<n;i++){
        printf("%d\n",a[i]);
    }
}
void main(){
    int a[SIZE]={1,5,3,4,6,7};
    InsertSort(a,SIZE);

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM