So I have to Create a Program where the Code Reads the CSV File (csv file consists of FirstName, lastName, ID,Mark). Then after it should sort it by it self and then print how much it takes to execute the program using hte Seleciton Sort method and the Collection.sort. When I Run it.. prints.. and If I Click Running it.. after the 5th click it will say it took 16 milliseconds to do it but for most of the part it's 0.
THe Following:
Using implemented selection sort time: 0 Milliseconds
Using implemented collection sort time: 0 Milliseconds
This is My Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class compareSort {
public static void main (String [] args) {
selectionTime ();
collectionTime ();
}
static void selectionTime () {
ArrayList <Student> students = loadFromFile ("C:\\users\\Nilesh\\Downloads\\students.csv");
long StartTime;
long EndTime;
StartTime = System.currentTimeMillis();
selectionSort (students);
EndTime = System.currentTimeMillis();
System.out.format("Using implemented selection sort time: %s Milliseconds\n", EndTime - StartTime);
}
static void collectionTime () {
ArrayList<Student> students = loadFromFile("C:\\users\\Nilesh\\Downloads\\students.csv");
long StartTime;
long EndTime;
StartTime = System.currentTimeMillis();
Collections.sort (students);
EndTime = System.currentTimeMillis();
System.out.format("Using implemented collection sort time: %s Milliseconds\n", EndTime - StartTime);
}
static <T extends Comparable<T>> void selectionSort ( ArrayList<T> array ) {
for (int checkPosition = 0; checkPosition < array.size(); checkPosition ++){
int minimumPosition = checkPosition;
for (int i = checkPosition + 1; i < array.size(); i++ ){
if ( array.get(i).compareTo(array.get(minimumPosition)) < 0 ) {
minimumPosition = i;
}
}
if (minimumPosition != checkPosition){
swap (array, checkPosition, minimumPosition);
}
}
}
static <T> void swap ( ArrayList<T> arr, int indexA, int indexB ) {
T temp = arr.get(indexA);
arr.set(indexA, arr.get(indexB));
arr.set(indexB, temp);
}
static ArrayList<Student> loadFromFile( String filePath ) {
ArrayList <Student> result = new ArrayList <Student>();
BufferedReader bufferReader = null;
String line = "";
try {
bufferReader = new BufferedReader (new FileReader(filePath));
line = bufferReader.readLine();
while ( (line = bufferReader.readLine()) != null ) {
String[] lineSplited = line.split(",");
if ( lineSplited.length == 4 ) {
String firstName = lineSplited[0];
String lastName = lineSplited[1];
int id = Integer.parseInt( lineSplited[2] );
int mark = Integer.parseInt( lineSplited[3] );
Student stu = new Student(firstName, lastName, id, mark);
result.add(stu);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not Found");
} catch (IOException e) {
System.out.println("Error While Reading the File");
}
finally {
if (bufferReader != null) {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
static void printStudents( ArrayList<Student> students) {
for (Student student : students){
System.out.println(student);
}
}
}