码迷,mamicode.com
首页 > 编程语言 > 详细

直接插入排序

时间:2016-02-22 13:30:48      阅读:139      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!