1 answer

Do in C++ language thank you Task 4: Functions or classes are ok. Please read data...

Question:

do in C++ language thank you

Task 4:

Functions or classes are ok.

Please read data into program from descending_mostly_sorted.txt and store into an array.

Implement Selection Sort to sort the values in ascending order. Least → Greatest

Measure the time it takes to execute the sort in milliseconds or even nanoseconds.

Please run the sort 3 times.

  • Attach Photos of Source Code and Output

  • Task 5:

    Functions or classes are ok.

    Please read data into program from descending_mostly_sorted.txt and store into an array.

    Implement Insertion Sort to sort the values in ascending order. Least → Greatest

    Measure the time it takes to execute the sort in milliseconds or even nanoseconds.

    Please run the sort 3 times.

  • Attach Photos of Source Code and Output



Answers

Task 4: Selection Sort

============================================================================================

#include<iostream>
#include<fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)
{
int i, j, min_idx;
  
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
{
        min_idx = j;   
           }
           //printf("min index is: %d\n",min_idx);
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
  

void display(int arr[],int n)
{
   for(int i=0;i<n;i++)
   cout<<arr[i]<<" ";
}
int main()
{

ifstream inputFile("descending_mostly_sorted.txt");
int arr[1000];
int count=0;
//start time and end time stored here
clock_t start, end;
long timeElapsed;


if (inputFile.is_open())

{
   while(!inputFile.eof())
   {
       inputFile>>arr[count++];
   }
   cout<<"Array before sorting: \n";
   display(arr,count);
  
   //count the time before searching
   start = clock();

   selectionSort(arr,count);
  
   //count the time after searching
   end = clock();
   timeElapsed = end - start;
   cout<<"\nArray after sorting: \n";
   display(arr,count);

   //print the time taken by searching
   cout << "\nTime taken for sorting: " << timeElapsed << " milliseconds" << endl;
  
}
else
cout << "File cannot be opened.";

return 0;
}

============================================================================================

Output

============================================================================================

Task 5: Insertion Sort

============================================================================================

#include<iostream>
#include<fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

void insertionSort(int arr[],int n)
{
int i, j;
int temp;
for (i = 1; i <n; i++)
{
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}

void display(int arr[],int n)
{
   for(int i=0;i<n;i++)
   cout<<arr[i]<<" ";
}
int main()
{

ifstream inputFile("descending_mostly_sorted.txt");
int arr[1000];
int count=0;
//start time and end time stored here
clock_t start, end;
long timeElapsed;


if (inputFile.is_open())

{
   while(!inputFile.eof())
   {
       inputFile>>arr[count++];
   }
   cout<<"Array before sorting: \n";
   display(arr,count);
  
   //count the time before searching
   start = clock();

   insertionSort(arr,count);
  
   //count the time after searching
   end = clock();
   timeElapsed = end - start;
   cout<<"\nArray after sorting: \n";
   display(arr,count);

   //print the time taken by searching
   cout << "\nTime taken for sorting: " << timeElapsed << " milliseconds" << endl;
  
}
else
cout << "File cannot be opened.";

return 0;
}

============================================================================================

Ouptut

.

Similar Solved Questions

1 answer
You are in the Define phase and decide to review the problem statement in the project...
You are in the Define phase and decide to review the problem statement in the project charter. Which of the following characteristics should NOT be included in a problem statement? I. Solution(s) II. Symptoms of the problem III. Root cause(s) IV. Metrics V. Baseline data A: IV and V B: I and II C: I...
1 answer
41. In January, Lance sold stock with a cost basis of 526,000 to his brother, James,...
41. In January, Lance sold stock with a cost basis of 526,000 to his brother, James, for $24,000, the fair market value of the stock on the date of sale. Five months later, James sold the same stock through his broker for $27,000. What is the tax effect of these transactions? a. Disallowed loss to J...
1 answer
Amy Dyken, controller at Bridgeport Pharmaceutical Industries, a public company, is currently preparing the calculation for...
Amy Dyken, controller at Bridgeport Pharmaceutical Industries, a public company, is currently preparing the calculation for basic and diluted earnings per share and the related disclosure for Bridgeport’s financial statements. Below is selected financial information for the fiscal year ended J...
1 answer
Just write the answers. Part A After 0.64g of CoCl2 6H2O is heated, the residue has...
just write the answers. Part A After 0.64g of CoCl2 6H2O is heated, the residue has a mass of 0.32 g . Calculate the % H20 in the hydrate. ΑΣΦ H20 Submit Request Answer Part B What was the actual number of moles of water per formula unit CoCl2? ΑΣΦ Consider the follow...
1 answer
Help! Short or no explanations! Answer right and you will have postivle feedback! Answer all 4,...
Help! Short or no explanations! Answer right and you will have postivle feedback! Answer all 4, because accouding to the policy in a single post I can ask 4 questions! Thanks Question 17 (of 33) 17 The comparison of a company's financial condition and performance to a base amount is known...
1 answer
How do you check solutions to equations with variables on both sides?
How do you check solutions to equations with variables on both sides?...
1 answer
If the interest rate is 10%, what is the present value of a security that pays...
If the interest rate is 10%, what is the present value of a security that pays you $1,100 next year, sl 220 the year after, and S1338 the year after that? Present value is S(Round your response to the nearest penny)...
1 answer
Maltz Company estimates that unit sales will be 9,600 in quarter 1; 12,000 in quarter 2;...
Maltz Company estimates that unit sales will be 9,600 in quarter 1; 12,000 in quarter 2; 13,800 in quarter 3; and 17,400 in quarter 4. Management wants to have ar ending finished goods inventory equal to 21% of the next quarter's expected unit sales. Prepare a production budget by quarters for t...
1 answer
Please reply to this post and ask one question regarding this post: Nursing informatics is a...
please reply to this post and ask one question regarding this post: Nursing informatics is a specialty that supports nursing through the integration of information science, computer science, nursing science to provide data communication management (Darvish et al., 2014). With technology advancing mo...
1 answer
Hello, Please help me with the questions below. Thank you so much for your time. 2....
Hello, Please help me with the questions below. Thank you so much for your time. 2. For an airplane during landing, an angle of approach is defined as the angle formed by the runway and the path of the aircraft as it approaches the runway. For a safe landing, pilots need to land the plane with an...
3 answers
Help with rounding 6318 to the nearest thousand, hundred, and ten
Help with rounding 6318 to the nearest thousand, hundred, and ten....
1 answer
Lincoln Company issued $50,000 of 10-year, 8% bonds payable on January 1, 2018. Lincoln Company pays...
Lincoln Company issued $50,000 of 10-year, 8% bonds payable on January 1, 2018. Lincoln Company pays interest each January 1 and July 1 and amortizes discount or premium by the straight-line amortization method. The company can issue its bonds payable under various conditions. Read the requirements....
1 answer
7.U)A cylindrical capacitor of length L consists of coaxial conducting surfaces of radii a and b...
7.U)A cylindrical capacitor of length L consists of coaxial conducting surfaces of radii a and b (Fig. 4). The dielectric material between the surfaces has a relative permittivity s, 2+(4/r) for a <r< b. (a) Determine the capacitance of this capacitor. (b) Find the electrostatic energy stored ...
1 answer
7. For each of the following, prove that the sequence {a,) converges and find the limit....
7. For each of the following, prove that the sequence {a,) converges and find the limit. 2. Qn+1 = (2a, + 5). a, = 2 b. a -1 = V2an, a, = 3 *c. Qn+1 = V2a, + 3. a, - 1 d. 2n+1 = V2a, + 3, a, = 4...
1 answer
How we plot? and how we put values inside formula for B? i am confused help...
How we plot? and how we put values inside formula for B? i am confused help would like explanation Social Skills Training A social skills training program was implemented with seven mildly challenged students in a study to determine whether the program caused improvement in pre/post measures and be...
1 answer
Please show formulas you use, it isn't multiple questions only 1. Teacher likes to break the...
Please show formulas you use, it isn't multiple questions only 1. Teacher likes to break the question into multiple parts to help students with the steps. Thank you :)! 5. Perform a hypothesis test to determine if a difference exists in the proportion of uninsured drivers between the states of M...
1 answer
Other things being equal which of the following types of bonds should have lower yield to...
other things being equal which of the following types of bonds should have lower yield to maturity a bond is a debenture a bond has no collateral all bonds should have the same yiled to maturity a bond is secured by land...
1 answer
In the short­run, foreign direct investment tends to a- Decrease wage in the host country b-...
In the short­run, foreign direct investment tends to a- Decrease wage in the host country b- Increase wage in the source country c- Decrease the rental rate in the host (receiving) country d- Decrease the rental rate in the source (sending) country...
1 answer
QUESTION 7 Consider the poset (A, R) represented by the following Hasse diagram (2 (a) Give each of the following If any do not exıst, explan why (i) The greatest element of (A, R) (i:) The least ele...
QUESTION 7 Consider the poset (A, R) represented by the following Hasse diagram (2 (a) Give each of the following If any do not exıst, explan why (i) The greatest element of (A, R) (i:) The least element of (A, R) (i) All upper bounds of {h, eh (iv) The least upper bound (LUB) or(h (v) All lowe...
1 answer
5. Your university star basketball player is having a difficult night of free throws. His long...
5. Your university star basketball player is having a difficult night of free throws. His long range probability of making free throws is 80%, however, tonight he only made 2 out of 4 shots. Fans think he was nervous. Let's use simulation to see how likely it is for him to only make 2 out of 4 s...