Question

For this program, you will be working with data from the NASA website which lists Near...

For this program, you will be working with data from the NASA website which lists Near Earth Objects detected by the JPL Sentry System. You are given a text file listing the designation and impact probability (with Earth, generally within the next 100 years) of 585 Near Earth Objects. Your job will be to sort these objects by their impact probabilities.

Input File Format

The input file contains 585 records. Each record is on a separate line. Each line contains a designation string and an impact probability. The designation string contains no spaces. There is a space between the designation string and the impact probability. The impact probability is a floating point number. Below are three example records from the input file:

2015_KP18 0.00004000000 2015_MN11 0.00000510000 2015_KR157 0.00000000030

Sorting Algorithm Requirements

For this assignment, you will implement two different sorting techniques and examine their performances: MergeSort and QuickSort. Do not use a C/C++ library for the sorting. You must write your own code to perform the sorting operations. For QuickSort, choose the pivot using the median-of-three approach. You must write a separate program for each of these two sorting techniques. Your programs should time the execution time for each sorting method. http://www.cplusplus.com/reference/ctime/clock/ provides a simple mechanism for timing code using clicks. Only time the sort routines, not the entire program.

Program Input/Output Requirements

Your programs must use standard input with file redirection (similar to Programming Assignment 1). Each of your two programs must create a new file named neo_sorted.txt which contains the records from the input file sorted in ascending order of impact probability. The format of the output file must be identical to that of the input file. The only difference between the input file and output file will be the order of the records.

Each of your two programs must also print the number of clicks taken by the sorting procedure.

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

#include <iostream>
#include <string>
#include <time.h>
using namespace std;

void swap(int* a, int* b)
{
   int t = *a;
   *a = *b;
   *b = t;
}

void merge(int *a, int low, int high, int mid)
{
   int i, j, k, c[50];
   i = low;
   k = low;
   j = mid + 1;
   while (i <= mid && j <= high)
   {
       if (a[i] < a[j])
       {
           c[k] = a[i];
           k++;
           i++;
       }
       else
       {
           c[k] = a[j];
           k++;
           j++;
       }
   }
   while (i <= mid)
   {
       c[k] = a[i];
       k++;
       i++;
   }
   while (j <= high)
   {
       c[k] = a[j];
       k++;
       j++;
   }
   for (i = low; i < k; i++)
   {
       a[i] = c[i];
   }
}
void mergesort(int *a, int low, int high)
{
   int mid;
   if (low < high)
   {
       mid = (low + high) / 2;
       mergesort(a, low, mid);
       mergesort(a, mid + 1, high);
       merge(a, low, high, mid);
   }
   return;
}
//Quick sort
int partition(int arr[], int l, int h)
{
   int x = arr[h]; // pivot
   int i = (l - 1);

   for (int j = l; j <= h - 1; j++)
   {
      
       if (arr[j] <= x)
       {
           i++;   
           swap(&arr[i], &arr[j]);
       }
   }
   swap(&arr[i + 1], &arr[h]);
   return (i + 1);
}

void quickSort(int arr[], int l, int h)
{
   if (l < h)
   {
       int p = partition(arr, l, h);
       quickSort(arr, l, p - 1);
       quickSort(arr, p + 1, h);
   }
}


int main()
{
   //Read data
   int *a; //you can define a struct to include string and impact and then use the impact
   //I'll focus on core part, i.e merge sort and quick sort
   clock_t t = clock();
   mergesort(a, 0, 584); //Since 585 no of records are there
   t = clock() - t;
   cout << "It took me " << ((float)t) / CLOCKS_PER_SEC << " seconds for merge sort" << endl;]
   t = clock();
   quickSort(a, 0, 584);
   t = clock() - t;
   cout << "It took me " << ((float)t) / CLOCKS_PER_SEC << " seconds for quick sort" << endl; ]
   system("pause");
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
For this program, you will be working with data from the NASA website which lists Near...
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
  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

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

  • FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...

    FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named TextBookSort.java. Include these steps: Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price. This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java. Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for...

  • VISUAL BASIC- create a program for managing a "To Do" list. The program will need to...

    VISUAL BASIC- create a program for managing a "To Do" list. The program will need to read and update a text file named ToDoList.txt. The record structure of the file must be: 1) sort order, 2) task name and 3) desired completion date. When the program starts, it should read the contents file into a structure. The information should then be displayed in a data grid view. The data should be initially sorted by the sort order value. Afterwards, the...

  • Please help with this assignment For this assignment, you must design and develop a program to...

    Please help with this assignment For this assignment, you must design and develop a program to read any CSV file and output an HTML table. The program should be able to input any csv file with any number of fields and use the field headings in the CSV file's first row to become the table headings of the table. The program should output an HTML Bootstrap table with table headings and alternating zebra strips for rows. Your program should demonstrate...

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

  • Please write comments so the user will understand the program. The output for the calculation is provided below thw assignment. For this assignment, you will take on the role of a member of an...

    Please write comments so the user will understand the program. The output for the calculation is provided below thw assignment. For this assignment, you will take on the role of a member of an IT department. Your chief technology officer (CTO) has tasked your department with the replacement of IT equipment. Your manager does not want to buy a piece of equipment based on the brand name alone. Rather, your manager wants to know the return on investment for each...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

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