简体   繁体   English

冒泡排序的 C++ 问题

[英]C++ issue with bubble sort

I am attempting to use bubble sort in C++ which I am new to.我正在尝试在我不熟悉的 C++ 中使用冒泡排序。 This is my code so far:到目前为止,这是我的代码:

My issue is that when ever I try and sort the arrays, the integer array is set to zero at every index.我的问题是,当我尝试对数组进行排序时,整数数组在每个索引处都设置为零。

Could this have something to do with memory allocation?这可能与内存分配有关吗?

#include <iostream>
#include <cstring>
#include <array>

using namespace std;

int main() {
    //requires users input to create an array that wouldn't use too much memory
    cout << "Enter number of dealers you wish to enter" << endl;
    int array_size;
    cin >> array_size;
    std::string sales_names[array_size]; //makes a 2D array that can have 15 rows and 2 colums {Name, sales total}
    int sales_data[array_size];

    for(int i = 0; i < array_size; i++){
        std::string dealers_name;
        int dealers_value;
        cout << "Enter Dealers Name: ";
        cin >> dealers_name;
        cin.clear();
        cout << "Enter Dealers Sales: ";
        cin >> dealers_value;
        sales_names[i] = dealers_name;
        sales_data[i] = dealers_value;
        cout << endl;
    }

    string temp_name;
    int temp_value;

    //Bubble Sort?
    for(int i = 0; i < array_size; i++){
        for(int k = 0; k < array_size; k++){
            if(sales_data[k] = sales_data[k+1])
            {
                temp_value = sales_data[k];
                temp_name = sales_names[k];

                sales_data[k] = sales_data[k+1];
                sales_names[k] = sales_names[k+1];

                sales_data[k+1] = temp_value;
                sales_names[k+1] = temp_name;
            }
        }
    }

    for(int i = 0; i < array_size; i++){
        cout << sales_data[i] << endl;
        cout << sales_names[i] << endl;
    }

    return 0;
}

Im not sure why, but after the first loop of the for loop, every item in the sales_data array is set to 0我不知道为什么,但是在 for 循环的第一个循环之后,sales_data 数组中的每个项目都设置为 0

Here is my output:这是我的输出:

Enter number of dealers you wish to enter
3
Enter Dealers Name:test1
 Enter Dealers Sales:12

Enter Dealers Name:test2
 Enter Dealers Sales:6

Enter Dealers Name:test3
 Enter Dealers Sales:9

0
test3
0
test2
0
test1

Thanks in advance提前致谢

You have two obvious problems.你有两个明显的问题。

In

for(int i = 0; i < array_size; i++){
    for(int k = 0; k < array_size; k++){
        if(sales_data[k] = sales_data[k+1])

you are assigning ( = ) instead of comparing ( == ).您正在分配 ( = ) 而不是比较 ( == )。 Though there's no point in a bubble if they match.尽管如果它们匹配,泡沫就没有意义。 Perhaps you want less than?也许你想要的少于?

Also, watch out for running up to k , and inspecting k+1另外,注意跑到k ,并检查k+1

There are multiple issues with your code:您的代码存在多个问题:

  • sales_data[k] = sales_data[k+1] should be sales_data[k] == sales_data[k+1] sales_data[k] = sales_data[k+1]应该是sales_data[k] == sales_data[k+1]

  • for(int k = 0; k < array_size; k++) loop should start from i+1 for a bubble sort or you can change the condition to k < array_size-i-1 for(int k = 0; k < array_size; k++)循环应该从i+1开始进行冒泡排序,或者您可以将条件更改为k < array_size-i-1

Your comparison is wrong:你的比较是错误的:

if (sales_data[k] = sales_data[k + 1])

This should be a < (or a > , depending on if you want to sort ascending or descending).这应该是一个< (或一个> ,取决于您是想升序还是降序)。 Even if you wanted to test for equality, == would have been right, not = .即使您想测试相等性, ==也是正确的,而不是= Also here也在这里

for (int k = 0; k < array_size-1; k++) {

You should put array_size-1 , otherwise it's going out of bounds.你应该把array_size-1 ,否则它会越界。

Also note that VLAs aren't actually part of standard C++.另请注意,VLA 实际上并不是标准 C++ 的一部分。 Use std::vector instead:使用std::vector代替:

std::vector<std::string> sales_names(array_size);
std::vector<int> sales_data(array_size);

By the way, that comment about it making a 2D array is wrong.顺便说一句,关于它制作二维数组的评论是错误的。

2 changes/questions: 2个变化/问题:

  1. Why are you swapping when data is same ?当数据相同时为什么要交换? You only need to swap if i < k and arr[i] > arr[k].你只需要在 i < k 和 arr[i] > arr[k] 时交换。

  2. You are going out of bounds when accessing k+1 for inner loop, it should be k内部循环访问k+1时越界,应该是k

. .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM