现在的位置: 首页 > 综合 > 正文

Hackerrank && Insertion Sort – Part 1

2018年10月30日 ⁄ 综合 ⁄ 共 430字 ⁄ 字号 评论关闭
#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排序的 初步 排序,这里面 把最后一个当做了哨兵,然后 与前面的元素相比较 。。。

抱歉!评论已关闭.