c - 选择排序算法(C)

标签 c algorithm sorting select compilation

我正在尝试研究一些算法问题,但找不到启动我的选择排序算法代码的方法。我使用 Visual Studio,代码在编译时没有显示任何错误。当我启动代码时,系统只会停止几秒钟并打印“按任意键继续...”。我是初学者,我看不出有什么问题。帮助我吧

#include <stdio.h>

int n = 6;

int qwerty(int a[]) {
    int i, j, t;
    for (i=1; i<=n-1; i++){
        for (j=i+1; j<=n; j++){
            if (a[i] > a[j]){
                t = a[j]; 
                a[j] = a[i]; 
                a[i] = t;
            }
        }
    }
}

int main(void) {
    int a[6] = { 2, 14, 20, 8, 17, 13 };
    qwerty(a[n]);
    int i;
    for (i = 1; i <= n; i++){
        printf("%d ", a[i]);
    }
}

最佳答案

在 C 语言中,数组从 0 开始索引,最后一个元素位于索引 n-1 处。因此,您应该更改的第一件事是将循环更改为 for(i = 0; i < n-1; i++)for(j = i + 1; j < n; j++)

此外,这是冒泡排序算法,而不是选择排序。选择排序算法可以在任何地方找到,因此我不会在这里键入它。

关于c - 选择排序算法(C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51679358/

相关文章:

C中常量指针的困惑

用于 BFS 遍历的 C++ Lambda 函数

c# - 将整数列表移动到列表的前面

ruby - 用于配对两个数组元素的快速 Ruby 方法/算法

angular - 如何从 typescript 中的对象中删除除某些属性之外的所有属性

python - python 中是否有与 MATLAB 函数 bsxfun 等效的函数?

c - ORA-01008 : not all variables bound (in C w OCI)

algorithm - 遗传算法收敛的合理位串大小

c++ - 这个递归函数的运行时间是多少?

c - 多客户端聊天程序: send messages to all Clients