0%

冒泡排序

冒泡排序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stddef.h>
#include <stdio.h>

void bubble_sort(int *arr, size_t arrsiz)
{
printf("\n");
for (int ind = 0; ind < (int)(arrsiz) - 1; ind++) {
for (int jnd = (int)(arrsiz) - 1; jnd > ind; jnd--) {
if (arr[jnd] < arr[jnd - 1]) {
int tmp = arr[jnd];
arr[jnd] = arr[jnd - 1];
arr[jnd - 1] = tmp;
}
}
printf("\r\033[k");
printf("Progress: %d / %ld", ind, arrsiz);
}
printf("\r\033[k");
printf(" ");
printf("\r\033[k");
}

效率测试框架

插入排序 类似

测试结果

比插入和简单选择慢

>>> BUBBLE SORT >>>
[SORT array_size=10]
        gen_time: 0
                ORIGION ARRAY: [ 3, 6, 7, 5, 3, 5, 6, 2, 9, 1]
        sort_time: 0
                SORTED ARRAY: [ 1, 2, 3, 3, 5, 5, 6, 6, 7, 9]
[SORT array_size=100]
        gen_time: 0
        sort_time: 0
[SORT array_size=10000]
        gen_time: 0
        sort_time: 1
[SORT array_size=100000]
        gen_time: 0
        sort_time: 31
[SORT array_size=1000000]
        gen_time: 0
        sort_time: 3037
<<< BUBBLE SORT <<<