试图理解冒泡排序功能
Trying to understand bubblesort function
我对 C 和一般编程还很陌生,我对以下冒泡排序函数中的循环有疑问:
void sort(int values[], int n);
{
int c, d, t;
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < c - n - 1; d++)
{
if (values[d] > values[d + 1])
{
t = values[d];
values[d] = values[d + 1];
values[d + 1] = t;
}
}
}
}
我特别想了解的部分是第二个 for 循环:
for (d = 0; d < c - n - 1; d++)
我知道函数每次都遍历数组,比较并排的值直到最后一个排序的元素,并将最大的元素冒泡到数组的末尾;但是,我似乎无法理解以下含义:
while d < c - n - 1;
及其翻译方式。当我在脑海中播放这个时,我在想象第一次循环时,d < c - n - 1 等于 0 < 0 - 10 - 1。在我看来,因为 c 总是小于n - 1,从 c 中减去 n - 1 将始终导致小于 0 的值,并且 d 绝不会小于 0,因此永远不应执行。我知道我只是看错了,当答案出现时可能会有 'duh' 的时刻,但这让我很困扰。
如果我把第一个for循环写成的话,它说:当c小于数组长度时,执行这个循环的内容然后递增c。有人可以用类似的方式把第二个for循环写成文字,解释d < c - n - 1的含义吗?
不要把简单的事情复杂化,而且你的代码也不起作用试试下面的代码。`
void sort(int values[], int n);
{
int c, d, t;
// since c is intitalised to 0 u can simply use n
for (c = 0; c < n; c++)
{
//Assign d = c+1 instead of doing it in next step
for (d = c+1; d < n; d++)
{
//It is ascending sort it checks first and second value ,if greater it will swap
if (values[c] > values[d])
{
t = values[c];
values[c] = values[d];
values[d] = t;
}
}
}
}`
先看看这个QA:
我的功能性冒泡排序 C++ 实现可以在其中找到,其中包含注释。
你的第一个循环应该每次都使用少一个数组大小来循环冒泡排序,因为数组中的最后一个值已经从之前的 运行.
中排序了
所以第二个循环应该在 <0,n-1-c>
范围内。 if
语句只选择排序是 asc 还是 desc 并在需要时交换元素。
就像大多数冒泡排序一样,你忘记在排序完成后停止(最后 运行 期间没有发生交换)
改用此代码。
#include<stdio.h>
void sort(int values[], int n)
{
int c,d,t=n;
//if we have n elements then outer loop must iterate for n times.
for(c=0;c<n;c++)
{
/*after every complete iteration we get a
sorted element in last position, next iteration
will be one less, so we assigned value of n into t
and we will decrease it by 1 after every internal complete iteration.
*/
for(d=0;d<t-1;d++)
{
if(values[d]>values[d+1])
{
int temp;
temp = values[d+1];
values[d+1] = values[d];
values[d] = temp;
}
}
//decrease the test cases in next iteration,
//we already have last element sorted.
t--;
}
}
int main()
{
int i;
int values[]={24,12,2,4,6};
sort(values, 5);
for(i=0; i<5; i++)
{
printf("%d\n",values[i]);
}
return 0;
}
根据需要进行更改。
我对 C 和一般编程还很陌生,我对以下冒泡排序函数中的循环有疑问:
void sort(int values[], int n);
{
int c, d, t;
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < c - n - 1; d++)
{
if (values[d] > values[d + 1])
{
t = values[d];
values[d] = values[d + 1];
values[d + 1] = t;
}
}
}
}
我特别想了解的部分是第二个 for 循环:
for (d = 0; d < c - n - 1; d++)
我知道函数每次都遍历数组,比较并排的值直到最后一个排序的元素,并将最大的元素冒泡到数组的末尾;但是,我似乎无法理解以下含义:
while d < c - n - 1;
及其翻译方式。当我在脑海中播放这个时,我在想象第一次循环时,d < c - n - 1 等于 0 < 0 - 10 - 1。在我看来,因为 c 总是小于n - 1,从 c 中减去 n - 1 将始终导致小于 0 的值,并且 d 绝不会小于 0,因此永远不应执行。我知道我只是看错了,当答案出现时可能会有 'duh' 的时刻,但这让我很困扰。
如果我把第一个for循环写成的话,它说:当c小于数组长度时,执行这个循环的内容然后递增c。有人可以用类似的方式把第二个for循环写成文字,解释d < c - n - 1的含义吗?
不要把简单的事情复杂化,而且你的代码也不起作用试试下面的代码。`
void sort(int values[], int n);
{
int c, d, t;
// since c is intitalised to 0 u can simply use n
for (c = 0; c < n; c++)
{
//Assign d = c+1 instead of doing it in next step
for (d = c+1; d < n; d++)
{
//It is ascending sort it checks first and second value ,if greater it will swap
if (values[c] > values[d])
{
t = values[c];
values[c] = values[d];
values[d] = t;
}
}
}
}`
先看看这个QA:
我的功能性冒泡排序 C++ 实现可以在其中找到,其中包含注释。
你的第一个循环应该每次都使用少一个数组大小来循环冒泡排序,因为数组中的最后一个值已经从之前的 运行.
中排序了所以第二个循环应该在 <0,n-1-c>
范围内。 if
语句只选择排序是 asc 还是 desc 并在需要时交换元素。
就像大多数冒泡排序一样,你忘记在排序完成后停止(最后 运行 期间没有发生交换)
改用此代码。
#include<stdio.h>
void sort(int values[], int n)
{
int c,d,t=n;
//if we have n elements then outer loop must iterate for n times.
for(c=0;c<n;c++)
{
/*after every complete iteration we get a
sorted element in last position, next iteration
will be one less, so we assigned value of n into t
and we will decrease it by 1 after every internal complete iteration.
*/
for(d=0;d<t-1;d++)
{
if(values[d]>values[d+1])
{
int temp;
temp = values[d+1];
values[d+1] = values[d];
values[d] = temp;
}
}
//decrease the test cases in next iteration,
//we already have last element sorted.
t--;
}
}
int main()
{
int i;
int values[]={24,12,2,4,6};
sort(values, 5);
for(i=0; i<5; i++)
{
printf("%d\n",values[i]);
}
return 0;
}
根据需要进行更改。