In: Computer Science
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);
}
}