码迷,mamicode.com
首页 > 其他好文 > 详细

Insertsort

时间:2019-06-06 18:53:04      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:std   ++   复杂   pre   color   str   insert   时间复杂度   nbsp   

 1 //插入排序时间复杂度为O(n)到O(n方) 
 2 #include<iostream>
 3 using namespace std;
 4 void Insertsort(int a[],int n)
 5 {
 6     for(int i=1;i<n;i++)//i是当前处理的数的下标,下标0到0已经排好了 
 7         for(int j=i-1;j>=0&&a[j+1]<a[j];j--)
 8               {
 9                       int tem=a[j+1];
10                       a[j+1]=a[j];
11                     a[j]=tem; 
12               }
13 } 
14 int main()
15 {
16     int a[10];
17     for(int i=0;i<10;i++)
18         cin>>a[i];
19     Insertsort(a,10);
20     for(int i=0;i<10;i++)
21         cout<<a[i];
22     return 0;
23 }

 

Insertsort

标签:std   ++   复杂   pre   color   str   insert   时间复杂度   nbsp   

原文地址:https://www.cnblogs.com/TYXmax/p/10986156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!