// implementation of Insertion Sort (C++)
#include <iostream>
using namespace std;
void SwapTwo(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void InsertSort(int arr[], int size)
{
for (int i=1; i<size; i++)
{
int inserter = arr[i];
int index = i-1;
while (index>=0 && inserter < arr[index])
{
arr[index+1] = arr[index];
index--;
}
arr[index+1] = inserter;
}
}
int main()
{
int nums[] = {5,3,7,2,1,9,14,8,7,4,30,18,1,23,27};
int size = sizeof(nums)/sizeof(int);
InsertSort (nums, size);
for (int i=0; i<size; i++)
{
cout << nums[i] << " ";
}
cout << endl;
return 0;
}
C++插入排序法(Insertion Sort),布布扣,bubuko.com
C++插入排序法(Insertion Sort)