Data structures (reviewed sorting algorithm) - selected from the foam plug (selected bubble, insertion, Hill)

#include <stdio.h> / * * 
* Hill sorting (optimization) 
* average time complexity: O (n-^ 1.3) 
* average spatial complexity: O (. 1) 
* Stability: Unstable 
* ideas: packet carried out for each gap are divided by 2, 
* such size component, benefits the mobile data becomes smaller 
* * / void shell_sort ( int a [], int len) {
     for ( int gap len >> = 2 ; GAP> 0 ; GAP / = 2 ) {
             for ( int I = GAP; I <len; I ++ ) {
                 int J, tmp = A [I];
                 for (J = I; J> 0 && tmp <A [J-GAP ]; = J- GAP) {



                        A [J] = A [J- GAP]; 
                } 
                A [J] = tmp; 
            } 
    } 
} 

/ * * 
* insertion sort (optimization) 
* average time complexity: O (^ n-2) 
* average spatial complexity: O (. 1) 
* stability: stable 
* ideas: circulated from front to back, back to front from the circulation, 
* if a large front than the mark with a live end of the cycle 
* exchanged 
* * / 
void insertion_sort_update ( int A [], int len) {
     for ( int I = . 1 ; I <len; I ++ ) {
         int J, tmp = A [I];
         for (I = J; J>0 && tmp <A [J- . 1 ]; J, ) { 
                A [J] = A [J- . 1 ]; 
        } 
        A [J] = tmp; 
    } 
} 

/ * * 
* insert sequencing 
* average time complexity: O (^ n-2) 
* average spatial complexity: O (. 1) 
* stability: stable 
* ideas: circulated from front to back, back to front from the circulation, 
* if a large front than the swap 
* * / 
void insertion_sort ( int A [], int len) {
     for ( int I = . 1 ; I <len; I ++ ) {
         for ( int J = I; J> 0 && A [J] <A [J-. 1 ]; J, ) {
                 int tmp = A [J]; 
                A [J] = A [J- . 1 ]; 
                A [J - . 1 ] = tmp; 
        } 
    } 
} 

/ * * 
* Selection Sort 
* average time complexity degree: O (n-^ 2) 
* complexity average space: O (. 1) 
* ideas: the former is selected after selecting the minimum 
* exchanged with the digital beginning 
* * / 
void selection_sort ( int a [], int len) {
     for ( int I = 0 ; I <len . 1 ; I ++ ) {
         int MIN_NUM =I;
         for ( int J = I + . 1 ; J <len; J ++ ) {
             IF (A [J] <A [MIN_NUM]) MIN_NUM = J; 
        } 
        int tmp = A [MIN_NUM]; 
        A [MIN_NUM] = A [I ]; 
        A [I] = tmp; 
    } 
} 

/ * * 
* bubble sort 
* average time complexity: O (^ n-2) 
* average spatial complexity: O (. 1) 
* ideas: for forwardly from the rear bubble, 
* each bubble is possible to float the maximum number, 
* so as to achieve the effect of sorting 
* * / 
void bubble_sort ( int A [], int len) {
     for ( int i=len-1;i>=0;i--){
        for(int j=1;j<=i;j++){
            if(a[j]<a[j-1]){
                int tmp=a[j];
                a[j]=a[j-1];
                a[j-1]=tmp;
            }
        }
    }
}

int main()
{
    int a[]={5,1,4,7,8,6,2};
    shell_sort(a,sizeof(a)/sizeof(int));
    for(int i=0;i<sizeof(a)/sizeof(int);i++) printf("%d ",a[i]);
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11444482.html