Question

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

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



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

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

Add a comment
Know the answer?
Add Answer to:
do in C++ language thank you Task 4: Functions or classes are ok. Please read data...
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
  • Task 3: Functions or classes are ok. Create an array that holds 1000 random floats between...

    Task 3: Functions or classes are ok. Create an array that holds 1000 random floats between 1-1000. Implement Quick Sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds. Please run the sort 3 times. Attach Photos of Source Code and Output

  • Please read data into a program from ascending_mostly_sorted.txt (a text file containing integers that are mostly...

    Please read data into a program from ascending_mostly_sorted.txt (a text file containing integers that are mostly in ascending order) 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. IN c++ Please run the sort 3 times.

  • Please Use C++ Language. And Note that please donot post the answer already there Because there...

    Please Use C++ Language. And Note that please donot post the answer already there Because there is similar answer code but I need with modified Quick sort algorithm ​. Update your program from ... Create an array that holds 1000 random integers between 1-1000. Allow the user to enter an integer to search. Create and implement modified Quick sort algorithm which will sort the array before the Binary Search algorithm is executed. Create and implement a Binary Search Algorithm ....

  • PART 1  Initialize the array from the Values.txt Mostly Sorted Descending. (VALUES ARE GIVEN BELOW) (YOU CAN...

    PART 1  Initialize the array from the Values.txt Mostly Sorted Descending. (VALUES ARE GIVEN BELOW) (YOU CAN COPY AND PASTE THE ARRAY VALUES INTO YOUR PROGRAM) Task 1: Functions or classes are ok. Initialize the array from the Values.txt Mostly Sorted Descending. Implement Selection sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds. Please run the sort 3 times. Attach Photos of Source Code and Output Task 2:...

  • C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that...

    C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that creates an array of 10 integers. The program should then use the following functions: getData() Used to ask the user for the numbers and store them into an array displayData() Used to display the data in the array displayLargest() Used to find and display the largest number in the array (prior to sort). displaySmallest() Used to find and display the smallest number in the...

  • (C++ please. Make sure it works in Visual Studios if possible) Functions or classes are ok....

    (C++ please. Make sure it works in Visual Studios if possible) 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. Data for descending_mostly_sorted.txt {1001000.0, 10000.0, 9990.0, 9980.0, 9970.0, 9960.0, 9950.0, 9940.0, 9930.0, 9920.0, 9910.0, 9900.0, 9890.0, 9880.0, 9870.0, 9860.0,...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay ...

    Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay Due: 4/22/2019(Monday) Introduction And now for something completely different.   Different sorting algorithms are better for different size data sets.   Other sorting algorithms are better for data sets of a specific type – for instance, data that is already ordered. In this assignment you will implement four different sorting algorithms and collect statistics for each of those algorithms while sorting multiple different...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

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