简体   繁体   中英

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. Display the resulting data using a displaydata function that you pass the array (and it's size) into.

  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).

[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. The valid range of indexes of the array are [0, SIZE -1] that is [0, 5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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