直接插入排序

来源:互联网 发布:手机数据备份软件 编辑:程序博客网 时间:2024/05/20 02:28
// InsertSort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>void InsertSort(int a[],int n){int i,j;int temp;for (i=1;i<n;i++){temp=a[i];//取出第一个准备插入的数字for(j=i-1;j>=0 && temp<a[j];j--){a[j+1]=a[j];}a[j+1]=temp;//插入数据到序列}}int _tmain(int argc, _TCHAR* argv[]){int a[]={2,3,1,5};    InsertSort(a,4);      for (int i=0;i<4;i++)      {      printf("%3d ",a[i]);      }  system("pause");return 0;}