Hackerrank && Insertion Sort - Part 1

来源:互联网 发布:js 数组 json 编辑:程序博客网 时间:2024/04/30 05:28
#include <iostream>using namespace std ;int main(){int n ;cin >> n ;int a[n]  ;for ( int i = 0 ; i < n ; ++i)            cin>>a[i] ;    bool flag = false ;int temp = a[n-1];for ( int i = n-2 ; i >= 0 ;--i)      {           if (a[i]>temp)           {                                 a[i+1]  = a[i] ;   for (int j = 0 ; j <n; ++j)       cout << a[j] << " " ;   cout << endl ;   if (i==0)              {                    a[i]  = temp ;      //a[i]  =  temp ;      for (int j = 0 ; j <n; ++j)      cout << a[j] << " " ;      cout << endl ;      }       }    else  {  a[i+1] = temp ;  temp  = a[i] ;  for (int j = 0 ; j <n; ++j)       cout << a[j] << " " ;  cout << endl ;  break ;    }          }      return 0 ;}
这是insert排序的 初步 排序,这里面 把最后一个当做了哨兵,然后 与前面的元素相比较 。。。
0 0
原创粉丝点击