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
Liula [17]
4 years ago
8

Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algo

rithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count of the number of exchanges it makes. Display these values on the screen.
Computers and Technology
1 answer:
Anit [1.1K]4 years ago
3 0

Answer:

The C++ code is given below

Explanation:

#include<iostream>

using namespace std;

void swap(int &a,int &b)

{

int temp = a;

a = b;

b = temp;

}

int bubbleSort(int *arr,int size)

{

int count = 0;

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

{

for (int j = 0; j < size-i-1; j++)

{

if (arr[j] > arr[j+1])

{

swap(arr[j],arr[j+1]);

count++;

}

}

}

 

return count;

}

int selectionSort(int *arr,int size)

{

int position,count = 0;

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

{

position = i;

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

{

if (arr[position] > arr[j])

position = j;

}

if (position != i)

{

swap(arr[i],arr[position]);

count++;

}

}

return count;

}

void printArray(int *arr,int size)

{

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

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

cout << endl;

}

int main()

{

int arr[20] = {1,3,4,2,6,7,8,5,12,13,14,15,9,10,11,20,19,18,17,16};

int arr1[20] = {1,3,4,2,6,7,8,5,12,13,14,15,9,10,11,20,19,18,17,16};

 

cout << "Array Before sorting : ";

printArray(arr,20);

 

int count = bubbleSort(arr,20);

 

cout << "Array After sorting : ";

printArray(arr,20);

 

cout << "Array Before sorting : ";

printArray(arr1,20);

 

int count1 = selectionSort(arr1,20);

 

cout << "Array After sorting : ";

printArray(arr1,20);

 

cout << "Number of exchanges in bubble sort is : " << count << endl;

cout << "Number of exchanges in selection sort is : " << count1 << endl;

return 0;

}

You might be interested in
The New option is found in the ...............tab.​
morpeh [17]

in your notes books and in your vopy

3 0
3 years ago
Read 2 more answers
What is a Forloop and what is it used for?
anastassius [24]
It is used to repeat any block of code multiple times (iteration)
6 0
4 years ago
Read 2 more answers
Implement a sublinear running time complexity recursive function in Java public static long exponentiation (long x, int n) to ca
Charra [1.4K]

Answer:

Following are the code block in the Java Programming Language.

//define recursive function

public static long exponentiation(long x, int n) {

//check the integer variable is equal to the 0.

if (x == 0) {

//then, return 1

return 1;

}

//Otherwise, set else

else {

//set long data type variable

long q = exponentiation(x, n/2);

q *= q;

//check if the remainder is 1

if (n % 2 == 1) {

q *= x;

}

//return the variable

return q;

}

}

Explanation:

<u>Following are the description of the code block</u>.

  • Firstly, we define the long data type recursive function.
  • Then, set the if conditional statement and return the value 1.
  • Otherwise, set the long data type variable 'q' that sore the output of the recursive function.
  • Set the if conditional statement and check that the remainder is 1 and return the variable 'q'.
4 0
3 years ago
Instructions The population of town A is less than the population of town B. However, the population of town A is growing faster
weeeeeb [17]

Answer: The c++ program is given below.

#include <iostream>

using namespace std;

int main() {

float townA, townB, growthA, growthB, populationA, populationB;

    int year=0;    

cout<<"Enter present population of town A : ";

cin >> townA;  

cout<<endl<<"Enter present growth rate of town A : ";

cin >> growthA;

growthA = growthA/100;  

cout<<endl<<"Enter present population of town B : ";

cin >> townB;  

cout<<endl<<"Enter present growth rate of town B : ";

cin >> growthB;

growthB = growthB/100;  

do

{

    populationA = townA + (townA * growthA);

    populationB = townB + (townB * growthB);      

    townA = populationA;

    townB = populationB;      

    year++;      

}while(populationA < populationB);  

cout<<endl<<"After " <<year<< " years, population of town A is "<<populationA << " and population of town B is "<< population<<endl;

return 0;

}

Explanation:

All the variables for population and growth rate are declared with float datatype.

The user inputs the present population of both the towns.

The growth rate entered by the user is the percentage growth.

For example, town A has 10% growth rate as shown in the output image.

The program converts 10% into float as 10/100.

growthA = growthA/100;

growthB = growthB/100;

The above conversion is done to ease the calculations.

The year variable is declared as integer and initialized to 0.

The growth in population is computed in a do-while loop. After each growth is calculated, the year variable is incremented by 1.

The loop continues until population of town A becomes greater than or equal to population of town B as mentioned in the question.

Once the loop discontinues, the final populations of town A and town B and the years needed for this growth is displayed.

The new line is introduced using endl keyword.

The function main has return type int hence, 0 is returned at the end of the program.

6 0
4 years ago
What is printed by the following program? var isRaining = false; var isCloudy = false; var isSunny = !isRaining &amp;&amp; !isCl
hram777 [196]

Answer:

The output to this given question is "Is it warm: true".

Explanation:

In this question firstly two-variable defines that are isRaining and isCloudy. In those variables assign false value. Then we define another variable that is isSunny. In isSunny variable, we check condition that is if variable isRaining not AND (logical gate) variable isCloudy not equal to isSunny. The AND logical gate is used to check that both condition is true or not. Then we define another variable that is isWarm. In this variable, we check condition if isSunny OR (logical operator) isSummer is true. The OR operator is used to check in both value if one condition will true it will print true. At the last will print the value.

8 0
3 years ago
Other questions:
  • . It is essential for a relay energized by alternating current to have A. many turns of small wire. B. a laminated core and a sh
    8·1 answer
  • What is the difference between HTML and CSS? * 1. CSS is a markup language unlike HTML. 2. HTML is a backend technology and CSS
    12·2 answers
  • You csn access various sites on the www by using hyperlinks or by
    7·1 answer
  • Nikolas has a idea that he could use the compressed carbon dioxide in a fire extinguisher to propel him on his skateboard. Nikol
    13·2 answers
  • What does it mean to be self demanding?
    12·1 answer
  • Tell me the most scariest website you know and i'll give you brainlest
    5·1 answer
  • In Subtractive empathy, the counselor responses gives back less (or distorts) than what the client has said. slightly add someth
    14·1 answer
  • The _____________computer function accepts data from input devices and sends it to the computer processor.
    11·1 answer
  • What can handle work that is hard on a person and could cause repetitive injuries?
    6·1 answer
  • If you wish to install a new OS without disturbing the old one so that you can boot to either OS, what type of boot setup should
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!