#pragma once #include <assert.h> void InsertSort(int* array, size_t n) { assert(array); for (size_t i = 0; i < n-1; ++i) { int end = i; int tmp = array[end+1]; while ((end >= 0) && (array[end] < tmp)) { array[end+1] = array[end]; --end; } array[end+1] = tmp; } } void InsertSortTest() { int array[] = {2, 4, 6, 5, 3, 1, 8, 7, 0, 9}; InsertSort(array, sizeof(array)/sizeof(array[0])); for (size_t i = 0; i < sizeof(array)/sizeof(array[0]); ++i) { cout<<array[i]<<" "; } cout<<endl; }
#include <iostream> using namespace std; #include "InsertSort.h" int main() { InsertSortTest(); return 0; }
本文出自 “zgw285763054” 博客,请务必保留此出处http://zgw285763054.blog.51cto.com/11591804/1786960
原文:http://zgw285763054.blog.51cto.com/11591804/1786960