1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ilia_Sergeevich [38]
3 years ago
10

Add a counter to the functions insertionSort and mergeSort that counts the number of comparisons that are made. Run the two func

tions with arrays of various sizes.
At what size does the difference in the number of comparisons become significant?
How does this size compare with the size that the orders of these algorithms predict?

Computers and Technology
1 answer:
xxMikexx [17]3 years ago
8 0

Answer:

#include <iostream>

#include <string>

#include <ctime>

using namespace std;

void insertionSort(int *arr, int size);

void mergeSort(int *items, int size);

void merge(int *temp1, int *temp2, int *items, int p, int q, int size);

void copyArray(int *sour, int sstart, int size, int *dest, int dstart, int n);

void printArray(int *arr, int size);

int insertionSortComparisons = 0;

int mergeSortComparisons = 0;

int main()

{

srand(time(NULL));

int size = 0;

cout << "Size\tinsertionSort\tmergeSort" << endl;

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

{

size = size + 5;

insertionSortComparisons = 0;

mergeSortComparisons = 0;

int *arr1 = new int[size];

int *arr2 = new int[size];

for(int i = 0; i < size; i++)

{

int number = 1 + rand() % 100;

arr1[i] = number;

arr2[i] = number;

}

insertionSort(arr1, size);

mergeSort(arr2, size);

cout << size << "\t\t" << insertionSortComparisons << "\t" << mergeSortComparisons << endl;

}

system("pause");

return 0;

}

void insertionSort(int arr[], int size)

{

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

{

int temp = arr[i];

int j = i;

insertionSortComparisons++;

while(j > 0 && temp < arr[j - 1])

{

arr[j] = arr[j - 1];

j--;

}

arr[j] = temp;

}

}

void mergeSort(int items[], int size)

{

int s1 = size / 2;

int s2 = size - s1;

int *temp1 = new int[s1];

int *temp2 = new int[s2];

if(size > 1)

{

copyArray(items, 0, size, temp1, 0, s1);

copyArray(items, s1, size, temp2, 0, s2);

mergeSort(temp1, s1);

mergeSort(temp2, s2);

merge(temp1, temp2, items, s1, s2, size);

}

}

void merge(int temp1[], int temp2[], int items[], int p, int q, int size)

{

int i = 0;

int j = 0;

int k = 0;

while(i < p && j < q)

{

mergeSortComparisons++;

if(temp1[i] <= temp2[j])

{

items[k] = temp1[i];

i = i + 1;

}

else

{

items[k] = temp2[j];

j = j + 1;

}

k = k + 1;

}

if(i == p)

copyArray(temp2, j, size, items, k, q - j);

else

copyArray(temp1, i, size, items, k, p - i);

}

void copyArray(int sour[], int sstart, int size, int dest[], int dstart, int n)

{

for(int i = sstart, j = dstart; i < size; i++, j++)

{

dest[j] = sour[i];

}

}

void printArray(int *arr, int size)

{

for(int i = 0; i < size; i++)

{

cout << arr[i] << " ";

}

cout << endl;

}

Explanation:

This program is a c++ program.

The c++ add a counter to the functions insertionSort and mergeSort that counts the number of comparisons that are made.

It Run the two functions with arrays of various sizes.

It also derermines the size that does the difference in the number of comparisons and how it become significant. Also talks How does this size compare with the size that the orders of these algorithms predict.

From the sample output in attachment, the difference between the number of comparisons of the insertionSort and the mergeSort techniques is increasing with the size of the array.

See attachment for sample output.

You might be interested in
How to hack the school system
Verizon [17]

Why Would You Need To " Hack" Your Schools Console??

Your school has your computer administrated, Go to developer settings then reboot the computer.

3 0
3 years ago
En que parte del mall del rio venden tarjetas de google play en cuenca​
JulijaS [17]

chxgfk hcyskvuct auhchovuzq vuvscisv

6 0
3 years ago
Read one positive integer n. Then create an n X n two-dimensional array and write the code that stores integers from 1 to n2 as
marysya [2.9K]

Answer:

The program in Java is as follows:

import java.util.*;

public class Main{

public static void main(String[] args) {

    int n;

    Scanner input = new Scanner(System.in);

 System.out.print("Size of array: ");

 n = input.nextInt();

 int count = 1;

 int[][] arr = new int[n][n];

 for(int i = 0; i<n;i++){

     for(int j = 0; j<n;j++){

         arr[i][j] = count;

         count++;      }  }

 for(int i = 0; i<n;i++){

     for(int j = 0; j<n; j++){

         System.out.printf(arr[i][j]+" ");      }

     System.out.println();  } }}

Explanation:

This declares the size of the array

    int n;

    Scanner input = new Scanner(System.in);

This prompts the user for size of array

 System.out.print("Size of array: ");

This gets input for size of array

 n = input.nextInt();

This initializes the array element to 1

 int count = 1;

This creates a 2d array

 int[][] arr = new int[n][n];

This iterates through the rows

 for(int i = 0; i<n;i++){

This iterates through the columns

     for(int j = 0; j<n;j++){

This populates the array

         arr[i][j] = count;

         count++;      }  }

The following nested loop prints the array elements

 for(int i = 0; i<n;i++){

     for(int j = 0; j<n; j++){

         System.out.printf(arr[i][j]+" ");      }

     System.out.println();  } }}

8 0
3 years ago
We piped the results of the Get-Process cmdlet to the Sort-Object cmdlet to sort them in descending order. Which property was us
Basile [38]
The answer is yes hope this helps did before
5 0
3 years ago
What is the key function of a Scrum Master?
Reptile [31]

Answer:

The scrum master is the team role responsible for ensuring the team lives agile values and principles and follows the processes and practices that the team agreed they would use. The responsibilities of this role include: Clearing obstacles. Establishing an environment where the team can be effective.

Explanation:

8 0
4 years ago
Other questions:
  • When determining the cost of an item, the seller will often analyze the demand as well as the supply before setting the price of
    8·2 answers
  • Which of the following is the largest disadvantage of wind power?
    12·2 answers
  • In what way, if any, are colleges and universities related?
    14·1 answer
  • Write a program named Admission for a college’s admissions office. The user enters a numeric high school grade point average (fo
    15·1 answer
  • Scientific models can be used for a variety of different purposes. Which of the following statements about scientific models is
    7·2 answers
  • What role does the agenda play in a webinar??
    14·1 answer
  • Which concept often comes in conflict with privacy rights?
    14·1 answer
  • Python is a popular programming language and has been labeled as having a safe core.
    14·1 answer
  • Question 10
    14·1 answer
  • Write python code that does the following.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!