c++插入排序代码详解

//
// Created by 91614 on 2018/12/20.
//插入排序

#ifndef ARRAY_9_11_H
#define ARRAY_9_11_H
template
void  insertionSort(T a[],int n){
    int i,j;//j是将要插入的位置,i是目标元素
    T temp;//模板
    //默认a[0]是已经排序好的
    for(int i=1;i0 && temp=a[j-1]时,j就是你要插入的位置
            //现在假设进入循环,给temp腾出位置
            a[j] = a[j-1];
            j--;
        }
        a[j] = temp;

    }

}
#endif //ARRAY_9_11_H

你可能感兴趣的:(c++插入排序代码详解)