尝试在 C 编程中对数组进行排序时获取未知值
我正在使用 VS 代码。我正在使用冒泡排序方法对 C 中的数组进行排序。它似乎排序正确,但我在最后得到的数字 4 不在数组中。这是代码和输出。
#include <stdio.h>
int bubble_sort(int nums[], int size);
int main() {
int nums[] = { 101, 201, 111, 23, 41, 33, 1 };
int size = sizeof(nums) / sizeof(int);
int buble = bubble_sort(nums, size);
printf("%d\n", buble);
}
// defining bubble sort
int bubble_sort(int nums[], int size) {
for (int a = 0; a < size - 1; a++) {
for (int b = 0; b < size - a - 1; b++) {
//swapping
if (nums[b] > nums[b + 1]) {
nums[b] = nums[b] ^ nums[b + 1];
nums[b + 1] = nums[b] ^ nums[b + 1];
nums[b] = nums[b] ^ nums[b + 1];
}
}
}
for (int i = 0; i < 7; i++) {
printf("%d\n", nums[i]);
}
}
我得到的输出:
1
23
33
41
101
111
201
4
这是我得到的输出。在这里,请参阅最后一个数字。它不在数组中。我不知道这个号码是从哪里来的。我尝试过调试,但它来自某个地方。谁能告诉我从哪里以及为什么得到这个号码。
I am using VS code. I was sorting an array in c using bubble sort method. It seems to sort right, But I am getting number 4 at the end which is not in the array. here is the code and output.
#include <stdio.h>
int bubble_sort(int nums[], int size);
int main() {
int nums[] = { 101, 201, 111, 23, 41, 33, 1 };
int size = sizeof(nums) / sizeof(int);
int buble = bubble_sort(nums, size);
printf("%d\n", buble);
}
// defining bubble sort
int bubble_sort(int nums[], int size) {
for (int a = 0; a < size - 1; a++) {
for (int b = 0; b < size - a - 1; b++) {
//swapping
if (nums[b] > nums[b + 1]) {
nums[b] = nums[b] ^ nums[b + 1];
nums[b + 1] = nums[b] ^ nums[b + 1];
nums[b] = nums[b] ^ nums[b + 1];
}
}
}
for (int i = 0; i < 7; i++) {
printf("%d\n", nums[i]);
}
}
output I am getting:
1
23
33
41
101
111
201
4
This is the output I am getting. Here, see the last number. It is not there in the array. I have no idea where this number is from. I tried debugging but it is coming from somewhere. Could anyone tell me from where and Why I am getting this number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这条线没有意义。
您正在打印
bubble_sort
函数的返回值,但它没有return
语句,因此您不知道它将返回什么。您应该使用
-Wall
选项进行编译。如果这样做,您会收到有关您做错的事情的警告,包括以下警告:它说“您有一个想要返回值的函数,但没有
return
语句。”This line doesn't make sense.
You're printing the return value of the
bubble_sort
function, but it doesn't have areturn
statement, so you don't know what that is going to return.You should be compiling with the
-Wall
option. If you do, you'll get warnings about things you're doing wrong, including this one:It's saying "You have a function that wants to return a value, and you have no
return
statement."您正在使用
buble_sort
函数内的循环在屏幕上打印输出。您不必将返回类型设置为 int 或将其返回值存储在新变量中,这将占用额外的内存空间。由于您的函数没有返回值,并且您已将返回类型原型化为
int
,因此它可能会打印int
的大小(即 4 个字节)。我建议您将以下行更改为:
行
并删除以下
You're using loop inside
buble_sort
function to print the output on the screen. You don't have to set return type toint
or store its return value in a new variable which will take extra space in memory.Since your function does not have a return value and you've prototyped return type as
int
, it is possibly printing the size ofint
(which is 4 byte).I suggest you to change below lines :
to this
and delete below