简体   繁体   English

气泡和选择排序C ++

[英]Bubble and Selection sort c++

I almost have this code working only problem is my selection and bubble sorts are dropping the last integer in my array and replacing it with a zero or its printing a zero and not seeing the last digit in the array. 我几乎让这段代码起作用的唯一问题是我的选择,气泡排序正在删除数组中的最后一个整数,并用零替换它,或者将其打印为零,而看不到数组中的最后一位数字。 Anyways can't figure out how to fix the issue. 无论如何都无法弄清楚如何解决该问题。 This is what my program needs to do 这是我的程序需要做的

  1. Begin the program by creating a random array of 6 values between 1 and 49. Make sure that you use the time() function and srand to seed the rand() function. 通过创建一个1到49之间的6个值的随机数组来开始程序。请确保您使用time()函数并使用srand作为rand()函数的种子。 Display the resulting data using a displaydata function that you pass the array (and it's size) into. 使用将数组(及其大小)传递到的displaydata函数显示结果数据。

  2. Have the user determine whether they wish to use a bubble sort to sort the array or selection sort. 让用户确定是否要使用冒泡排序对数组进行排序或选择排序。 Once the user has made their decision have the program call either the bubblesort or selectionsort function (with the random array and its size as arguments) which sorts the array and calls the displaydata function (with the sorted array as an argument). 一旦用户做出决定,程序就可以调用Bubblesort或selectionsort函数(以随机数组及其大小作为参数),该函数对数组进行排序并调用displaydata函数(以排序后的数组作为参数)。

[code] [码]

 #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;
}

This loop 这个循环

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

is invald because you are trying to access element array[6] that does not belong to the array. 之所以被称为Invald,是因为您尝试访问不属于该数组的元素array [6]。 The valid range of indexes of the array are [0, SIZE -1] that is [0, 5] 数组索引的有效范围是[0,SIZE -1],即[0,5]

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

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