C语言冒泡排序三种写法

#include 
/*
 @function: bubble_sort_one
 @functional: bubble sort
 @order for parameter value, 1 is ascending and 0 is descending
 */
static int bubble_sort_one(int order)
{
    int arry[] = {12, 13, 5, 3, 14, 90, 0, 11, 23, 9, 15, 99, 100, 96};
    int i = 0, j = 0;
    int temp = 0;
    int number_of_times = 0;
    int arry_length = sizeof(arry)/sizeof(int);

    for(i = 0; i < arry_length; i++)
        printf("%d ", arry[i]);
    printf("\n");

    if (order == 1) { /*ascending order*/
        for(i = 0; i < arry_length - 1; i++) {
            for(j = 0; j < arry_length -1; j++) {
                if (arry[j] > arry[j+1]) {
                    temp = arry[j];
                    arry[j] = arry[j+1];
                    arry[j+1] = temp;
                }
                number_of_times++;
            }
        }
    } else { /*descending order*/
        for(i = 0; i < arry_length - 1; i++) {
            for(j = 0; j < arry_length - 1; j++) {
                if (arry[j] < arry[j+1]) {
                    temp = arry[j];
                    arry[j] = arry[j+1];
                    arry[j+1] = temp;
                }
                number_of_times++;
            }
        }
    }

    printf("i = %d,j = %d\n",i,j);
    for(i = 0; i < arry_length; i++)
        printf("%d ", arry[i]);
    printf("\n");
    printf("bubble_sort_one: number of times = %d\n\n", number_of_times);
    return 0;
}

/*
 @function: bubble_sort_two
 @functional: bubble sort
 @order for parameter value, 1 is ascending and 0 is descending
 */
static int bubble_sort_two(int order)
{
    int arry[] = {12, 13, 5, 3, 14, 90, 0, 11, 23, 9, 15, 99, 100, 96};
    int i = 0, j = 0;
    int temp = 0;
    int number_of_times = 0;
    int arry_length = sizeof(arry)/sizeof(int);

    for(i = 0; i < arry_length; i++)
        printf("%d ", arry[i]);
    printf("\n");

    if (order == 1) { /*ascending order*/
        for(i = 0; i < arry_length - 1; i++) {
            for(j = 0; j < arry_length - (i + 1); j++) {
                if (arry[j] > arry[j+1]) {
                    temp = arry[j];
                    arry[j] = arry[j+1];
                    arry[j+1] = temp;
                }
                number_of_times++;
            }
        }
    } else { /*descending order*/
        for(i = 0; i < arry_length - 1; i++) {
            for(j = 0; j < arry_length - (i+1); j++) {
                if (arry[j] < arry[j+1]) {
                    temp = arry[j];
                    arry[j] = arry[j+1];
                    arry[j+1] = temp;
                }
                number_of_times++;
            }
        }
    }

    printf("i = %d,j = %d\n",i,j);
    for(i = 0; i < arry_length; i++)
        printf("%d ", arry[i]);
    printf("\n");
    printf("bubble_sort_two: number of times = %d\n\n", number_of_times);
    return 0;
}

/*
 @function: bubble_sort_three
 @functional: bubble sort
 @order for parameter value, 1 is ascending and 0 is descending
 */
static int bubble_sort_three(int order)
{
    int arry[] = {12, 13, 5, 3, 14, 90, 0, 11, 23, 9, 15, 99, 100, 96};
    int i = 0, j = 0;
    int temp = 0;
    int number_of_times = 0;
    int swap_flag = 0;
    int arry_length = sizeof(arry)/sizeof(int);

    for(i = 0; i < arry_length; i++)
        printf("%d ", arry[i]);
    printf("\n");

    if (order == 1) { /*ascending order*/
        for(i = 0; i < arry_length - 1; i++) {
            swap_flag = 0;
            for(j = arry_length - 1; j > i; j--) {
                if (arry[j - 1] > arry[j]) {
                    temp = arry[j - 1];
                    arry[j - 1] = arry[j];
                    arry[j] = temp;
                    swap_flag = 1;
                }
                number_of_times++;
            }
            if (!swap_flag)
                break;
        }
    } else { /*descending order*/
        for(i = 0; i < arry_length - i; i++) {
            swap_flag = 0;
            for(j = arry_length - 1; j > i; j--) {
                if (arry[j-1] < arry[j]) {
                    temp = arry[j - 1];
                    arry[j-1] = arry[j];
                    arry[j] = temp;
                    swap_flag = 1;
                }
                number_of_times++;
            }
            if (!swap_flag)
                break;
        }
    }

    printf("i = %d,j = %d\n",i,j);
    for(i = 0; i < arry_length; i++)
        printf("%d ", arry[i]);
    printf("\n");
    printf("bubble_sort_three: number of times = %d\n\n", number_of_times);
    return 0;
}

int main(void)
{
    bubble_sort_one(1);
    bubble_sort_two(1);
    bubble_sort_three(1);
    return 0;
}

gcc sort.c
./a.out
12 13 5 3 14 90 0 11 23 9 15 99 100 96
i = 13,j = 13
0 3 5 9 11 12 13 14 15 23 90 96 99 100
bubble_sort_one: number of times = 169

12 13 5 3 14 90 0 11 23 9 15 99 100 96
i = 13,j = 1
0 3 5 9 11 12 13 14 15 23 90 96 99 100
bubble_sort_two: number of times = 91

12 13 5 3 14 90 0 11 23 9 15 99 100 96
i = 5,j = 5
0 3 5 9 11 12 13 14 15 23 90 96 99 100
bubble_sort_three: number of times = 63

三种冒泡排序写法。第一种,有 n 个数据,则冒泡 n*(n-1)次。第二种,因为每次冒泡,都会确认一个当次最大或最小数值的位置,没必要每次都比较到最后,优化第一种写法,则冒泡 n*(n-1)/2 次。第三种,在第二种的基础上,进一步优化,只要确定了有一次没发生交换,就表明已经排好序了,没必要再进行下一轮的比较。第三种冒泡排序所用的时间,将进一步减少。

你可能感兴趣的:(数据结构和算法)