简单插入排序

来源:互联网 发布:sql do while的用法 编辑:程序博客网 时间:2024/05/01 01:30
#include<iostream>using namespace std;//定义一个交换函数void swap(int &a,int &b){int tmp;tmp=a;a=b;b=tmp;}void InsertSort(int *A,int length){ int i,j;for(int i=1;i<length;++i){   if(A[i]<A[i-1]){int j=i;while(A[j]<A[j-1]&&j>=1){swap(A[j],A[j-1]);j--;}}}}int main(){   int a[9]={9,1,5,8,3,7,4,6,2};InsertSort(a,9);    for(int i=0;i<9;++i)cout<<a[i]<<" ";cout<<endl;system("pause");return 0;}

1 0
原创粉丝点击