气泡排序无法正确排序

发布于 2025-02-07 07:34:58 字数 1978 浏览 1 评论 0原文

我试图对这个随机生成的数组进行分类,但是我的输出看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2025-02-14 07:34:59

在这些嵌套的循环中,

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
        {
            ;
        }
        
       
    }
}

是变量k在最外部循环的每次迭代中重置为0

for (i; i < size; i++) {

变量

for (i = 0; i < size; i++) {
    for (j = 0; j < size - i; j++) 
    {
        for(k = 0; 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
        {
            ;
        }
        
       
    }
}

J也不 在最外部循环的第一次迭代中,未定义的行为是i等于0,因为在此中,如果语句

        if (random_array[j] > random_array[j + 1]) {

express rando> rando> rando> rando> rando> rando> rando> /code>当j等于大小-1时,访问数组以外的内存。

最好以1

    for (j = 1; j < size - i; j++) 
    {

和if语句写入内部循环启动循环

        if (random_array[j-1] > random_array[j]) {

In these nested for loops

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
        {
            ;
        }
        
       
    }
}

neither variable j nor the variable k are reset to 0 in each iteration of the most outer loop

for (i; i < size; i++) {

At least you should write

for (i = 0; i < size; i++) {
    for (j = 0; j < size - i; j++) 
    {
        for(k = 0; 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
        {
            ;
        }
        
       
    }
}

Pay attention to that the program invokes undefined behavior in the first iteration of the most outer loop that is when i is equal to 0 because in this if statement

        if (random_array[j] > random_array[j + 1]) {

the expression random_array[j + 1] accesses memory beyond the array when j is equal size - 1.

It is better to start the inner for loop with 1

    for (j = 1; j < size - i; j++) 
    {

and in if statement to write

        if (random_array[j-1] > random_array[j]) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文