Question

Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The...

Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm

Note: (Complete Programming Exercise #15 on page 1345. Submit a screen shot testing with 20 numbers, 500 numbers, 1,000 numbers. Show the output of the arrays of 20 so you can see they are in order. Save the file with your FirstInitial LastName Lab10.

Include also three source file: main.cpp, function.h,function.cpp)

C++ Program Design Including data Structures,7th Edition, D.S Malik

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code for above problem, with screenshots of the code and sample output.

If you have any doubts, feel free to ask in comments.

function.h

#include <iostream>

using namespace std;

//function to do bubble sort
void bubbleSort(int[], int);

//function to do selection sort
void selectionSort(int[], int);

//function to do insertion sort
void insertionSort(int[], int);

//function to print array
void printArray(int[], int);

function.cpp

#include "function.h"

#include <iostream>
#include <stdlib.h>

using namespace std;

//function that does bubble sort
void bubbleSort(int arr[], int n) {
  
   int numOfComparisons = 0;
   int numOfAssignments = 0;
  
    for (int i = 0; i < n - 1; i++) {
      
       for (int j = 0; j < n - i - 1; j++) {
          
           numOfComparisons++;
           if (arr[j] > arr[j + 1]) {
              
               int temp = arr[j];
               numOfAssignments++;
               arr[j] = arr[j + 1];
               numOfAssignments++;
               arr[j + 1] = temp;
               numOfAssignments++;
            }
        }
    }
  
    cout<<"Doing Bubble Sort with "<<n<<" elements...\n";
  
    cout<<"Number of Comparisons = "<<numOfComparisons<<endl;
  
    cout<<"Number of Assignments = "<<numOfAssignments<<endl<<endl;
}

//function that does selection sort
void selectionSort(int arr[], int n) {
  
   int numOfComparisons = 0;
   int numOfAssignments = 0;
  
    for (int i = 0; i < n - 1; i++) {
      
        int min_idx = i;
        for (int j = i + 1; j < n; j++) {
          
           numOfComparisons++;
              if (arr[j] < arr[min_idx]) {
                 
               min_idx = j;
           }
        }
      
        if (min_idx != i) {
          
           int temp = arr[min_idx];
           numOfAssignments++;
           arr[min_idx] = arr[i];
           numOfAssignments++;
           arr[i] = temp;
           numOfAssignments++;  
       }
    }
  
    cout<<"Doing Selection Sort with "<<n<<" elements...\n";
  
    cout<<"Number of Comparisons = "<<numOfComparisons<<endl;
  
    cout<<"Number of Assignments = "<<numOfAssignments<<endl<<endl;
}

//function that does insertion sort
void insertionSort(int arr[], int n) {
  
   int numOfComparisons = 0;
   int numOfAssignments = 0;
  
   for (int i = 1; i < n; i++) {
      
        int key = arr[i];
        int j = i - 1;
      
       numOfComparisons++;
      
        while (j >= 0 && arr[j] > key) {
          
            arr[j + 1] = arr[j];
            numOfAssignments++;
            j = j - 1;
            numOfAssignments++;
        }
      
        arr[j + 1] = key;
       numOfAssignments++;
    }
  
  
    cout<<"Doing Insertion Sort with "<<n<<" elements...\n";
  
    cout<<"Number of Comparisons = "<<numOfComparisons<<endl;
  
    cout<<"Number of Assignments = "<<numOfAssignments<<endl<<endl;
}

//function that prints the content of an array
void printArray(int arr[], int n) {
  
   for (int i = 0; i < n; i++) {
       cout<<arr[i]<<" ";
   }
   cout<<endl;
  
   cout<<"-----------------------------------------------------------------------------------------------------------------------------\n";
}

main.cpp

#include "function.cpp"

#include <iostream>
#include <stdlib.h>

using namespace std;

int main() {
  
   //define three arrays
   int arr1[1000];
   int arr2[1000];
   int arr3[1000];
  
   //initialize arrays with random values
   for(int i = 0; i < 20; i++) {

       int val = (rand() % 100) + 1;
      
       arr1[i] = val;
       arr2[i] = val;
       arr3[i] = val;

    }
  
    cout<<"Array 1, before sorting = ";
    printArray(arr1, 20);
  
   bubbleSort(arr1, 20);
  
    cout<<"Array 1, after sorting = ";
    printArray(arr1, 20);
  
    cout<<"Array 2, before sorting = ";
    printArray(arr2, 20);
  
    selectionSort(arr2, 20);
  
    cout<<"Array 2, after sorting = ";
    printArray(arr2, 20);
  
    cout<<"Array 3, before sorting = ";
    printArray(arr3, 20);
  
    insertionSort(arr3, 20);
  
    cout<<"Array 3, after sorting = ";
    printArray(arr3, 20);
  
   for(int i = 0; i < 500; i++) {

       int val = (rand() % 1000) + 1;
      
       arr1[i] = val;
       arr2[i] = val;
       arr3[i] = val;

    }
  
   bubbleSort(arr1, 500);
  
    selectionSort(arr2, 500);
  
    insertionSort(arr3, 500);
  
    for(int i = 0; i < 1000; i++) {

       int val = (rand() % 1000) + 1;
      
       arr1[i] = val;
       arr2[i] = val;
       arr3[i] = val;

    }
  
   bubbleSort(arr1, 1000);
  
    selectionSort(arr2, 1000);
  
    insertionSort(arr3, 1000);
      
}

Screenshot of function.h :

Screenshots of function.cpp:

1)

2)

3)

Screenshots of main.cpp :

1)

2)

3)

Screenshot of the sample output :

Add a comment
Know the answer?
Add Answer to:
Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that creates three identical arrays,list1,list2, and list3, of 5000 elements. The program then...

    Write a program that creates three identical arrays,list1,list2, and list3, of 5000 elements. The program then sortslist1using bubble sort, list2using selection sort, andlist3using insertion sort, and outputs the number of comparisons and item assignments made by each sorting algorithm. Submit a screen shot testing with 20 numbers, 500 numbers, 1000 numbers. Show the output of the arrays of 20 so you can see they are in order. please use c++

  • sort

    Problem Statement 4Write a program that creates three identical arrays, list1, list2, and list3 of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm.

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • Hi this program must be completed using a Eclipes Compiler for Java only . This is...

    Hi this program must be completed using a Eclipes Compiler for Java only . This is and intro to java class , so only intro methods should be used for this assignment. Please include a copy of the input data and a sample of the out put data . Thanks a bunch Homework-Topic 9-Donations Write a complete program to do the following: The main program calls a method to read in (Erom an input file) a set of people's three-digit...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT