From 5b4ddcae4d62de1e49ac2d09720714ce19cff98a Mon Sep 17 00:00:00 2001 From: Pedro Lopes Date: Tue, 24 Oct 2023 08:48:22 -0300 Subject: [PATCH] add: TP02Q18 solved --- .../753045_quicksortParcial.txt | 1 + .../Arq.java | 206 ++++++++ .../Player.java | 275 +++++++++++ .../pedro.out | 10 + .../pub.in | 465 ++++++++++++++++++ .../pub.out | 10 + 6 files changed, 967 insertions(+) create mode 100644 tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt create mode 100644 tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java create mode 100644 tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Player.java create mode 100644 tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out create mode 100644 tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in create mode 100644 tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.out diff --git a/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt new file mode 100644 index 0000000..502ac9f --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt @@ -0,0 +1 @@ +753045 31ms 106491 \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Player.java b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Player.java new file mode 100644 index 0000000..17c3351 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/Player.java @@ -0,0 +1,275 @@ +/** + * @path TP02Q18 - Quicksort PARCIAL em Java + * @description Player class implemented with parcial quicksort + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2023-09-27 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +// ---------------------------------------------------------------------------------------------------- // + +public class Player { + + // Global variables + public static final String FILE_PATH = "/tmp/players.csv"; + public static ArrayList allPlayers = new ArrayList(); + public static int comparisons = 0; + + // -------------------------- // + + // Attributes + private int id; + private String name; + private int height; + private int weight; + private String college; + private int yearOfBirth; + private String birthCity; + private String birthState; + + // Empty constructor + public Player() { + + this.id = 0; + this.name = ""; + this.height = 0; + this.weight = 0; + this.college = ""; + this.yearOfBirth = 0; + this.birthCity = ""; + this.birthState = ""; + } + + // Constructor + public Player(int id, String name, int height, int weight, String college, int yearOfBirth, String birthCity, String birthState) { + + this.id = id; + this.name = name; + this.height = height; + this.weight = weight; + this.college = college; + this.yearOfBirth = yearOfBirth; + this.birthCity = birthCity; + this.birthState = birthState; + } + + // Gets + public int getId() { return this.id; } + public String getName() { return this.name; } + public int getHeight() { return this.height; } + public int getWeight() { return this.weight; } + public String getCollege() { return this.college; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getBirthCity() { return this.birthCity; } + public String getBirthState() { return this.birthState; } + + // Sets + public void setId(int id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setHeight(int height) { this.height = height; } + public void setWeight(int weight) { this.weight = weight; } + public void setCollege(String college) { this.college = college; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setBirthCity(String birthCity) { this.birthCity = birthCity; } + public void setBirthState(String birthState) { this.birthState = birthState; } + + // Clone + public Player clone() { return new Player(this.id, this.name, this.height, this.weight, this.college, this.yearOfBirth, this.birthCity, this.birthState); } + + // Print + public void print() { + + System.out.printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n", + this.id, this.name, this.height, this.weight, this.yearOfBirth, this.college, this.birthCity, this.birthState); + } + + // Read + public void read(String line) { + + // Split line by "," + String[] splitted = line.split(",", -1); + + // Fill empty attributes + for(int i = 0; i < splitted.length; i++) { + + if(splitted[i].equals("")) splitted[i] = "nao informado"; + } + + // Set attributes + this.id = Integer.parseInt(splitted[0]); + this.name = splitted[1]; + this.height = Integer.parseInt(splitted[2]); + this.weight = Integer.parseInt(splitted[3]); + this.college = splitted[4]; + this.yearOfBirth = Integer.parseInt(splitted[5]); + this.birthCity = splitted[6]; + this.birthState = splitted[7]; + } + + // CompareTo + public int compareTo(Player other) { + + // Compare by birthState + int birthStateComparison = this.birthState.compareTo(other.birthState); + + if(birthStateComparison != 0) return birthStateComparison; + else return this.name.compareTo(other.name); + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all players function + public static void startPlayers() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize player + Player player = new Player(); + + // Read line + player.read(line); + + // Add player to array + allPlayers.add(player); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id + public static Player searchById(int id, ArrayList players) { + + // Search for player + for(int i = 0; i < players.size(); i++) { + + if(players.get(i).getId() == id) return players.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all players in CSV file + startPlayers(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print players from pub.in id entries and add to mainPlayers array + + // Initialize mainPlayers array + ArrayList mainPlayers = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize player + Player player = new Player(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + int id = Integer.parseInt(line); + + // Search for player + player = searchById(id, allPlayers); + + // Print player + if(player != null) mainPlayers.add(player); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainPlayers array by key "birthState", in draw case, order by key "name" + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // ----------------- // + + // Parcial quicksort with k = 10 + int k = 10; + + for(int i = 0; i < mainPlayers.size() - 1; i++) { + + for(int j = i + 1; j < mainPlayers.size() - 1; j++) { + + comparisons++; + + if(mainPlayers.get(i).compareTo(mainPlayers.get(j)) > 0) { + + Player temp = mainPlayers.get(i); + mainPlayers.set(i, mainPlayers.get(j)); + mainPlayers.set(j, temp); + } + else if(mainPlayers.get(i).compareTo(mainPlayers.get(j)) == 0) { + + comparisons++; + + if(mainPlayers.get(i).getName().compareTo(mainPlayers.get(j).getName()) > 0) { + + Player temp = mainPlayers.get(i); + mainPlayers.set(i, mainPlayers.get(j)); + mainPlayers.set(j, temp); + } + } + } + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_quicksortParcial.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainPlayers array + for(int i = 0; i < k; i++) mainPlayers.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out new file mode 100644 index 0000000..d12ceab --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out @@ -0,0 +1,10 @@ +[969 ## Bud Stallworth ## 196 ## 86 ## 1950 ## University of Kansas ## Hartselle ## Alabama] +[1807 ## Chuck Person ## 203 ## 99 ## 1964 ## Auburn University ## Brantley ## Alabama] +[919 ## Curtis Rowe ## 201 ## 102 ## 1949 ## University of California - Los Angeles ## Bessemer ## Alabama] +[1145 ## Leon Douglas ## 208 ## 104 ## 1954 ## University of Alabama ## Leighton ## Alabama] +[2203 ## Robert Horry ## 206 ## 99 ## 1970 ## University of Alabama ## Andalusia ## Alabama] +[2843 ## Rodney White ## 203 ## 95 ## 1959 ## University of South Alabama ## Tuskegee ## Alabama] +[1587 ## Rory White ## 203 ## 95 ## 1959 ## University of South Alabama ## Tuskegee ## Alabama] +[947 ## Travis Grant ## 201 ## 97 ## 1950 ## Kentucky State University ## Clayton ## Alabama] +[3315 ## Walter Sharpe ## 206 ## 111 ## 1986 ## University of Alabama at Birmingham ## Huntsville ## Alabama] +[3844 ## Nicolas Brussino ## 201 ## 88 ## 1993 ## nao informado ## Santa Fe ## Argentina] diff --git a/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in new file mode 100644 index 0000000..dbaf6f9 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in @@ -0,0 +1,465 @@ +10 +1022 +1026 +104 +1046 +1086 +1103 +1118 +1122 +1144 +1145 +1146 +1150 +1152 +1160 +1165 +1169 +1171 +1173 +1183 +1188 +1201 +1208 +1212 +122 +1220 +1227 +1231 +1232 +1235 +1242 +1248 +1249 +1252 +1253 +1259 +1280 +1292 +1303 +1320 +1334 +1336 +1350 +1358 +1367 +1389 +1401 +141 +1415 +1417 +1435 +144 +1442 +145 +146 +1461 +1464 +1465 +1466 +1472 +1473 +15 +150 +1501 +1508 +1525 +154 +1541 +1545 +1549 +1555 +1558 +1559 +1568 +1585 +1587 +1597 +1613 +1618 +1630 +1631 +1650 +1656 +1672 +1675 +1680 +1686 +1693 +1702 +1704 +1718 +1730 +1733 +1739 +1740 +1743 +1744 +1752 +1768 +1772 +1785 +1798 +1805 +1807 +1810 +1829 +183 +1833 +1835 +1839 +1841 +1842 +1853 +1870 +1884 +1887 +1914 +1923 +1939 +1940 +1950 +1951 +1958 +1959 +1973 +1975 +1985 +1987 +1988 +1992 +1995 +2011 +2019 +2020 +2023 +2024 +2025 +204 +2040 +2046 +2047 +2052 +2056 +2067 +2069 +2075 +208 +2087 +2088 +2092 +2107 +211 +2117 +2124 +2126 +2127 +213 +2130 +2132 +2137 +2182 +2198 +220 +2203 +2204 +2210 +2215 +2216 +2227 +2246 +2261 +2275 +2297 +2312 +2317 +2323 +2326 +2327 +233 +2360 +2361 +2383 +2398 +2402 +2405 +2406 +2408 +2429 +2433 +2448 +246 +2462 +2470 +2472 +2478 +2482 +251 +2514 +2522 +2532 +2534 +2535 +2540 +2546 +255 +2559 +2564 +2569 +2576 +2582 +2586 +2589 +2596 +2600 +2603 +2617 +2619 +2621 +2623 +2628 +2631 +2636 +264 +2643 +2653 +2654 +2655 +2663 +267 +2675 +2682 +2683 +2690 +2698 +2701 +2712 +2738 +2751 +2752 +2789 +2792 +280 +2809 +2813 +282 +2827 +2843 +2863 +2864 +2865 +2866 +287 +2870 +2880 +2888 +289 +2904 +291 +2914 +2916 +2921 +2930 +2933 +295 +2960 +2963 +298 +299 +2997 +3 +3000 +3010 +3029 +3045 +3060 +3067 +3078 +3079 +3084 +309 +3096 +3109 +3127 +3128 +313 +3138 +3139 +3149 +3157 +3189 +3208 +3227 +3229 +3241 +3261 +3277 +3284 +3289 +3299 +33 +3308 +3313 +3315 +3325 +3330 +3331 +3346 +3355 +3358 +3368 +3370 +3374 +3376 +3387 +3394 +3397 +34 +3403 +3420 +3427 +3439 +345 +3474 +3475 +3477 +3494 +3501 +3516 +3520 +3525 +3548 +3559 +3582 +3586 +3588 +3595 +3620 +3628 +3629 +3638 +3639 +3643 +3644 +3659 +3664 +3667 +3671 +3682 +3698 +370 +3719 +3728 +3729 +3734 +3744 +3745 +3746 +3757 +3778 +3780 +3783 +3787 +3789 +3795 +3814 +3821 +3826 +3829 +3834 +3838 +3844 +3850 +3851 +3862 +3877 +3896 +39 +395 +403 +419 +430 +439 +444 +46 +462 +467 +471 +479 +483 +496 +511 +539 +54 +545 +55 +558 +563 +566 +575 +587 +588 +590 +593 +599 +60 +602 +605 +610 +618 +630 +634 +640 +65 +651 +665 +672 +68 +683 +686 +7 +713 +717 +726 +738 +749 +760 +769 +772 +775 +787 +788 +79 +797 +805 +806 +808 +826 +831 +833 +845 +868 +874 +88 +886 +889 +89 +892 +90 +902 +910 +919 +921 +936 +94 +947 +953 +954 +966 +969 +975 +980 +982 +984 +FIM + diff --git a/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.out b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.out new file mode 100644 index 0000000..d12ceab --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.out @@ -0,0 +1,10 @@ +[969 ## Bud Stallworth ## 196 ## 86 ## 1950 ## University of Kansas ## Hartselle ## Alabama] +[1807 ## Chuck Person ## 203 ## 99 ## 1964 ## Auburn University ## Brantley ## Alabama] +[919 ## Curtis Rowe ## 201 ## 102 ## 1949 ## University of California - Los Angeles ## Bessemer ## Alabama] +[1145 ## Leon Douglas ## 208 ## 104 ## 1954 ## University of Alabama ## Leighton ## Alabama] +[2203 ## Robert Horry ## 206 ## 99 ## 1970 ## University of Alabama ## Andalusia ## Alabama] +[2843 ## Rodney White ## 203 ## 95 ## 1959 ## University of South Alabama ## Tuskegee ## Alabama] +[1587 ## Rory White ## 203 ## 95 ## 1959 ## University of South Alabama ## Tuskegee ## Alabama] +[947 ## Travis Grant ## 201 ## 97 ## 1950 ## Kentucky State University ## Clayton ## Alabama] +[3315 ## Walter Sharpe ## 206 ## 111 ## 1986 ## University of Alabama at Birmingham ## Huntsville ## Alabama] +[3844 ## Nicolas Brussino ## 201 ## 88 ## 1993 ## nao informado ## Santa Fe ## Argentina]