有人可以向我解释一下这种分类算法如何工作,该算法的名称是什么?
我创建了一个排序功能。我可以知道该算法的名称吗?这是泡沫吗?
我是新来的。
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不是 bubble sort 。它不会比较相邻元素,也不会实施任何检查,以停止遍历数组。
它类似于 Exchange sort ,可以像以下差异一样实现(请注意差异,差异,但是):
我认为发布的算法与 insertion Sort 。看看它如何变形数组:
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):
I'd argue that the posted algorithm is much more similar to insertion sort. See how it transforms the array:
我认为它没有名字,但在本文中进行了描述:这是最简单的(也是最令人惊讶的(也是最令人惊讶的) )分类算法吗?
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