【问题标题】:Bubble and Selection sort c++冒泡和选择排序 C++
【发布时间】:2013-12-01 18:42:16
【问题描述】:

我几乎让这段代码正常工作,唯一的问题是我的选择和冒泡排序正在删除我数组中的最后一个整数并用零替换它,或者它打印一个零并且看不到数组中的最后一个数字。无论如何无法弄清楚如何解决这个问题。这是我的程序需要做的事情

  1. 通过创建一个包含 1 到 49 之间的 6 个值的随机数组来开始程序。确保使用 time() 函数和 srand 为 rand() 函数提供种子。使用您将数组(及其大小)传递到其中的 displaydata 函数显示结果数据。

  2. 让用户确定他们是希望使用冒泡排序还是选择排序来对数组进行排序。一旦用户做出决定,让程序调用冒泡排序或选择排序函数(以随机数组及其大小作为参数)对数组进行排序并调用 displaydata 函数(以排序后的数组作为参数)。

[代码]

 #include <iostream>
 #include<time.h>
 using namespace std;

 const int SIZE = 6;


void displaydata ( int[], int );
void bubblesort ( int[], int );
void selectionsort ( int[], int );

int main()
{
    char choice;

    int array [ SIZE ] = {0,0,0,0,0,0};

    srand ( ( int ) time ( 0 ) );

    for ( int i = 0; i < SIZE; i++ )
    {
         array [ i ] =  1 + ( rand() % 49 );
    }

   cout << "Do you wish to use Bubble Sort (Enter 'B') or Selection Sort (Enter 'S'): ";

   cin >> choice;
   cout << endl;

   displaydata ( array, SIZE );

   if ( choice == 'b' || choice == 'B' )
   {
       bubblesort ( array, SIZE );
   }
   else if ( choice == 's' || choice == 'S' )
   {
       selectionsort ( array, SIZE );
   }
   else
   {
       cout << " Invalid Entry ";
   }

   return 0;
}

void displaydata ( int array[], int size )
{
   cout<<"----------------\n";
   cout<<" Original Array \n";
   cout<<"----------------\n\n";

/* loop 5 times */
   for (int size = 1; size < 7; size++ )
   {
    cout << array [ size ] << endl;
   }
}

void bubblesort ( int array[], int b )
{
    for( int i=1; i<b ;i++ )
    {
       for( int a=0; a<b-1; a++)
       {
          if(array[a] > array[a+1])
          {
            int temp;
            temp = array[a];
            array[a] = array[a+1];
            array[a+1] = temp;   
          } 
       }
    }

    cout<<endl;
    cout<<"-------------------\n";
    cout<<" Bubble Sort Array \n";
    cout<<"-------------------\n\n";

    for( int a=0; a<b; a++)
      cout<<array[a]<<endl;
}

void selectionsort ( int array[], int s )
{
    int pos_min,temp;

    for (int i=0; i < s-1; i++)
   {
       pos_min = i;

   for (int j=i+1; j < s; j++)
   {
           if (array[j] < array[pos_min])
               pos_min=j;
       }

       if (pos_min != i)
       {
           temp = array[i];
           array[i] = array[pos_min];
           array[pos_min] = temp;
       }
   }

   cout<<endl;
   cout<<"----------------------\n";
   cout<<" Selection Sort Array \n";
   cout<<"----------------------\n\n";

   for( int a=0; a<s; a++)
       cout<<array[a]<<endl;
}

【问题讨论】:

    标签: c++ arrays sorting


    【解决方案1】:

    这个循环

    for ( int i = 1; i < 7; i++ )
    

    无效,因为您试图访问不属于该数组的元素数组 [6]。数组索引的有效范围为 [0, SIZE -1] 即 [0, 5]

    【讨论】:

    • 我有点困惑,我该如何解决这个问题,因为我需要它随机生成 6 个不包括 0 的整数
    • 您定义了常量 SIZE,所以使用它而不是魔术值 7 或 6。例如,您的代码中的第一个这样的循环可以写为 for (int i = 0; i
    • 所以我将第一个循环更改为 int i=0; i
    • 您不仅需要更改代码中的第一个循环,还需要更改其他循环,包括函数 displaydata 中的循环。我指出了一个原因。您应该自己检查循环的正确性。
    • 解决了零问题的真棒......现在不确定是我的代码还是随机数生成,但我已经运行程序 15 次,每次随机 6 个整数中出现 5 时,如果发生这种情况我数字越来越低,但总是有 5
    猜你喜欢
    • 2015-02-14
    • 2015-04-25
    • 2016-11-01
    • 2015-09-16
    • 2017-02-07
    • 2016-05-28
    • 2014-03-26
    相关资源
    最近更新 更多