PLAYER CLASS WITH COMPARABLE IMPLEMENTATION
// Player class, which implements the Comparable interface with type parameter Player
public class Player implements Comparable<Player> {
private String name; // player name
private String team; // player's team
private int points; // player's points per game
// Player object constructor
public Player(String name, String team, int points) {
this.name = name; // initialize name attribute
this.team = team; // initialize team attribute
this.points = points; // initialize points attribute
}
// Name getter method
public String getName() {
return name;
}
// Team getter method
public String getTeam() {
return team;
}
// Points getter
public int getPoints() {
return points;
}
// Override the toString() method to provide custom string representation of player object
@Override
public String toString() {
return String.format("%s - %s: %d points per game", name, team, points);
}
// Override the compareTo() method to allow comparison of Player objects based on points
@Override
public int compareTo(Player other) {
return Integer.compare(this.points, other.points); // compare points of this player with another player
}
}
TEAM CLASS
import java.util.ArrayList;
import java.util.Random;
// Define team class
public class Team {
private ArrayList<Player> team;
// Constructor to initialize team object with a specified count of players
public Team(int count) {
this.team = new ArrayList<>(); // Initialize the team ArrayList
populateTeam(count); // Populate the team with players
}
// Populating team with players
private void populateTeam(int count) {
String[] names = {"LeBron James", "Kevin Durant", "Stephen Curry", "Kawhi Leonard", "Giannis Antetokounmpo", "James Harden"};
String[] teams = {"Lakers", "Nets", "Warriors", "Clippers", "Bucks", "Nets"};
Random random = new Random();
// Populating team with players
for (int i = 0; i < count; i++) {
int idx = random.nextInt(names.length);
int points = random.nextInt(30) + 10; // Generate a random points per game for the player
team.add(new Player(names[idx], teams[idx], points)); // Add a new player to the team
}
}
// Perform quick sort on team based on player points
public void quickSort() {
quickSortHelper(0, team.size() - 1); // Call helper method to perform quick sort
}
// Helper method for quick sort
private void quickSortHelper(int low, int high) {
if (low < high) {
int pi = partition(low, high); // Get partition index
quickSortHelper(low, pi - 1); // Sort left part of subarray
quickSortHelper(pi + 1, high); // Sort right part of subarray
}
}
// Method to partition array and return the partitioning index
private int partition(int low, int high) {
Player pivot = team.get(high); // Select pivot
int i = (low - 1); // Smaller element's index
// Loop to partition array
for (int j = low; j < high; j++) {
// If current element is smaller than pivot
if (team.get(j).compareTo(pivot) < 0) {
i++;
// Swap team[i] and team[j]
Player temp = team.get(i);
team.set(i, team.get(j));
team.set(j, temp);
}
}
// Swap team[i+1] and team[high] (or pivot)
Player temp = team.get(i + 1);
team.set(i + 1, team.get(high));
team.set(high, temp);
return i + 1;
}
// Override toString() method to provide a string representation of team
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Player p : team) {
sb.append(p.toString()).append("\n");
}
return sb.toString();
}
// Testing sorting algorithms
public static void main(String[] args) {
// Create a team with 10000 players
Team team = new Team(10000);
// Timing Quick Sort
long startTime = System.currentTimeMillis();
team.quickSort();
long endTime = System.currentTimeMillis();
System.out.println("Quick Sort took: " + (endTime - startTime) + " ms");
}
}
James Harden - Nets: 12 points per game
Kevin Durant - Nets: 15 points per game
Stephen Curry - Warriors: 18 points per game
LeBron James - Lakers: 29 points per game
Quick Sort took: 5 ms