Last active
February 3, 2022 12:15
-
-
Save BRINGE5/81e4df1ef8b805175a0413dd977ae115 to your computer and use it in GitHub Desktop.
Sorting generic data types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Sorting { | |
public static void main (String args[]) { | |
Student[] lst = new Student[] { | |
new Student("Mitchell", 3.9f), | |
new Student("Matthew", 3.2f), | |
new Student("Lizzy", 3.5f) | |
}; | |
Task<Student> studentSort = new Task<Student>(); | |
studentSort.getData("ascending"); | |
studentSort.setAlgorithm("selection-sort"); | |
studentSort.setComparator(new Comparator<Student>() { | |
public int compare(Student s1, Student s2) { | |
return (int) (s1.gpa - s2.gpa); | |
} | |
}); | |
studentSort.sort(lst); | |
System.out.println(Arrays.toString(lst)); | |
System.out.println(studentSort.getExecutionTime()); | |
Course[] lst2 = new Course[] { | |
new Course("Math", 261), | |
new Course("English", 101), | |
new Course("CSC", 341) | |
}; | |
Task<Course> courseSort = new Task<Course>(); | |
courseSort.getData("ascending"); | |
courseSort.setAlgorithm("insertion-sort"); | |
courseSort.setComparator(new Comparator<Course>() { | |
public int compare(Course c1, Course c2) { | |
return (int)(c1.num - c2.num); | |
} | |
}); | |
courseSort.sort(lst2); | |
System.out.println(Arrays.toString(lst2)); | |
System.out.println(courseSort.getExecutionTime()); | |
} | |
} | |
class Student{ | |
String name; | |
float gpa; | |
public Student(String n, float gpa){ name = n; this.gpa = gpa; } | |
public String toString(){ | |
return name + " GPA: " + gpa; | |
} | |
} | |
class Course{ | |
String name; | |
int num; | |
public Course(String name, int num) { | |
this.name = name; | |
this.num = num; | |
} | |
public String toString() { | |
return "Course Number: " + num + " Title: " + name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Sorts { | |
public static <T> void bubbleSort(T[] list, Comparator comp, Boolean direction) { | |
//System.out.println("bubble sort"); | |
T temp = null; | |
for(int i = 0; i < list.length; i++) { | |
for(int j = 0; j < list.length - i - 1; j++) { | |
if(comp.compare(list[j], list[j+1]) > 0) { | |
temp = list[i]; | |
list[j] = list[j+1]; | |
list[j + 1] = temp; | |
} | |
} | |
} | |
if(direction) { | |
//System.out.println("reversing"); | |
reverse(list); | |
} | |
} | |
public static <T> void selectionSort(T[] list, Comparator comp, Boolean direction) { | |
//System.out.println("selection sort"); | |
int low; | |
for(int i = 0; i < list.length; i++) { | |
low = i; | |
for(int j = i+1; j < list.length; j++) { | |
if(comp.compare(list[j], list[low]) < 0) { | |
low = j; | |
} | |
} | |
if( low != i) { | |
T temp = list[low]; | |
list[low] = list[i]; | |
list[i] = temp; | |
} | |
} | |
if(direction) { | |
//System.out.println("reversing"); | |
reverse(list); | |
} | |
} | |
public static <T> void insertionSort(T[] list, Comparator comp, boolean direction) { | |
//System.out.println("insertion sort"); | |
for(int i = 0; i < list.length; i++) { | |
T temp = list[i]; | |
int j = i; | |
while(j > 0 && comp.compare(temp, list[j-1]) < 0) { | |
list[j] = list[j-1]; | |
j = j-1; | |
} | |
list[j] = temp; | |
} | |
if(direction) { | |
//System.out.println("reversing"); | |
reverse(list); | |
} | |
} | |
private static <T> void reverse(T[] list) { | |
for(int i = 0; i < list.length / 2; i ++) { | |
T temp = list[i]; | |
list[i] = list[list.length - i -1]; | |
list[list.length - i - 1] = temp; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Task <T> { | |
int algorithmType; | |
/* | |
* TYPE 0 = SELECTION SORT | |
* TYPE 1 = BUBBLE SORT | |
* DEFAULT = INSERTION SORT | |
*/ | |
boolean direction; | |
/* | |
* TRUE = DESCENDING ORDER | |
* FALSE = ASCENDING ORDER | |
*/ | |
Comparator comp; | |
long executionTime = 0; | |
public Task() { | |
algorithmType = 0; | |
direction = false; | |
comp = null; | |
} | |
public void sort(T[] list) { | |
long startTime = System.currentTimeMillis(); | |
if(algorithmType == 0) | |
Sorts.selectionSort(list, comp, direction); | |
else if(algorithmType == 1) | |
Sorts.bubbleSort(list, comp, direction); | |
else | |
Sorts.insertionSort(list, comp, direction); | |
long endTime = System.currentTimeMillis(); | |
this.executionTime = endTime - startTime; | |
} | |
//SETS THE TYPE OF SORTING ALGORITHM THAT WILL BE USED | |
public void setAlgorithm(String type) { | |
if(type == "selection-sort") | |
this.algorithmType = 0; | |
else if(type == "bubble-sort") | |
this.algorithmType = 1; | |
else | |
this.algorithmType = 2; | |
} | |
//SETS THE DIRECTION DATA WILL BE SORTED IN | |
public void getData(String direction) { | |
if(direction == "descending") | |
this.direction = true; | |
else | |
this.direction = false; | |
} | |
//THE COMPARATOR IS DEFINED WHEN CALLING THIS METHOD AS AN ANONYMOUS CLASS | |
public void setComparator(Comparator<T> comp) { | |
this.comp = comp; | |
} | |
//RETURNS THE TIME TAKEN TO COMPLETE A SORT | |
public long getExecutionTime() { | |
return executionTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment