排序算法之鸡尾酒排序

本文介绍了鸡尾酒排序算法的基本思想及其三种不同实现方式。该算法通过从两端交替比较和交换元素来完成排序过程,类似于冒泡排序但更加高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法描述:

数组中的数字本是无规律的排放,先找到最小的数字,把他放到第一位,然后找到最大的数字放到最后一位。然后再找到第二小的数字放到第二位,再找到第二大的数字放到倒数第二位。以此类推,直到完成排序。

算法度量:

数据结构: 数组
最差时间复杂度:  O(n^2)
最优时间复杂度: O(n)
平均时间复杂度:   O(n^2)
最佳算法:  No


源码1:完全按照鸡尾酒算法的描述进行实现,实际的循环次数与冒泡算法是一致的


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>

void print_ary( int * ary, unsigned int len )
{
    while( len-- > 0 ) {
        printf("%d ", *ary++ );    
    }
    putchar('\n');
}

void create_rondom_number(int **parray, unsigned int number)
{
    //struct timeval tpstart;//for linux
    unsigned int i =0;

    if (NULL != *parray)
    {
        return;
    }

    *parray = (int *)malloc(sizeof(unsigned int) * number);
    (void)memset(*parray, 0, sizeof(unsigned int) * number);
    
    //gettimeofday(&tpstart,NULL); //for linux

    srand(time(NULL));//standard c
    //srandom(time(NULL));//for linux
    //srandom(tpstart.tv_usec);//for linux

    for (i=0; i<number; ++i)
    {
        (*parray)[i] = rand()%number; // range is [0, number-1] //standard c
        //(*parray)[i] = random()%number; // range is [0, number-1]//for linux 
        //(*parray)[i] = random()%(b-a) + a; //range is [a, b)//for linux
        //((*parray)[i] = 1 + (int) ((double)number * rand() / (RAND_MAX + 1.0))); // [1, number]//for linux
    }
}

void cocktail_sort(int *list, unsigned int list_length)
{ 
    unsigned int bottom = 0;
    unsigned int top = list_length - 1;
    int tmp;
    unsigned int i;
    unsigned int counter = 0;

    while(bottom < top) 
    {
	printf("\n1num=%d top=%d bottom=%d\n", top - bottom, top, bottom);
        for(i = bottom; i < top; i = i + 1)
        {
            printf("bottom -> top a[%d] & a[%d](i=%d) ", i, i+1, i);
            if(list[i] > list[i + 1])  // test whether the two elements are in the correct order
            {
                tmp = list[i];
                list[i] = list[i+1];
                list[i+1] = tmp;
                printf("swap ");
            }
            print_ary(list, list_length);
	    counter++;
        }

        // decreases top the because the element with the largest value in the unsorted
        // part of the list is now on the position top 
        top = top - 1; 
	printf("\n2num=%d top=%d bottom=%d\n", top - bottom, top, bottom);
        for(i = top; i > bottom; i = i - 1)
        {
            printf("top -> bottom a[%d] & a[%d](i=%d) ", i, i-1, i);
            if(list[i] < list[i - 1]) 
            {
                tmp = list[i];
                list[i] = list[i-1];
                list[i-1] = tmp;
                printf("swap ");
            }
            print_ary(list, list_length);
	    counter++;
        }
        // increases bottom because the element with the smallest value in the unsorted 
        // part of the list is now on the position bottom 
        bottom = bottom + 1;  
    }
    printf("counter = %d\n", counter);
}

