这段代码有什么错误 [英] what the wrong in this code

查看:69
本文介绍了这段代码有什么错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此代码不进行第二次迭代?


why this code doesn''t do the 2nd iteration?


#include <iostream>
using std::cout;
using std::endl;

void selectionSort( int * const, const int ); 
void swap( int * const, int * const ); 
int main(int argc, char *argv[])
{
    const int arraySize = 8;
    int a[ arraySize ] = { 2, 5, 12, 1, 9, 6, 3, 10 };

    selectionSort( a, arraySize ); 

     
    for ( int j = 1; j < arraySize; j++ )
        cout << a[ j ] << " ";

    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
  
void selectionSort( int * const array, const int size )
{
    
       
    int smallest; 
    int max;
    int m=0;
    int i=1;
     
    while( i < size - i && m < 2)
    {
       
        smallest = i; 
        max=i;
        
        for ( int index = i + 1; index < size-i+1; index++ )
        {
            if ( array[ index ] < array[ smallest ] )
                smallest = index;
        }
              
        for ( int x = i + 1; x < size-i+1; x++ )
        {
            if ( array[ x ] > array[ max ] )
                max = x;
        }
              
        swap( &array[ i ], &array[ smallest ] );
        swap( &array[ size-i ], &array[ max ] );
        i++;
        m++;
    }
    
      
}                       
void swap( int * const element1Ptr, int * const element2Ptr )
{                                                            
    int hold = *element1Ptr;                                  
    *element1Ptr = *element2Ptr;                              
    *element2Ptr = hold;                                      
}

推荐答案

您需要做的第一件事是整理缩进-到处都是缩进,这使得您的代码很难阅读.如果按CTRL + A将其全部选中,则CTRL + K CTRL + F Visual Studio会为您进行排序.

然后,从查看循环开始-还是没有注意到它完成后只打印了8个元素中的7个?

整理完之后,更改变量名. "i"和"m"应该做什么? "i"可以接受(只是)作为不重要的直接索引,但是您可以为此使用"index",而"i"无论如何都不能完成该工作.

查看您的代码及其大量注释,我不知道"m"的含义是什么-我知道这可能与您的问题有关,因为它将外部循环限制为两次通过-这意味着您只能交换8个值,因此,如果将10个元素传递给方法,则会浪费大量时间.

我想,您需要回到算法上,并开始研究它.
The first thing you need to do is sort out the indentation - it is all over the place and that makes your code very hard to read. If you press CTRL+A to select it all, then CTRL+K CTRL+F Visual Studio will sort it out for you.

Then, start by looking at your loops - or hadn''t you noticed it only printed 7 out of the 8 elements when it finished?

When you have sorted that out, change your variable names. What are "i" and "m" supposed to do? "i" is acceptable (just) as an unimportant straight index, but you have "index" for that, and "i" doesn''t do that job anyway.

Looking at your code, and it''s copious comments, I have no idea what "m" is there for - I know it is probably something to do with your problem, since it restricts the outer loop to two passes - which means you can only swap eight values, so if you pass your method ten elements it''ll fail big time.

You need to go back to your algorithm, I think, and start looking at that as well.


只需添加到lewax的建议更改中.

第一个交换(最小的数组和第i个 数组元素)应移到第一个for循环之后(找到最小的数组元素的索引).否则,当第i th 元素最大时,selectionSort将失败,并且在可以将其交换为最大元素之前,将其替换为最小的元素.

Just to add to lewax''s suggested changes.

The first swap (of the smallest and ith array elements) should be moved to after the first for loop (where the index of the smallest array element is found). Otherwise, selectionSort fails when the ith element is the largest, and is swapped for the smallest before it can be swapped for the largest.

void selectionSort( int * const array, const int size )
{
    int smallest;
    int max;
    int i=0;
    
    while( i < size - i )
    {
        smallest = i;
        max = i;
        
        for ( int index = i; index < size-i; index++ )
        {
            if ( array[ index ] < array[ smallest ] )
                smallest = index;
        }

        swap( &array[ i ], &array[ smallest ] );
        
        for ( int x = i; x < size-i; x++ )
        {
            if ( array[ x ] > array[ max ] )
                max = x;
        }

        swap( &array[ size - ( i + 1 ) ], &array[ max ] );

        i++;
    }
}


您在这里遇到了一些问题.

首先,数组从0开始,而不是从1开始,因此main和selectionSort中的循环都应从0开始,而不是从1开始.

接下来,我不确定为什么选择排序中的循环会转到"size-i + 1",而在i = 0的情况下它超出范围,"size-i"就可以正常工作.

另一方面,大小比数组的最大索引大一个(在这种情况下,索引从0-7开始),因此第二次交换应使用"size-i-1"的索引,而不仅仅是"size-i-1" -i.

我认为这些是我所做的仅有的更改.

OriginalGriff的解决方案提醒我,我也选择了selectionSort中的m变量.不知道它做了什么,没有它就可以正常工作.
You have a few issues here.

First, arrays start at 0, not one, so both your loop in main and in selectionSort should start at 0, not at 1.

Next, I''m not sure why your loops in selection sort go to "size-i+1" and in the case of i=0 it goes out of bounds, "size-i" works fine.

On the other hand, size is one greater than the maximum index of the array (in this case, the indexes go from 0-7) so the second swap should use the index of "size-i-1" instead of just "size-i".

I think those were the only changes I made to get it working.

OriginalGriff''s solution reminded me, I took out the m variable in selectionSort as well. Wasn''t sure what it did and it worked fine without it.


这篇关于这段代码有什么错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