Question

In: Computer Science

Objectives: Sort data objects Compare the CPU efficiency between the object array and ArrayList used in...

Objectives:
Sort data objects
Compare the CPU efficiency between the object array and ArrayList used in the sorting operation.
Input file: Data.csv (file is to big;I cannot update, you can use any data file)
there are less than 20,000 records in Data.csv Your program will skip/reject dirty data lines. For example, if a line contains 2 fields, it is considered as a dirty data line and will be rejected/skipped by your program.
You are required to develop a Java program to:
1. Read all data records from Data.csv file into 2 data structures:
An object array of MyRecord
java.util.ArrayList
2. Sort records in descending order by Price (largest first) and display the CPU time used for such an operation. Perform these operations against the object array of MyRecord and java.util.ArrayList.
Fields data is:
119736,CLAY COUNTY,792148.9
448094,CLAY COUNTY,1438163.57
206893,CLAY COUNTY,192476.78
333743,CLAY COUNTY,86854.48
172534,CLAY COUNTY,246144.49
785275,CLAY COUNTY,884419.17
995932,CLAY COUNTY,20610000
223488,CLAY COUNTY,348374.25
433512,CLAY COUNTY,265821.57
142071,CLAY COUNTY,1010842.56
253816,CLAY COUNTY,1117791.48
894922,CLAY COUNTY,33952.19
422834,CLAY COUNTY,66755.39
582721,CLAY COUNTY,42826.99
842700,CLAY COUNTY,50656.8
874333,CLAY COUNTY,67905.07
580146,CLAY COUNTY,66938.9
456149,CLAY COUNTY,86421.04
767862,CLAY COUNTY,73798.5
353022,CLAY COUNTY,62467.29
367814,CLAY COUNTY,42727.74
671392,CLAY COUNTY,11700000
772887,CLAY COUNTY,2099127.76
983122,CLAY COUNTY,211372.57
934215,CLAY COUNTY,157171.16

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

class MyRecord implements Comparable<MyRecord> {
    String name;
    double price;
    long id;
    MyRecord(String name, double price, long id){
        this.name = name;
        this.price = price;
        this.id = id;
    }

    public int compareTo(MyRecord record){  
        if(price == record.price)  
            return 0;  
        else if(price > record.price)  
            return 1;  
        else  
            return -1;  
    }  
    public String toString(){
        return "id: "+ id+" name: "+ name+" price: "+price;
    }
}

public class test {

    static void merge(MyRecord arr[], int l, int m, int r) { 
        int i, j, k; 
        int n1 = m - l + 1; 
        int n2 = r - m; 
    
        
        MyRecord L[] = new MyRecord[n1];
        MyRecord R[] = new MyRecord[n2]; 
    
        /* Copy data to temp arrays L[] and R[] */
        for (i = 0; i < n1; i++) 
            L[i] = arr[l + i]; 
        for (j = 0; j < n2; j++) 
            R[j] = arr[m + 1 + j]; 
    
        /* Merge the temp arrays back into arr[l..r]*/
        i = 0; // Initial index of first subarray 
        j = 0; // Initial index of second subarray 
        k = l; // Initial index of merged subarray 
        while (i < n1 && j < n2) { 
            if (L[i].compareTo(R[j]) >= 0) { 
                arr[k] = L[i]; 
                i++; 
            } 
            else { 
                arr[k] = R[j]; 
                j++; 
            } 
            k++; 
        } 
    
        /* Copy the remaining elements of L[], if there 
        are any */
        while (i < n1) { 
            arr[k] = L[i]; 
            i++; 
            k++; 
        } 
    
        /* Copy the remaining elements of R[], if there 
        are any */
        while (j < n2) { 
            arr[k] = R[j]; 
            j++; 
            k++; 
        } 
    } 
    
