简体   繁体   English

我的选择排序用 0 替换了数组中的一些数字

[英]My selection sort replaces some numbers in the array with 0

This is the complete code that I wrote: ( [ nums in the function] and [ arr in main] is the array that needs to be sorted, size is the amount of numbers in the array, min is the smallest number in the unsorted part)这是我写的完整代码:([函数中的nums ]和[主中的arr ]是需要排序的数组, size是数组中的数字数量, min是未排序部分中的最小数字)

#include <iostream>
#include <vector>
using namespace std;

void sort(vector <int> &nums, int size){
    int min = 0;
    for(int i=0;i<size;i++){
        min = i;
        for(int j=i+1;j<size;j++){
            if(nums[j]<nums[min]){
                min = j;                //comparing
            }
        }
        nums[i] = nums[min] + nums[i];      //swaping
        nums[min] = nums[i] - nums[min];
        nums[i] = nums[i] - nums[min];
    }
}

int main(){
    cout<<"\nEnter Numbers:\n";
    vector <int> arr;
    int num;
    while(cin>>num){
        arr.push_back(num);
    }
    sort(arr,arr.size());
    cout<<"\nSorted:\n";
    for(int i=0;i<arr.size();i++){
        cout<<arr[i]<<" ";
    }
}

I'm writing a code that simply sorts the given array.我正在编写一个简单地对给定数组进行排序的代码。 But after trying to debug and find solutions online, I can't figure out which part is wrong.但是尝试在线调试并找到解决方案后,我无法弄清楚哪个部分是错误的。 These are some examples of my results:这些是我的结果的一些示例:

Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z

Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z

Sorted:
2 3 4 5 0 7 0
Enter Numbers:
9 8 7 6 5 4 3 2 1 ^Z

Sorted:
1 2 3 4 0 0 0 0 0

This is the result when I added a for loop under the swapping part to show what every round has done to the array:这是我在交换部分下添加了一个 for 循环以显示每一轮对数组所做的结果:

Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z

1 8 7 6 9 2 3 4 5 
1 2 7 6 9 8 3 4 5 
1 2 3 6 9 8 7 4 5 
1 2 3 4 9 8 7 6 5 
1 2 3 4 5 8 7 6 9 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 0 8 9
1 2 3 4 5 6 0 0 9
1 2 3 4 5 6 0 0 0

Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z

2 4 8 7 6 3 5
2 3 8 7 6 4 5
2 3 4 7 6 8 5
2 3 4 5 6 8 7
2 3 4 5 0 8 7
2 3 4 5 0 7 8
2 3 4 5 0 7 0

Sorted:
2 3 4 5 0 7 0
9 8 7 6 5 4 3 2 1 ^Z

1 8 7 6 5 4 3 2 9
1 2 7 6 5 4 3 8 9
1 2 3 6 5 4 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 0 6 7 8 9
1 2 3 4 0 0 7 8 9
1 2 3 4 0 0 0 8 9
1 2 3 4 0 0 0 0 9
1 2 3 4 0 0 0 0 0

Sorted:
1 2 3 4 0 0 0 0 0

Please help, thanks.请帮忙,谢谢。

Your swapping logic doesn't handle the case where the smallest remaining element is the first unsorted element (ie, i == min ).您的交换逻辑不处理最小剩余元素是第一个未排序元素(即i == min )的情况。 Consider what each line does in this case:考虑在这种情况下每一行的作用:

        nums[i] = nums[min] + nums[i];      //nums[i] will be doubled
        nums[min] = nums[i] - nums[min];    // nums[min] is subtracted from itself, making it 0
        nums[i] = nums[i] - nums[min];      // nums[i] is subtracted from itself gain, but 0-0 is still 0

The goal of avoiding a temporary isn't bad by itself, but you do have this nasty edge case.避免临时的目标本身并不坏,但你确实有这种讨厌的边缘情况。 You'd either have to detect the edge case or just bite the bullet and deal with a temporary.您要么必须检测边缘情况,要么只是硬着头皮处理临时问题。 You could also call std::swap , but that likely uses a temporary as well.您也可以调用std::swap ,但这也可能使用临时的。

The advantage to using a temporary or std::swap is that this code would be easier to make generic for other types (especially via std::swap ).使用临时或std::swap的优点是该代码更容易为其他类型生成通用代码(尤其是通过std::swap )。 In addition, std::swap can be specialized for types to avoid temporaries if possible, and if this is actually a bottleneck.此外,如果可能的话, std::swap可以专门用于类型以避免临时性,如果这实际上是一个瓶颈。

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

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