C++排序法

选择排序

// sort
void sort ( int x[] , int n )
{ int max , t ;
  for ( int i = 0 ;  i < n-1 ;  i ++ )	    //对数组排序
    {   t = i ;
        for ( int j = i + 1 ;  j < n ; j ++ )	    //寻找最大元素
              if ( x [ j ] > x [ t ] )  t = j ;
        if ( t != i )
          { max = x [ i ] ; 		    //交换数组元素
             x [ i ] = x [ t ] ;
             x [ t ] = max ;
         }		
    }
  return ;
} 

冒泡排序

void bubble ( int a[] , int size )
{ int i , temp , work ;
   for ( int pass = 1 ;  pass < size ;  pass ++ )      //对数组排序
     { work = 1 ;
        for ( i = 0 ;  i < size-pass ;  i ++ )
        if ( a [ i ] > a [ i + 1 ]  )		    //相邻元素比较
          { temp = a [ i ] ; 
             a [ i ] = a [ i + 1 ] ;  
             a [ i + 1 ] = temp ;  
             work = 0 ; 
          }
        if ( work )  break  ;
   }
} 

你可能感兴趣的:(C++,算法,排序算法,数据结构)