Question

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

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

The c++ code for your program is as. Make sure you have a text file filled with lots and lots of numbers one number in a line. Below i will attach the python script too which will generate this txt file for you.After that use proper path of that txt file in the c++ program.

****************************myProg.cpp***************************

//============================================================================
// Name : SelectionSortTime.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <fstream>
#include <chrono>
using namespace std;
using namespace std::chrono;
void selectionSort(int arr[],int size){
   int index,temp;
   for (int i = 0; i < size-1; i++)
   {
   index = i;
   for (int j = i+1; j < size; j++) {
   if (arr[j] < arr[index]) {
   index = j;
   }
   }

   temp=arr[index];
   arr[index]=arr[i];
   arr[i]=temp;

   }
}

int main() {

   int arr[50000];
   int i=0;
      ifstream file("descending_mostly_sorted.txt");
      if(!file){
          cout<<"File not found"<<endl;
          return 0;
      }
   string str;
   while (getline(file, str))
   {
       //cout<<str<<endl;
       arr[i]=stoi(str);
       i++;
   }

   auto start = high_resolution_clock::now();
   selectionSort(arr,i);
   auto stop = high_resolution_clock::now();
   auto duration = duration_cast<milliseconds>(stop - start);

   cout<<"\nSorting descending array took "<<duration.count()<<" milliseconds"<<endl;

   auto start2 = high_resolution_clock::now();
   selectionSort(arr,i);
   auto stop2 = high_resolution_clock::now();
   auto duration2 = duration_cast<milliseconds>(stop2 - start2);

   //best case time complexity is almost same for selection sort as worst case
   cout<<"\nSorting sorted array took "<<duration2.count()<<" milliseconds"<<endl;
   //output sorted numbers to another file
   ofstream f;
       f.open("ascending.txt");
       f<<"sorted numbers\n";

       for(int j=0;j<i;j++){
           f<<arr[j]<<"\n";
       }
   return 0;
}


The text file looks like this:

Add a comment
Know the answer?
Add Answer to:
c++ program Functions or classes are ok. Please read data into program from descending_mostly_sorted.txt and store...
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
  • 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...

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

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

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object con...

    Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object consists of the following fields: int rollno String name String address Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method and place your code in a separate Java source file. Do not use a sort method from the Java collections library.

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

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

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

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