直接插入排序

来源:互联网 发布:局域网添加网络打印机 编辑:程序博客网 时间:2024/06/05 07:27
//#include"aa.h"#include"time.h"#include <iostream>  using namespace std;  void InsertSort(int s[], int len){int j;for(int i=1;i<len-1;i++)                //s[0]已在正确位置,从s[1]开始扫描   {   int temp=s[i];                  //将s[i]取出准备插向正确的位置for(j=i-1;j>=0;j--)         //for(j=i-1,temp<s[j];j--)的另一种写法,直接在循环体内判断,不需if语句                                                                                    //0到i-1的位置是有序的,j从有序的最后一个位置i-1开始往前扫描,一直到s[0]{if(temp<s[j])            //每次遇到比是s[i](即temp)大的值,就将它们往后移一位    s[j+1]=s[j];         //因为上面的for循环j的第一步在i-1,即有序的最后一位,所以后移的时候第一步是去j后面那个位置即j+1else break;}s[j+1]=temp;             //此时temp赋给j+1位置的原因,上面的for循环最后一步后执行j--,j现在在j-1的位置,需要+1补回来 cout<<"第"<<i<<"次直接插入排序的结果是: "; for(int k=0;k<len;k++) cout<<s[k]<<" "; cout<<endl;}}int main(){int a[10]={6,3,14,12,4,5,1,31,53,62,};InsertSort(a,10);cout<<endl;cout<<"程序耗时: "<<clock()<<" ms"<<endl;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    system("pause");return 0;}

0 0
原创粉丝点击