int main( int argc, char ** argv )
{
    int *pary = NULL;

    if (argc !=2)
    {
        printf("\nUsage: %s Number\n", argv[0]); 
        return -1;
    }

    create_rondom_number(&pary, (unsigned int)atoi(argv[1]));    

    printf("\nrandom number: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );

    printf("\nStart sortting: \n");
    cocktail_sort( pary, (unsigned int)atoi(argv[1]));

    printf("\nafter sorted: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );
    
    return 0;
}


 

运行结果:

C:\WINDOWS\system32\cmd.exe /c bibubble 10

random number: 0 5 6 1 7 6 4 7 0 7

Start sortting:

1num=9 top=9 bottom=0
bottom -> top a[0] & a[1](i=0) 0 5 6 1 7 6 4 7 0 7
bottom -> top a[1] & a[2](i=1) 0 5 6 1 7 6 4 7 0 7
bottom -> top a[2] & a[3](i=2) swap 0 5 1 6 7 6 4 7 0 7
bottom -> top a[3] & a[4](i=3) 0 5 1 6 7 6 4 7 0 7
bottom -> top a[4] & a[5](i=4) swap 0 5 1 6 6 7 4 7 0 7
bottom -> top a[5] & a[6](i=5) swap 0 5 1 6 6 4 7 7 0 7
bottom -> top a[6] & a[7](i=6) 0 5 1 6 6 4 7 7 0 7
bottom -> top a[7] & a[8](i=7) swap 0 5 1 6 6 4 7 0 7 7
bottom -> top a[8] & a[9](i=8) 0 5 1 6 6 4 7 0 7 7

2num=8 top=8 bottom=0
top -> bottom a[8] & a[7](i=8) 0 5 1 6 6 4 7 0 7 7
top -> bottom a[7] & a[6](i=7) swap 0 5 1 6 6 4 0 7 7 7
top -> bottom a[6] & a[5](i=6) swap 0 5 1 6 6 0 4 7 7 7
top -> bottom a[5] & a[4](i=5) swap 0 5 1 6 0 6 4 7 7 7
top -> bottom a[4] & a[3](i=4) swap 0 5 1 0 6 6 4 7 7 7
top -> bottom a[3] & a[2](i=3) swap 0 5 0 1 6 6 4 7 7 7
top -> bottom a[2] & a[1](i=2) swap 0 0 5 1 6 6 4 7 7 7
top -> bottom a[1] & a[0](i=1) 0 0 5 1 6 6 4 7 7 7

1num=7 top=8 bottom=1
bottom -> top a[1] & a[2](i=1) 0 0 5 1 6 6 4 7 7 7
bottom -> top a[2] & a[3](i=2) swap 0 0 1 5 6 6 4 7 7 7
bottom -> top a[3] & a[4](i=3) 0 0 1 5 6 6 4 7 7 7
bottom -> top a[4] & a[5](i=4) 0 0 1 5 6 6 4 7 7 7
bottom -> top a[5] & a[6](i=5) swap 0 0 1 5 6 4 6 7 7 7
bottom -> top a[6] & a[7](i=6) 0 0 1 5 6 4 6 7 7 7
bottom -> top a[7] & a[8](i=7) 0 0 1 5 6 4 6 7 7 7

2num=6 top=7 bottom=1
top -> bottom a[7] & a[6](i=7) 0 0 1 5 6 4 6 7 7 7
top -> bottom a[6] & a[5](i=6) 0 0 1 5 6 4 6 7 7 7
top -> bottom a[5] & a[4](i=5) swap 0 0 1 5 4 6 6 7 7 7
top -> bottom a[4] & a[3](i=4) swap 0 0 1 4 5 6 6 7 7 7
top -> bottom a[3] & a[2](i=3) 0 0 1 4 5 6 6 7 7 7
top -> bottom a[2] & a[1](i=2) 0 0 1 4 5 6 6 7 7 7

1num=5 top=7 bottom=2
bottom -> top a[2] & a[3](i=2) 0 0 1 4 5 6 6 7 7 7
bottom -> top a[3] & a[4](i=3) 0 0 1 4 5 6 6 7 7 7
bottom -> top a[4] & a[5](i=4) 0 0 1 4 5 6 6 7 7 7
bottom -> top a[5] & a[6](i=5) 0 0 1 4 5 6 6 7 7 7
bottom -> top a[6] & a[7](i=6) 0 0 1 4 5 6 6 7 7 7

2num=4 top=6 bottom=2
top -> bottom a[6] & a[5](i=6) 0 0 1 4 5 6 6 7 7 7
top -> bottom a[5] & a[4](i=5) 0 0 1 4 5 6 6 7 7 7
top -> bottom a[4] & a[3](i=4) 0 0 1 4 5 6 6 7 7 7
top -> bottom a[3] & a[2](i=3) 0 0 1 4 5 6 6 7 7 7

1num=3 top=6 bottom=3
bottom -> top a[3] & a[4](i=3) 0 0 1 4 5 6 6 7 7 7
bottom -> top a[4] & a[5](i=4) 0 0 1 4 5 6 6 7 7 7
bottom -> top a[5] & a[6](i=5) 0 0 1 4 5 6 6 7 7 7

2num=2 top=5 bottom=3
top -> bottom a[5] & a[4](i=5) 0 0 1 4 5 6 6 7 7 7
top -> bottom a[4] & a[3](i=4) 0 0 1 4 5 6 6 7 7 7

1num=1 top=5 bottom=4
bottom -> top a[4] & a[5](i=4) 0 0 1 4 5 6 6 7 7 7

2num=0 top=4 bottom=4
counter = 45

after sorted: 0 0 1 4 5 6 6 7 7 7
Hit any key to close this window...

 

源码2:此实现加了一个标志,与冒泡加标志的改进算法原理一样,但是此鸡尾酒加标志的实现并没有比冒泡加标志更优,反而当第一个for循环出现没有交换时,并没有退出循环,还是执行了第二个for循环,而冒泡加标志当遇到没有交换发生时会立马退出。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>

void print_ary( int * ary, unsigned int len )
{
    while( len-- > 0 ) {
        printf("%d ", *ary++ );    
    }
    putchar('\n');
}

void create_rondom_number(int **parray, unsigned int number)
{
    //struct timeval tpstart;//for linux
    unsigned int i =0;

    if (NULL != *parray)
    {
        return;
    }

    *parray = (int *)malloc(sizeof(unsigned int) * number);
    (void)memset(*parray, 0, sizeof(unsigned int) * number);
    
    //gettimeofday(&tpstart,NULL); //for linux

    srand(time(NULL));//standard c
    //srandom(time(NULL));//for linux
    //srandom(tpstart.tv_usec);//for linux

    for (i=0; i<number; ++i)
    {
        (*parray)[i] = rand()%number; // range is [0, number-1] //standard c
        //(*parray)[i] = random()%number; // range is [0, number-1]//for linux 
        //(*parray)[i] = random()%(b-a) + a; //range is [a, b)//for linux
        //((*parray)[i] = 1 + (int) ((double)number * rand() / (RAND_MAX + 1.0))); // [1, number]//for linux
    }
}

void cocktail_sort(int *list, unsigned int list_length)
{ 
    unsigned int bottom = 0;
    unsigned int top = list_length - 1;
    bool swapped = true; 
    int tmp;
    unsigned int i;
    unsigned int counter = 0;

    while(swapped == true) // if no elements have been swapped, then the list is sorted
    {
        swapped = false; 
	printf("\n1num=%d top=%d bottom=%d\n", top - bottom, top, bottom);
        for(i = bottom; i < top; i = i + 1)
        {
            printf("bottom -> top a[%d] & a[%d](i=%d) ", i, i+1, i);
            if(list[i] > list[i + 1])  // test whether the two elements are in the correct order
            {
                tmp = list[i];
                list[i] = list[i+1];
                list[i+1] = tmp;
                printf("swap ");
                swapped = true;
            }
            print_ary(list, list_length);
	    counter++;
        }

        // decreases top the because the element with the largest value in the unsorted
        // part of the list is now on the position top 
        top = top - 1; 
	printf("\n2num=%d top=%d bottom=%d\n", top - bottom, top, bottom);
        for(i = top; i > bottom; i = i - 1)
        {
            printf("top -> bottom a[%d] & a[%d](i=%d) ", i, i-1, i);
            if(list[i] < list[i - 1]) 
            {
                tmp = list[i];
                list[i] = list[i-1];
                list[i-1] = tmp;
                printf("swap ");
                swapped = true;
            }
            print_ary(list, list_length);
	    counter++;
        }
        // increases bottom because the element with the smallest value in the unsorted 
        // part of the list is now on the position bottom 
        bottom = bottom + 1;  
    }
    printf("counter = %d\n", counter);
}

int main( int argc, char ** argv )
{
    int *pary = NULL;

    if (argc !=2)
    {
        printf("\nUsage: %s Number\n", argv[0]); 
        return -1;
    }

    create_rondom_number(&pary, (unsigned int)atoi(argv[1]));    

    printf("\nrandom number: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );

    printf("\nStart sortting: \n");
    cocktail_sort( pary, (unsigned int)atoi(argv[1]));

    printf("\nafter sorted: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );
    
    return 0;
}


运行结果:

C:\WINDOWS\system32\cmd.exe /c bibubble1 10

random number: 5 6 4 6 2 9 5 0 8 0

Start sortting:

1num=9 top=9 bottom=0
bottom -> top a[0] & a[1](i=0) 5 6 4 6 2 9 5 0 8 0
bottom -> top a[1] & a[2](i=1) swap 5 4 6 6 2 9 5 0 8 0
bottom -> top a[2] & a[3](i=2) 5 4 6 6 2 9 5 0 8 0
bottom -> top a[3] & a[4](i=3) swap 5 4 6 2 6 9 5 0 8 0
bottom -> top a[4] & a[5](i=4) 5 4 6 2 6 9 5 0 8 0
bottom -> top a[5] & a[6](i=5) swap 5 4 6 2 6 5 9 0 8 0
bottom -> top a[6] & a[7](i=6) swap 5 4 6 2 6 5 0 9 8 0
bottom -> top a[7] & a[8](i=7) swap 5 4 6 2 6 5 0 8 9 0
bottom -> top a[8] & a[9](i=8) swap 5 4 6 2 6 5 0 8 0 9

2num=8 top=8 bottom=0
top -> bottom a[8] & a[7](i=8) swap 5 4 6 2 6 5 0 0 8 9
top -> bottom a[7] & a[6](i=7) 5 4 6 2 6 5 0 0 8 9
top -> bottom a[6] & a[5](i=6) swap 5 4 6 2 6 0 5 0 8 9
top -> bottom a[5] & a[4](i=5) swap 5 4 6 2 0 6 5 0 8 9
top -> bottom a[4] & a[3](i=4) swap 5 4 6 0 2 6 5 0 8 9
top -> bottom a[3] & a[2](i=3) swap 5 4 0 6 2 6 5 0 8 9
top -> bottom a[2] & a[1](i=2) swap 5 0 4 6 2 6 5 0 8 9
top -> bottom a[1] & a[0](i=1) swap 0 5 4 6 2 6 5 0 8 9

1num=7 top=8 bottom=1
bottom -> top a[1] & a[2](i=1) swap 0 4 5 6 2 6 5 0 8 9
bottom -> top a[2] & a[3](i=2) 0 4 5 6 2 6 5 0 8 9
bottom -> top a[3] & a[4](i=3) swap 0 4 5 2 6 6 5 0 8 9
bottom -> top a[4] & a[5](i=4) 0 4 5 2 6 6 5 0 8 9
bottom -> top a[5] & a[6](i=5) swap 0 4 5 2 6 5 6 0 8 9
bottom -> top a[6] & a[7](i=6) swap 0 4 5 2 6 5 0 6 8 9
bottom -> top a[7] & a[8](i=7) 0 4 5 2 6 5 0 6 8 9

2num=6 top=7 bottom=1
top -> bottom a[7] & a[6](i=7) 0 4 5 2 6 5 0 6 8 9
top -> bottom a[6] & a[5](i=6) swap 0 4 5 2 6 0 5 6 8 9
top -> bottom a[5] & a[4](i=5) swap 0 4 5 2 0 6 5 6 8 9
top -> bottom a[4] & a[3](i=4) swap 0 4 5 0 2 6 5 6 8 9
top -> bottom a[3] & a[2](i=3) swap 0 4 0 5 2 6 5 6 8 9
top -> bottom a[2] & a[1](i=2) swap 0 0 4 5 2 6 5 6 8 9

1num=5 top=7 bottom=2
bottom -> top a[2] & a[3](i=2) 0 0 4 5 2 6 5 6 8 9
bottom -> top a[3] & a[4](i=3) swap 0 0 4 2 5 6 5 6 8 9
bottom -> top a[4] & a[5](i=4) 0 0 4 2 5 6 5 6 8 9
bottom -> top a[5] & a[6](i=5) swap 0 0 4 2 5 5 6 6 8 9
bottom -> top a[6] & a[7](i=6) 0 0 4 2 5 5 6 6 8 9

2num=4 top=6 bottom=2
top -> bottom a[6] & a[5](i=6) 0 0 4 2 5 5 6 6 8 9
top -> bottom a[5] & a[4](i=5) 0 0 4 2 5 5 6 6 8 9
top -> bottom a[4] & a[3](i=4) 0 0 4 2 5 5 6 6 8 9
top -> bottom a[3] & a[2](i=3) swap 0 0 2 4 5 5 6 6 8 9

1num=3 top=6 bottom=3
bottom -> top a[3] & a[4](i=3) 0 0 2 4 5 5 6 6 8 9
bottom -> top a[4] & a[5](i=4) 0 0 2 4 5 5 6 6 8 9
bottom -> top a[5] & a[6](i=5) 0 0 2 4 5 5 6 6 8 9

2num=2 top=5 bottom=3
top -> bottom a[5] & a[4](i=5) 0 0 2 4 5 5 6 6 8 9
top -> bottom a[4] & a[3](i=4) 0 0 2 4 5 5 6 6 8 9
counter = 44

after sorted: 0 0 2 4 5 5 6 6 8 9
Hit any key to close this window...

 

源码3:此实现是最优的实现,在加了交换标志的同时,增加了一个边界标志,此边界标志记录了最后一次进行交换的元素位置,表明此位置之前或者之后的所有元素都已经排好序了,不需要再进行排序了,所以在某些情况下可以减少循环的次数。鸡尾酒算法是两头可以加边界标志,而冒泡算法只能在一边加标志,所以边界标志在加在鸡尾酒算法中比在冒泡算法中效率高一倍

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>

void print_ary( int * ary, unsigned int len )
{
    while( len-- > 0 ) {
        printf("%d ", *ary++ );    
    }
    putchar('\n');
}

void create_rondom_number(int **parray, unsigned int number)
{
    //struct timeval tpstart;//for linux
    unsigned int i =0;

    if (NULL != *parray)
    {
        return;
    }

    *parray = (int *)malloc(sizeof(unsigned int) * number);
    (void)memset(*parray, 0, sizeof(unsigned int) * number);
    
    //gettimeofday(&tpstart,NULL); //for linux

    srand(time(NULL));//standard c
    //srandom(time(NULL));//for linux
    //srandom(tpstart.tv_usec);//for linux

    for (i=0; i<number; ++i)
    {
        (*parray)[i] = rand()%number; // range is [0, number-1] //standard c
        //(*parray)[i] = random()%number; // range is [0, number-1]//for linux 
        //(*parray)[i] = random()%(b-a) + a; //range is [a, b)//for linux
        //((*parray)[i] = 1 + (int) ((double)number * rand() / (RAND_MAX + 1.0))); // [1, number]//for linux
    }
}

void cocktail_sort(int *list, unsigned int list_length)
{ 
    unsigned int bottom = 0;
    unsigned int top = list_length - 1;
    bool swapped = true; 
    int tmp;
    unsigned int bounder = 0;
    unsigned int i;
    unsigned int counter = 0;

    while(swapped == true) // if no elements have been swapped, then the list is sorted
    {
        swapped = false; 
	printf("\n1num=%d top=%d bottom=%d\n", top - bottom, top, bottom);
        for(i = bottom; i < top; i = i + 1)
        {
            printf("bottom -> top a[%d] & a[%d](i=%d) ", i, i+1, i);
            if(list[i] > list[i + 1])  // test whether the two elements are in the correct order
            {
                tmp = list[i];
                list[i] = list[i+1];
                list[i+1] = tmp;
                printf("swap ");
                swapped = true;
		bounder = i;
            }
            print_ary(list, list_length);
	    counter++;
        }

        // decreases top the because the element with the largest value in the unsorted
        // part of the list is now on the position top 
        top = bounder; 
	printf("\n2num=%d top=%d bottom=%d\n", top - bottom, top, bottom);
        for(i = top; i > bottom; i = i - 1)
        {
            printf("top -> bottom a[%d] & a[%d](i=%d) ", i, i-1, i);
            if(list[i] < list[i - 1]) 
            {
                tmp = list[i];
                list[i] = list[i-1];
                list[i-1] = tmp;
                printf("swap ");
                swapped = true;
		bounder = i;
            }
            print_ary(list, list_length);
	    counter++;
        }
        // increases bottom because the element with the smallest value in the unsorted 
        // part of the list is now on the position bottom 
        bottom = bounder;  
    }
    printf("counter = %d\n", counter);
}

int main( int argc, char ** argv )
{
    int *pary = NULL;

    if (argc !=2)
    {
        printf("\nUsage: %s Number\n", argv[0]); 
        return -1;
    }

    create_rondom_number(&pary, (unsigned int)atoi(argv[1]));    

    printf("\nrandom number: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );

    printf("\nStart sortting: \n");
    cocktail_sort( pary, (unsigned int)atoi(argv[1]));

    printf("\nafter sorted: ");
    print_ary( pary, (unsigned int)atoi(argv[1]) );
    
    return 0;
}


 

运行结果:

C:\WINDOWS\system32\cmd.exe /c bibubble2 10

random number: 7 8 5 1 6 3 0 9 0 5

Start sortting:

1num=9 top=9 bottom=0
bottom -> top a[0] & a[1](i=0) 7 8 5 1 6 3 0 9 0 5
bottom -> top a[1] & a[2](i=1) swap 7 5 8 1 6 3 0 9 0 5
bottom -> top a[2] & a[3](i=2) swap 7 5 1 8 6 3 0 9 0 5
bottom -> top a[3] & a[4](i=3) swap 7 5 1 6 8 3 0 9 0 5
bottom -> top a[4] & a[5](i=4) swap 7 5 1 6 3 8 0 9 0 5
bottom -> top a[5] & a[6](i=5) swap 7 5 1 6 3 0 8 9 0 5
bottom -> top a[6] & a[7](i=6) 7 5 1 6 3 0 8 9 0 5
bottom -> top a[7] & a[8](i=7) swap 7 5 1 6 3 0 8 0 9 5
bottom -> top a[8] & a[9](i=8) swap 7 5 1 6 3 0 8 0 5 9

2num=8 top=8 bottom=0
top -> bottom a[8] & a[7](i=8) 7 5 1 6 3 0 8 0 5 9
top -> bottom a[7] & a[6](i=7) swap 7 5 1 6 3 0 0 8 5 9
top -> bottom a[6] & a[5](i=6) 7 5 1 6 3 0 0 8 5 9
top -> bottom a[5] & a[4](i=5) swap 7 5 1 6 0 3 0 8 5 9
top -> bottom a[4] & a[3](i=4) swap 7 5 1 0 6 3 0 8 5 9
top -> bottom a[3] & a[2](i=3) swap 7 5 0 1 6 3 0 8 5 9
top -> bottom a[2] & a[1](i=2) swap 7 0 5 1 6 3 0 8 5 9
top -> bottom a[1] & a[0](i=1) swap 0 7 5 1 6 3 0 8 5 9

1num=7 top=8 bottom=1
bottom -> top a[1] & a[2](i=1) swap 0 5 7 1 6 3 0 8 5 9
bottom -> top a[2] & a[3](i=2) swap 0 5 1 7 6 3 0 8 5 9
bottom -> top a[3] & a[4](i=3) swap 0 5 1 6 7 3 0 8 5 9
bottom -> top a[4] & a[5](i=4) swap 0 5 1 6 3 7 0 8 5 9
bottom -> top a[5] & a[6](i=5) swap 0 5 1 6 3 0 7 8 5 9
bottom -> top a[6] & a[7](i=6) 0 5 1 6 3 0 7 8 5 9
bottom -> top a[7] & a[8](i=7) swap 0 5 1 6 3 0 7 5 8 9

2num=6 top=7 bottom=1
top -> bottom a[7] & a[6](i=7) swap 0 5 1 6 3 0 5 7 8 9
top -> bottom a[6] & a[5](i=6) 0 5 1 6 3 0 5 7 8 9
top -> bottom a[5] & a[4](i=5) swap 0 5 1 6 0 3 5 7 8 9
top -> bottom a[4] & a[3](i=4) swap 0 5 1 0 6 3 5 7 8 9
top -> bottom a[3] & a[2](i=3) swap 0 5 0 1 6 3 5 7 8 9
top -> bottom a[2] & a[1](i=2) swap 0 0 5 1 6 3 5 7 8 9

1num=5 top=7 bottom=2
bottom -> top a[2] & a[3](i=2) swap 0 0 1 5 6 3 5 7 8 9
bottom -> top a[3] & a[4](i=3) 0 0 1 5 6 3 5 7 8 9
bottom -> top a[4] & a[5](i=4) swap 0 0 1 5 3 6 5 7 8 9
bottom -> top a[5] & a[6](i=5) swap 0 0 1 5 3 5 6 7 8 9
bottom -> top a[6] & a[7](i=6) 0 0 1 5 3 5 6 7 8 9

2num=3 top=5 bottom=2
top -> bottom a[5] & a[4](i=5) 0 0 1 5 3 5 6 7 8 9
top -> bottom a[4] & a[3](i=4) swap 0 0 1 3 5 5 6 7 8 9
top -> bottom a[3] & a[2](i=3) 0 0 1 3 5 5 6 7 8 9

1num=1 top=5 bottom=4
bottom -> top a[4] & a[5](i=4) 0 0 1 3 5 5 6 7 8 9

2num=0 top=4 bottom=4
counter = 39

after sorted: 0 0 1 3 5 5 6 7 8 9
Hit any key to close this window...

 

参考:

http://baike.baidu.com/view/1981861.htm

http://zh.wikipedia.org/wiki/%E9%B8%A1%E5%B0%BE%E9%85%92%E6%8E%92%E5%BA%8F

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值