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
Let f be the following function: int f(char *s, char *t){char *p1, *p2;for(p1 = s, p2 = t; *p1 != ‘\0’&amp;&amp; *p2 != ‘\0’; p1
siniylev [52]

Answer:

return value =2.

Here the function f() returns the length of the substring we traversed before we find the same character at the equal index of two substrings.

Take the inputs s= “abcd” and t= “bccd”.  

• Now, p1 points to s1, i.e., p1 points to the character ‘a’ of “abcd”. And similarly, p2 points to ‘b’ of “bccd”.

• Then we compare the values at p1 and p2, are not equal, so p1 and p2 both are incremented by 1.

• Now the characters ‘b’ and ‘c’ of “abcd” and “bccd” respectively are compared. They are not equal. So both p1 and p2 both are incremented by 1.

• Now, p1 points to ‘c’ of “ abcd” that is the element at index 2 of s. And p2 points to ‘c’ of “bccd” that is the element at index 2 of t. Here value at p1 and p2 becomes equal. So the break statement is executed. We stop moving forward.

• As p1 is pointing to index 2 and s is pointing to the base that is index 0, so p1-s = 2.

Explanation:

#include<stdio.h>

int f(char *s, char *t);

void main()

{

int k = f("abcd", "bccd");

printf("%d", k);

}

int f(char *s, char *t)

{

char *p1, *p2;

for(p1 = s, p2 = t; *p1 != '\0'&& *p2 != '\0'; p1++, p2++)

{

 if (*p1 ==*p2)  

  break;  

}

return (p1-s);

}

OUPUT is given as image

7 0
3 years ago
.<br> 1. Press the _______ key to move to the next cell in a row.
likoan [24]
Tab it is the tab jey

3 0
3 years ago
Read 2 more answers
What is the use of html in websites
Alborosie
HTML is a very basic markup language and requires memorization of a few dozen HTML commands that structure the look and layout of a web page. Before writing <span>any HTML code or designing your first web page, you must decide on an HTML editor or text editor, such as Notepad or Word Pad.</span>
4 0
3 years ago
Read 2 more answers
WAP to input the rating of a movie, and print as per the given criteria:
vodka [1.7K]

Answer:

What is the question?

Explanation:

8 0
2 years ago
Hard drives have the largest capacity of any storage device. <br> a. True <br> b. False
VMariaS [17]
It's false as they store way lesser than we think
3 0
3 years ago
Read 2 more answers
Other questions:
  • How much power is consumed by a 1.4A motor connected to a 12V battery?​
    11·1 answer
  • ​ Search engines can perform date-filtered searches because, when web servers send a webpage to a ____, they include a header th
    11·1 answer
  • You are starting a spreadsheet, and you would like 500 to appear in cell C3. You should _____.
    11·2 answers
  • What network monitoring technology enables a switch to copy and forward traffic sent and received on multiple interfaces out ano
    9·1 answer
  • is either the number of bits used to indicate the color of a single pixel, or the number of bits used for each color component o
    7·1 answer
  • Which kind of system software tells the computer how to communicate with peripherals, such as a prero
    6·2 answers
  • What need did anti lock brakes address?
    10·1 answer
  • (Display four patterns using loops) Ask the user to enter an integer to
    8·1 answer
  • Refer to the exhibit. A network administrator is configuring PAT on an ASA device to enable internal workstations to access the
    12·1 answer
  • What happens when you run a program in Python? ​
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!