Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I did this program #include \"stdafx.h\" #include<iostream> using namespace std;

ID: 3559811 • Letter: I

Question

I did this program

#include "stdafx.h"
#include<iostream>
using namespace std;

//function prototypes
void bubbleSort (int [], int&, int&);
void selectionSort(int[] , int&, int&);
void inserionSort(int [],int&,int&);
void generateRandomArray(int[] , int[] , int[]);

//Global constant value

const int MAX=5000;
// main method to start program execution
int main()
{
   // Declare a integer variable to zero
   int comparisons=0;
   int itemAssignments=0;
   int list1 [MAX] , list2 [MAX] , list3 [MAX];

   generateRandomArray (list1, list2, list3);
   comparisons=0;
   itemAssignments=0;
   cout<<"Number of comparisons---"<<endl;

   //calling bubble sort method
   bubbleSort (list1, comparisons, itemAssignments);
   cout<<" Bubble sort:"<<
                           comparisons<<endl;
   comparisons=0;
   itemAssignments=0;
       //calling selection sort method
   selectionSort (list2, comparisons, itemAssignments);
   cout<<" Selection sort:"<<
                           comparisons<<endl;
   comparisons=0;
   itemAssignments=0;
   // calling insertion sort method
   inserionSort (list3,comparisons, itemAssignments);
   cout<<" Insertion sort:"<<
                           comparisons<<endl;
   comparisons=0;
   itemAssignments=0;
   cout<<endl;

   generateRandomArray (list1, list2, list3);
   cout<<"Number of item assignments---"<<endl;

   bubbleSort (list1, comparisons, itemAssignments);
   cout<<" Bubble sort:"<<
                           itemAssignments<<endl;
   comparisons=0;
   itemAssignments=0;
   selectionSort (list2, comparisons, itemAssignments);
   cout<<" Selection sort:"<<
                           itemAssignments<<endl;
   comparisons=0;
   itemAssignments=0;
   inserionSort (list3, comparisons, itemAssignments);
   cout<<" Insertion sort:"<<
                           itemAssignments<<endl;
   comparisons=0;
   itemAssignments=0;
   cout<<endl;
              
   //pause the program output
   system("pause");
   return 0;
} // end of main method

/*This method takes three integer arrays and fills the
* arrays with random numbers in the range 1 to 500
*/

void generateRandomArray(int arr1[] ,int arr2[], int arr3[])
{
   srand (time_t (0));
  
   for(int i=0;i<MAX;i++)
       arr1[i]=arr2[i]=arr3[i]=rand() %500+1;

} //end of the method generate random Array

/*Bubble_sort method to find the number of comparisons
and item_assignments*/

void bubbleSort( int num[],int &comparisons,int
&itemAssignments )
{
   for (int iter=1 ; iter<MAX; iter++)
   {
       for (int index=0; index<MAX-iter; index++)
       {
                       //increment comparisons value by one
           comparisons++;
           if (num[index] >num[index+1])
           {
               int temp=num[index];
               num[index]=num[index+1];
               num[index+1]=temp;
               //increment itemAssignments value by one
               itemAssignments++;
           }
       }
   }
} // end of bubble sort method

/*Selection sort method to find the number of comparisons
and item_assignments*/

void selectionSort( int num[],int &comparisons,
                               int &itemAssignments )
{
       // Local variables required to sort the elements
   int index;
   int smallestIndex;
   int location;
   int temp;
  
   for (index=0;index<MAX-1; index++)
   {
       smallestIndex=index;
       for (location=index+1; location<MAX; location++)
       {
           //increment comparisions value by one
           comparisons++;
           if(num[location]<num[smallestIndex])
           {
           smallestIndex=location;
           }
       }//end of location for loop
  
       temp=num[smallestIndex];
      
       num[smallestIndex]=num[index];
       //increment itemAssignments value by three
       itemAssignments=itemAssignments+3;
       num[index]= temp;
   } //end of index for loop
} //end of selection sort method

/*Insertion_sort method to find the number of comparisons
and item_assignments*/
void inserionSort(int num[] ,int &comparisons, int
&assignments)
{
   int firstOutOfOrder,location;
   int temp;
  
   for (firstOutOfOrder=1;
   firstOutOfOrder<MAX; firstOutOfOrder++)
   {
       //increment comparisions value by one
       comparisons++;
       if(num[firstOutOfOrder]<num[firstOutOfOrder-1])
       {
           comparisons++;
           temp=num [firstOutOfOrder];
           location=firstOutOfOrder;
           //increment assignments value by two
           assignments=assignments+2;

           do
           {
               num[location]=num[location-1];
               assignments++;
               location--;
               comparisons++;
           } while(location>0 && num[location-1]>temp);

           num[location]=temp;
           assignments++;
       }
   }
} //end of insertion sort method

and the output should be like

but i keep getting this output; which has 3 numbers wrong

the program creates the three identical arrays listl,list2 and list3 of 5000 elements .The program sorts the
arrays using bubble

Explanation / Answer

it gives different values different time.code is correct.Sometimes it is depend on compiler.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote