首页 > 编程语言 > 详细

直接插入排序

时间:2016-02-22 13:28:11      阅读:115      评论:0      收藏:0      [点我收藏+]
// 2015_2_22insertsort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void InsertSort(int sort[],int n);
int main(int argc, char* argv[])
{
printf("Hello World!\n");
	int arr[4]={3,2,1,4};

printf("数组长度%d\n",sizeof(arr)/sizeof(arr[0]));
	InsertSort(arr,4);
	for(int i=0;i<4;i++)
{
printf("%d",arr[i]);
}
printf("\n");
	return 0;
}
void InsertSort(int sort[],int n)
{
int temp;
int j;
for(int i=1;i<n;i++)
{
temp=sort[i];
j=i-1;
while(j>=0 && temp<sort[j])
{
sort[j+1]=sort[j];
j--;
}
sort[j+1]=temp;//这里面赋值竟然是j+1
}
}
 

直接插入排序

原文:http://www.cnblogs.com/Study02/p/5206682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!