【发布时间】:2015-05-26 23:08:57
【问题描述】:
基本上是初学者的问题。我不知道如何输出我的函数的结果并检查逻辑是否正确。任何帮助都会非常感谢。
#include<stdio.h>
#include<conio.h>
#define SIZE 3
int sort(int numbers[]);//function prototype
int main (void){
int response[SIZE];
int count;
for (count=0;count<SIZE;count++)
{
printf ("Enter your number %d\n",count);
scanf ("%d",&response[count]);
}
printf ("The array when sorted is\n");
getch();
return 0;
}
//function to sort array elements
int sort(int numbers[])
{
int store;
int x;
for (x=0; x<=SIZE;x++)
{
if (numbers[x+1]<numbers[x])
{
store=numbers[x+1];
numbers[x+1]=numbers[x];
numbers[x]=store;
}
}
}
【问题讨论】:
-
for(int i=0; i
-
排序不正确。如果它确实正确排序,您将因找到
O(n)比较排序而非常出名。 -
首先要做的是在
main中实际调用sort函数。然后使用for循环迭代responses,打印数组中的每个值。但是正如已经指出的那样,您会发现您的排序不正确。即使它有效,它也不是冒泡排序实现。 -
感谢您的 cmets。我希望你能举一个代码的例子。我正在使用我知道的每一种方式,但我弄错了。感谢您让我知道排序错误。
标签: c printing bubble-sort