Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

sorting - I am trying to sort an array with a method similar to selection sort but my code is not working

I was trying to find an alternative for the selection sort algorithm and wrote this. It's not working and I don't know why.

int main()
    {
        int size;
        cin>>size;
    
        int arr[size];
        for(int i=0; i<size; i++)
        {
            cin>>arr[i];
        }
    
        int start=0;
        int getAdd;
    
        while(start<size)
        {
            int minEl = arr[start];
    
            for (int i =start; i < size; i++)
            {
                if(arr[i]<minEl)
                {   minEl=arr[i];
                    getAdd=i;
                }
            }
    
            swap( arr[start],arr[getAdd] );
            start=start+1;
    }

This code works just fine for a size equal to 6. For a size greater than 6, it malfunctioning a little. For a size less than 6 god knows what it is.

question from:https://stackoverflow.com/questions/65872255/i-am-trying-to-sort-an-array-with-a-method-similar-to-selection-sort-but-my-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason is that you're not giving getAdd a new value every iteration of the while loop.

This means that the for-loop inside the while loop, this one:

for (int i =start; i < size; i++)
{
    if(arr[i]<minEl)
    {   minEl=arr[i];
        getAdd=i;
    }
}

if this loop doesn't find an element that is less than minEl, which will happen in those occasions where arr[start] is already the correct element, you will re-use the previous value given to getAdd, and then swap the correctly placed element in arr[start] with another previously correctly placed element in arr[getAdd], where getAdd < start, messing up the sort order.

In the case where the first element is correctly placed, you will also probably use the uninitialized value for getAdd, which depending on your compiler and compiler settings might be an arbitrary "random" value.

This had nothing to do with 6 items by the way. My guess is that you tested it with 6 items or less that didn't have this situation. Any sized array that had no elements in the right spot already would probably be sorted correctly.

To fix this, move the declaration of getAdd into the while loop, and give it a suitable value:

int start=0;

while(start<size)
{
    int minEl = arr[start];
    int getAdd = start;

    for (int i =start; i < size; i++)

You can then also do a check before swapping:

if (start != getAdd)
    swap( arr[start],arr[getAdd] );

With the change to getAdd, you should find that your selection sort works as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...