[Algorithm] [sort] [insert class] Shell sort ShellSort

#include <stdio.h> 
#include <time.h>  
#include <stdlib.h> int main () {
     int A [ 15 ];
     // set up a random number 
    srand (Time ( 0 ));  
     for ( int I = 0 ; I < 15 ; I ++ ) { 
        a [I] = RAND ()% ( 30 );     // range of random numbers between 0 to 29     } 
     // array a size int size = the sizeof (a) / . 4 ;
     / / print information array for ( int I =



    
    0 ; I <size; I ++ ) { 
        the printf ( " % D " , A [I]); 
    } 
        
    void ShellSort ( int A [], int size); 
    
    ShellSort (A, size); 
    
    return  0 ; 
} 

void ShellSort ( int a [], int size) {
     int All = size;     
     int L = size;         // comparison distance: distance L 

    // swap values address 
    void the swap ( int * I, int * J); 
    
    the while ( to true) {
         // Comparative half distance 
         // if the comparison distance is 0, the loop is exited 
        L = L / 2 ;
         IF (L == 0 ) {
             BREAK ; 
        } 
        // Output compare the distance 
        the printf ( " \ n-% D \ n- " , L); 
        
        // right comparison 
        for ( int I = 0 ; I <All-L; I ++ ) {
             // if they are in the right small values of x 
            IF (a [I]> a [I + L] ) {
                 // first value of the current position x and exchange 
                the swap (I + a, a + I + L);
                 //If the current position from the current comparison was compared the 
                IF (IL> = 0 ) {
                     // then compare also leftward from the position 
                    for ( int J = I; JL> = 0 ; J = JL ) {
                         // until x moves to the appropriate position: * (DX-L) <* DX <* (DX + L) 
                        IF (a [J] <a [J- L]) { 
                            the swap (a + J, J + a - L); 
                        } the else {
                         // Since the position in front of the already sorted, if the encountered number smaller than x to reach the appropriate position represented 
                            GOTO leftend; 
                        } 
                    } 
                }  
                leftend: {}
            } 
        }
    //打印数组信息 
        for(int i=0;i<size;i++){
            printf("%d ",a[i]);
        }    
    }
}


void swap(int* i,int* j){
    int t=*j;
    *j=*i;
    *i=t;
}

 

Guess you like

Origin www.cnblogs.com/LPworld/p/11223348.html