气泡排序无法正确排序
我试图对这个随机生成的数组进行分类,但是我的输出看起来像这样:
sorted round: 0: 87
sorted round: 0: 78
sorted round: 0: 16
sorted round: 0: 94
sorted round: 0: 36
sorted round: 0: 93
sorted round: 0: 50
sorted round: 0: 22
sorted round: 0: 63
sorted round: 0: 28
sorted round: 0: 91
sorted round: 0: 60
sorted round: 0: 64
sorted round: 0: 27
sorted round: 0: 41
sorted round: 0: 73
sorted round: 0: 37
sorted round: 0: 12
sorted round: 0: 69
84
78
16
87
36
93
50
22
63
28
91
60
64
27
41
73
37
12
69 0
我已经在这个数小时了,并且能够弄清楚我在做什么错,任何帮助都将受到赞赏。
#include <stdio.h>
#include <iostream>
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
using namespace std;
int main()
{
int random_array[20];
int i;
int j;
random_array[0]=rand()%(100-1+1)+1;
for (i=1; i<20;)
{
j= rand()%(100-1+1)+1;
bool exists;
exists = find(begin(random_array), end(random_array), j) != end(random_array);
if(!exists)
{
random_array[i]=j;
i=i+1;
}
else
{
;
}
}
int size=20;
i=0;
j=0;
int k =0;
for (i; i < size; i++) {
for (j; j < size - i; j++)
{
for(k; k<20; k++)
{
cout<<"sorted round: "<<j<<": "<<random_array[k]<<endl;
}
if (random_array[j] > random_array[j + 1]) {
swap(random_array[j], random_array[j + 1]);
}
else
{
;
}
}
}
for (i=0; i<20;i++)
{
cout<<random_array[i]<<endl;
}
return 0;
}
输出阵容应该是一个称为Random_Array的排序阵列,并且IM在以下代码上启动的问题:
int size=20;
i=0;
j=0;
int k =0;
for (i; i < size; i++) {
Im trying to sort this random generated array, but my output isincorrect it looks like this:
sorted round: 0: 87
sorted round: 0: 78
sorted round: 0: 16
sorted round: 0: 94
sorted round: 0: 36
sorted round: 0: 93
sorted round: 0: 50
sorted round: 0: 22
sorted round: 0: 63
sorted round: 0: 28
sorted round: 0: 91
sorted round: 0: 60
sorted round: 0: 64
sorted round: 0: 27
sorted round: 0: 41
sorted round: 0: 73
sorted round: 0: 37
sorted round: 0: 12
sorted round: 0: 69
84
78
16
87
36
93
50
22
63
28
91
60
64
27
41
73
37
12
69 0
ive been at this for hours and havnt been able to figure out what im doing wrong, any help is appreciated.
#include <stdio.h>
#include <iostream>
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
using namespace std;
int main()
{
int random_array[20];
int i;
int j;
random_array[0]=rand()%(100-1+1)+1;
for (i=1; i<20;)
{
j= rand()%(100-1+1)+1;
bool exists;
exists = find(begin(random_array), end(random_array), j) != end(random_array);
if(!exists)
{
random_array[i]=j;
i=i+1;
}
else
{
;
}
}
int size=20;
i=0;
j=0;
int k =0;
for (i; i < size; i++) {
for (j; j < size - i; j++)
{
for(k; k<20; k++)
{
cout<<"sorted round: "<<j<<": "<<random_array[k]<<endl;
}
if (random_array[j] > random_array[j + 1]) {
swap(random_array[j], random_array[j + 1]);
}
else
{
;
}
}
}
for (i=0; i<20;i++)
{
cout<<random_array[i]<<endl;
}
return 0;
}
The out put should be a sorted array called random_array and the issue im running into starts on this line of code:
int size=20;
i=0;
j=0;
int k =0;
for (i; i < size; i++) {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这些嵌套的循环中,
是变量
k
在最外部循环的每次迭代中重置为0变量
J
也不 在最外部循环的第一次迭代中,未定义的行为是i
等于0
,因为在此中,如果语句express
rando> rando> rando> rando> rando> rando> rando> /code>当j等于
大小-1
时,访问数组以外的内存。最好以1
和if语句写入内部循环启动循环
In these nested for loops
neither variable
j
nor the variablek
are reset to 0 in each iteration of the most outer loopAt least you should write
Pay attention to that the program invokes undefined behavior in the first iteration of the most outer loop that is when
i
is equal to0
because in this if statementthe expression
random_array[j + 1]
accesses memory beyond the array when j is equalsize - 1
.It is better to start the inner for loop with 1
and in if statement to write