c - 选择排序算法 (C)
问题描述
我试图研究一些算法问题,但我找不到开始我的选择排序算法代码的方法。我使用 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]);
}
}
解决方案
In the C language, arrays start indexing at 0, and the last element is at the index n-1. So the first thing you should change is the loops to be for(i = 0; i < n-1; i++)
and for(j = i + 1; j < n; j++)
Also, this is a bubble sort algorithm, not selection sort. Selection sort algorithms can be found anywhere so I'm not going to type it here.
推荐阅读
- javascript - 使用动态字符串附加 JSON 对象键
- javascript - 获得响应后如何刷新加载 iFrame 的页面
- javascript - 使用 Javascript 将来自 api 的 rgb 数据作为背景应用到 html 元素
- google-cloud-vision - Cloud Vision API 服务突然变慢
- c++ - 为什么不启动子循环的迭代器使其在一个父循环完成后停止?
- sql - 如何运行存储在 Redshift 表中的 SQL 查询
- c - 随机函数中的枚举
- javascript - 画布在 html 脚本中没有响应
- python - 根据 pandas df 中的两列替换值
- vue.js - TypeError:无法读取未定义的属性“$http” - 类星体