有人可以向我解释一下这种分类算法如何工作,该算法的名称是什么?

发布于 2025-02-03 10:11:23 字数 689 浏览 5 评论 0原文

我创建了一个排序功能。我可以知道该算法的名称吗?这是泡沫吗?

我是新来的。

#include <stdio.h>

void sort(int *, int);

int main(void) {
    int arrayNum[] = {1, 12, 8, 4, 90, 11, 76};
    int len = sizeof(arrayNum) / sizeof(arrayNum[0]);
    sort(arrayNum, len);
    for (int i = 0; i < len; i++) {
        printf("%d, ", arrayNum[i]);
    }
    
    return 0;
}

void sort(int *array, int length) {
    int temp;
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < length; j++) {
            if (array[i] < array[j]) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}

I have created a sorting function. Can I know the name of this algorithm? Is this bubble sort?

I am new in C.

#include <stdio.h>

void sort(int *, int);

int main(void) {
    int arrayNum[] = {1, 12, 8, 4, 90, 11, 76};
    int len = sizeof(arrayNum) / sizeof(arrayNum[0]);
    sort(arrayNum, len);
    for (int i = 0; i < len; i++) {
        printf("%d, ", arrayNum[i]);
    }
    
    return 0;
}

void sort(int *array, int length) {
    int temp;
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < length; j++) {
            if (array[i] < array[j]) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥︴琐丶欲为 2025-02-10 10:11:23

不,不是 bubble sort 。它不会比较相邻元素,也不会实施任何检查,以停止遍历数组。

它类似于 Exchange sort ,可以像以下差异一样实现(请注意差异,差异,但是):

for (int i = 0; i < length; i++) {
    // At every step, searches the minimum of the remaining elements     
    for (int j = i + 1; j < length; j++) {
        //       ^^^^^                          It doesn't touch the already sorted
        if ( array[j] < array[i] ) {
            //    ^^^        ^^^                To sort the array in ascending order
            /* swap array[j] and array[i] */
        }
    }
}

我认为发布的算法与 insertion Sort 。看看它如何变形数组:

Starting point
1, 12, 8, 4, 90, 11, 76

Swaps 1 with 12 and then 12 with 90.
90,   1, 8, 4, 12, 11, 76

Swaps 90 with 1.
1, 90,   8, 4, 12, 11, 76

Swaps 90 and 8. In other words, it "inserts" 8 in an already sorted partition.
1, 8, 90,   4, 12, 11, 76

Inserts 4 (it first swaps 8 and 4, then 90 and 8).
1, 4, 8, 90,   12, 11, 76

Inserts 12 (swaps 90 and 12).
1, 4, 8, 12, 90,   11, 76

Inserts 11 (swaps 12 and 11, then 90 and 11).
1, 4, 8, 11, 12, 90,   76

Inserts 76 (swaps 90 and 76), then ends.
1, 4, 8, 11, 12, 76, 90

No it's not bubble sort. It doesn't compare adjacent elements nor implements any check to stop traversing if the array becomes sorted.

It's similar to exchange sort, which could be implemented like the following (note the differences, though):

for (int i = 0; i < length; i++) {
    // At every step, searches the minimum of the remaining elements     
    for (int j = i + 1; j < length; j++) {
        //       ^^^^^                          It doesn't touch the already sorted
        if ( array[j] < array[i] ) {
            //    ^^^        ^^^                To sort the array in ascending order
            /* swap array[j] and array[i] */
        }
    }
}

I'd argue that the posted algorithm is much more similar to insertion sort. See how it transforms the array:

Starting point
1, 12, 8, 4, 90, 11, 76

Swaps 1 with 12 and then 12 with 90.
90,   1, 8, 4, 12, 11, 76

Swaps 90 with 1.
1, 90,   8, 4, 12, 11, 76

Swaps 90 and 8. In other words, it "inserts" 8 in an already sorted partition.
1, 8, 90,   4, 12, 11, 76

Inserts 4 (it first swaps 8 and 4, then 90 and 8).
1, 4, 8, 90,   12, 11, 76

Inserts 12 (swaps 90 and 12).
1, 4, 8, 12, 90,   11, 76

Inserts 11 (swaps 12 and 11, then 90 and 11).
1, 4, 8, 11, 12, 90,   76

Inserts 76 (swaps 90 and 76), then ends.
1, 4, 8, 11, 12, 76, 90
情域 2025-02-10 10:11:23

I don't think it has a name, but it's described in this paper: "Is this the simplest (and most surprising) sorting algorithm ever?" Stanley P. Y. Fung

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文