Ordenamiento de burbuja, tipo de inserción y selección especie

#include <iostream>
 el uso de  espacio de nombres std;
#define ElemType int
 const  int Maxn = 105 ;
void InsertSort (ElemType A [], int n) {
     int i, j;
    para (i = 2 ; i <= n; i ++ ) {
         si (A [i] <A [i- 1 ]) { 
            A [ 0 ] = a [i];
            para (j = i- 1 ; A [j]> A [ 0 ]; j-- ) { 
                A [j + 1 ] = A [j]; 
            }
            A [j +. 1 ] = A [ 0 ]; 
        } 
    } 
} 
// tiempo: preferiblemente O (n) y el O peor promedio (^ n-2)
 // (. 1): Space O
 // estable 
void SelectSort (elemType A [], int n-) {
     para ( int I = 0 ; I <N- 1. ; I ++ ) {
         int Mmin = I;
         para ( int J = I + 1. ; J <n-; J ++ ) {
             IF (a [J] < a [Mmin ]) 
                Mmin = J; 
        }
         I)si (mmin =!
            swap (A [i], A [mmin]); 
    } 
} 
// 时间: O (n ^ 2)
 // 空间: O (1)
 // 不稳定
void BubbleSort (ElemType A [], int n) {
     para ( int i = 0 ; i <n- 1 ; i ++ ) {
         bool f = false ;
        para ( int j = n- 1 ; j> i; j-- ) {
             si (A [j] <A [j- 1 ]) { 
                f = verdadero ; 
                swap (A [j], A [j - 1 ]); 
            } 
        } 
        SI (! F) de retorno ; 
    } 
} 
// tiempo: preferiblemente O (n) y el O peor promedio (^ n-2)
 // (. 1): Space O
 // estable 
int main () {
     int n-; 
    CIN >> n-; 
    elemType ARR [MAXN]; 
    para ( int I = 1. ; I <= N-; I ++ ) { 
        CIN >> ARR [I]; 
    } 
    // SelectSort (ARR, n-);
     // el BubbleSort ( ARR, n-); 
    InsertSort (ARR, n-);
     para ( int I =1  ; I <= N-; I ++) { 
        cout << arr [i] << "  " ; 
    } 
    Cout << endl;
    volver  0 ; 
} 
// 12
 // 8 2 9 3 23 7 2 6 4 1 16 7

 

Supongo que te gusta

Origin www.cnblogs.com/lyt888/p/12539742.html
Recomendado
Clasificación