    /* l is for left index and r is right index of the 
    sub-array of arr to be sorted */
    static void mergeSort(MyRecord arr[], int l, int r) 
    { 
        if (l < r) { 
            // Same as (l+r)/2, but avoids overflow for 
            // large l and h 
            int m = l + (r - l) / 2; 
    
            // Sort first and second halves 
            mergeSort(arr, l, m); 
            mergeSort(arr, m + 1, r); 
    
            merge(arr, l, m, r); 
        } 
    } 
    public static void main(String[] args) throws FileNotFoundException {
        Scanner fin = new Scanner(new File("data.csv")); //reading the data from the data.csv file
        ArrayList<MyRecord> myRecordList = new ArrayList<>(); // creating the list to store the data.

        while (fin.hasNext()) {
            String str[] = fin.nextLine().split(",");
            
            if (str.length != 3){ //checking whether the number of items is exactly 3 otherwise rejecting.
                continue;
            }
            // as no inforamaition is provided for the meaning of the each column considering the first value as the id, second value as the name, third value as the price
            MyRecord record = new MyRecord(str[1], Double.parseDouble(str[2]), Long.parseLong(str[0]) ); //creating the object of class MyRecord
            myRecordList.add(record); //adding to the list
            
        }

        fin.close();
        MyRecord[] myRecordArray = new MyRecord[myRecordList.size()]; //  declaring the array

        myRecordArray   = myRecordList.toArray(myRecordArray);       // creating the copy of the list to array

        long startTime = System.nanoTime(); // measuring the time for array sorting

        mergeSort(myRecordArray,0, myRecordArray.length -1); // using the merge sort to sort the array;
        
        long endTime = System.nanoTime();

        long timeForArraySorting = endTime -startTime; // calculating the time for array sorting

        startTime = System.nanoTime();

        Collections.sort(myRecordList, Collections.reverseOrder()); // sorting in the reverse order 

        endTime = System.nanoTime();

        long timeForListSorting = endTime - startTime;

        //priting the array of myrecord in descending order.
        for( MyRecord record: myRecordArray){
            System.out.println(record);
        }
        // time taken comparison
        System.out.println("Time taken for array sorting: "+timeForArraySorting);
        System.out.println("Time taken for list sorting: "+timeForListSorting);
        
        
    }

}

Related Solutions

You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
Compare and Contrast Bubble Sort with Merge Sort. Discuss about the efficiency of both.
Compare and Contrast Bubble Sort with Merge Sort. Discuss about the efficiency of both.
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should...
JAVASCRIPT Create an array of 5 objects named "movies" Each object in the movies array, should have the following properties: Movie Name Director Name Year Released WasSuccessful (this should be a boolean and at least 2 should be false) Genre Loop through all of the objects in Array If the movie is successful, display all the movie information on the page. These movies were a success: Title: Forrest Gump Year Realeased: 1994 Director: Robert Zemeckis Genre: Comedy
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
Instructions Create an application to accept data for an array of five CertOfDeposit objects, and then...
Instructions Create an application to accept data for an array of five CertOfDeposit objects, and then display the data. import java.time.*; public class CertOfDeposit { private String certNum; private String lastName; private double balance; private LocalDate issueDate; private LocalDate maturityDate; public CertOfDeposit(String num, String name, double bal, LocalDate issue) { } public void setCertNum(String n) { } public void setName(String name) { } public void setBalance(double bal) { } public void issueDate(LocalDate date) { } public String getCertNum() { }...
The insertion sort algorithm updates its sorted array as each new data value is entered. It...
The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step. • Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case. • Assume that we have thus far read N values and these are sorted in correct order (largest to...
Your nonprofit organization wishes to increase the efficiency of its fundraising efforts. What sort of data...
Your nonprofit organization wishes to increase the efficiency of its fundraising efforts. What sort of data might be useful to achieve this goal? How might BI tools be used to analyze this data?
Compare and contrast the efficiency and effectiveness of an in-house data center, an arrangement with an...
Compare and contrast the efficiency and effectiveness of an in-house data center, an arrangement with an outsourcing vendor to own and operate a data center for you, a service bureau, an application service provider (ASP).
Design and build objects to be used by a real estate company for listing the properties for sale. A base object of a property is necessary.
IN JAVA OOPDesign and build objects to be used by a real estate company for listing the properties for sale. A base object of a property is necessary. Decide what common about all properties and include these in the property object. Different type of properties are land, commercial and residential. A residential property can be single family and multi family. A commercial can be a commercial can be either a farm or non-farm.Look at real estate websites and decide what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT