您的位置:首页 > 编程语言 > C语言/C++

C++实现插入排序

2016-06-07 15:22 351 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: