算法笔记 --- Insertion Sort

#include <iostream>

using namespace std;
class InsertionSort {
public:
    int* insertionSort(int* A, int n) {
        // write code here
        int tmp;
        int index_insert;
        for(int index = 1; index < n; index ++){
            index_insert = index;
            while(A[index_insert] < A[index_insert - 1] && index_insert >= 1){
                tmp = A[index_insert];
                A[index_insert] = A[index_insert - 1];
                A[index_insert - 1] = tmp;
                index_insert--;
            }
        }
        return A;
    }
};
int main()
{
    int a[6] = {1, 5, 7, 4, 2, 9};
    int* res;
    InsertionSort sorter;
    res = sorter.insertionSort(a, 6);
    cout<<"after sorting:"<<endl;
    for(int i = 0; i < 6; i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
   
    return 0;
}
原文地址:https://www.cnblogs.com/zhongzhiqiang/p/5791084.html