0%

简单选择排序

简单选择排序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// primeselect.c

#include <stddef.h>
#include <stdio.h>

void primeselect_sort(int *arr, size_t arrsiz)
{
printf("\n"); // 避免同一行刷新排序进度时覆盖其他程序输出
for (int ind = 0; ind < arrsiz; ind++) {
int max_ind = ind;
for (int jnd = ind + 1; jnd < arrsiz; jnd++) {
if (arr[jnd] > arr[max_ind]) {
max_ind = jnd;
}
}
int swap_val = arr[max_ind];
arr[max_ind] = arr[ind];
arr[ind] = swap_val;
printf("\r\033[k");
printf("Progress: %d / %ld", ind, arrsiz);
}
printf("\r\033[k"); // 同一行刷新排序进度
printf(" ");
printf("\r\033[k"); // 清空刷新内容,避免干扰后续输出
}

效率测试框架

插入排序 类似

测试结果

>>> PRIEM SELECT 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: [ 9, 7, 6, 6, 5, 5, 3, 3, 2, 1]
[SORT array_size=100]
        gen_time: 0
        sort_time: 0
[SORT array_size=10000]
        gen_time: 0
        sort_time: 0
[SORT array_size=100000]
        gen_time: 0
        sort_time: 13
[SORT array_size=1000000]
        gen_time: 0
        sort_time: 1354