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
  • c++ program Functions or classes are ok. Please read data into program from descending_mostly_sorted.txt and store...

    c++ program 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 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:...

  • .Using OOP, write a C++ program that will read in a file of names. The file...

    .Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...

  • 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,...

  • Please do in C# with the code available to copy :) Program 4: Design (pseudocode) and...

    Please do in C# with the code available to copy :) Program 4: Design (pseudocode) and implement (source code) a program (name it MinMaxAvg) to determine the highest grade, lowest grade, and the average of all grades in a 4-by-4 two-dimensional arrays of integer grades (representing 4 students’ grades on 4 tests). The program main method populates the array (name it Grades) with random grades between 0 and 100 and then displays the grades as shown below. The main method...

  • 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....

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