InsertionSort

来源:互联网 发布:情义我心知解析 编辑:程序博客网 时间:2024/06/05 10:07
//insertion-sort//input a[10]//output a[10](ASC sequence)//////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include<iostream>using namespace std;void InsertSort(int *a, int len){for(int i=1;i<len;i++){while(a[i]<a[i-1]&& i>0){swap(a[i],a[i-1]);//系统函数,对a[i]和a[i-1]中的数据进行交换i--;}}}void main(){int a[10]={1,2,5,6,3,2,6,6,8,4};int len=sizeof(a)/sizeof(a[0]);InsertSort(a,len);for(int i=0;i<len;i++)cout<<a[i]<<"  ";getchar();}