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