diff --git a/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt new file mode 100644 index 0000000..29a9ab8 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt @@ -0,0 +1 @@ +753045 1ms 191739 \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Player b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Player new file mode 100644 index 0000000..b1964ed Binary files /dev/null and b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Player differ diff --git a/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Player.c b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Player.c new file mode 100644 index 0000000..9bedd3a --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Player.c @@ -0,0 +1,450 @@ +/** + * @path TP02Q16 - Ordenação PARCIAL por Inserção em C/Player.c + * @description C file that implements the Player class with partial insertion sort + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2023-09-27 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_PLAYERS 4000 +#define FILE_PATH "/tmp/players.csv" + +#define MAX_NAME_SIZE 40 +#define MAX_COLLEGE_SIZE 60 +#define MAX_BIRTH_CITY_SIZE 40 +#define MAX_BIRTH_STATE_SIZE 40 + +#define MAX_LINE_SIZE 300 +#define MAX_ATTRIBUTE_SIZE 100 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Player { + int id; + char *name; + int height; + int weight; + int birthYear; + char *birthCity; + char *birthState; + char *college; +} Player; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Player allPlayers[MAX_PLAYERS]; +int n = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + // Get next comma + *substringEnd = strchr(*substringStart, ','); + + // Get substring + if(*substringEnd) substring(attribute, *substringStart, *substringEnd - *substringStart); + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "nao informado"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n') attribute[strlen(attribute) - 1] = '\0'; +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods signatures + +// Class +Player player_newBlank(); +Player player_new(int id, char *name, int height, int weight, int birthYear, char *birthCity, char *birthState, char *college); +Player *player_clone(Player *player); +void player_print(Player *player); +Player player_read(char *line); +Player *player_searchById(int id); + +// Gets +int player_getId(Player *player); +char *player_getName(Player *player); +int player_getHeight(Player *player); +int player_getWeight(Player *player); +char *player_getCollege(Player *player); +int player_getBirthYear(Player *player); +char *player_getBirthCity(Player *player); +char *player_getBirthState(Player *player); + +// Sets +void player_setId(Player *player, int id); +void player_setName(Player *player, char *name); +void player_setHeight(Player *player, int height); +void player_setWeight(Player *player, int weight); +void player_setCollege(Player *player, char *college); +void player_setBirthYear(Player *player, int birthYear); +void player_setBirthCity(Player *player, char *birthCity); +void player_setBirthState(Player *player, char *birthState); + +// General +void startPlayers(); + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Class +Player player_newBlank() { + + Player player; + + player.id = -1; + player.height = -1; + player.weight = -1; + player.birthYear = -1; + + player.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(player.name, ""); + + player.birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char)); + strcpy(player.birthCity, ""); + + player.birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char)); + strcpy(player.birthState, ""); + + player.college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char)); + strcpy(player.college, ""); + + return player; +} + +Player player_new(int id, char *name, int height, int weight, int birthYear, char *birthCity, char *birthState, char *college) { + + Player player; + + player.id = id; + player.height = height; + player.weight = weight; + player.birthYear = birthYear; + + player.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(player.name, name); + + player.birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char)); + strcpy(player.birthCity, birthCity); + + player.birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char)); + strcpy(player.birthState, birthState); + + player.college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char)); + strcpy(player.college, college); + + return player; +} + +Player *player_clone(Player *player) { + + Player *clone = (Player *) malloc(sizeof(Player)); + + clone -> id = player -> id; + clone -> height = player -> height; + clone -> weight = player -> weight; + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, player -> name); + + clone -> birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char)); + strcpy(clone -> birthCity, player -> birthCity); + + clone -> birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char)); + strcpy(clone -> birthState, player -> birthState); + + clone -> college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char)); + strcpy(clone -> college, player -> college); + + return clone; +} + +void player_print(Player *player) { + + printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n", + player -> id, + player -> name, + player -> height, + player -> weight, + player -> birthYear, + player -> college, + player -> birthCity, + player -> birthState + ); +} + +Player player_read(char *line) { + + Player player = player_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_ATTRIBUTE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true); + player_setId(&player, atoi(attribute)); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setName(&player, attribute); + + // Get height + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setHeight(&player, atoi(attribute)); + + // Get weight + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setWeight(&player, atoi(attribute)); + + // Get college + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setCollege(&player, attribute); + + // Get birth year + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setBirthYear(&player, atoi(attribute)); + + // Get birth city + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setBirthCity(&player, attribute); + + // Get birth state + proccess_attribute(attribute, &substringStart, &substringEnd, false); + player_setBirthState(&player, attribute); + + return player; +} + +Player *player_searchById(int id) { + + for(int i = 0; i < n; i++) { + + if(player_getId(&allPlayers[i]) == id) return &allPlayers[i]; + } + return NULL; +} + +// Gets +int player_getId(Player *player) { return player -> id; } +char *player_getName(Player *player) { return player -> name; } +int player_getHeight(Player *player) { return player -> height; } +int player_getWeight(Player *player) { return player -> weight; } +char *player_getCollege(Player *player) { return player -> college; } +int player_getBirthYear(Player *player) { return player -> birthYear; } +char *player_getBirthCity(Player *player) { return player -> birthCity; } +char *player_getBirthState(Player *player) { return player -> birthState; } + +// Sets +void player_setId(Player *player, int id) { player -> id = id; } +void player_setName(Player *player, char *name) { strcpy(player -> name, name); } +void player_setHeight(Player *player, int height) { player -> height = height; } +void player_setWeight(Player *player, int weight) { player -> weight = weight; } +void player_setBirthYear(Player *player, int birthYear) { player -> birthYear = birthYear; } +void player_setBirthCity(Player *player, char *birthCity) { strcpy(player -> birthCity, birthCity); } +void player_setBirthState(Player *player, char *birthState) { strcpy(player -> birthState, birthState); } +void player_setCollege(Player *player, char *college) { strcpy(player -> college, college); } + +// General +void startPlayers() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read player from line + Player player = player_read(line); + + allPlayers[n++] = player; + + if(n >= MAX_PLAYERS) { + + perror("x Max players reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #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 + Player mainPlayers[MAX_PLAYERS]; + int m = 0; + + char in[5]; + scanf(" %[^\n]s", in); + + while(true) { + + if(isEnd(in)) break; + else { + + int id = atoi(in); + + Player *player = player_searchById(id); + + if(player) mainPlayers[m++] = *player; + + // ------------------------- // + + scanf(" %[^\n]s", in); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainPlayers array by key "yearOfBirth" using partial insertion sort, in draw case, order by key "name" + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // ----------------- // + + // Partial insertion sort with k = 10 + int k = 10; + + for(int i = 1; i < m; i++) { + + comparisons++; + + Player current = mainPlayers[i]; + int j = i - 1; + + // Compare based on birth year + while(j >= 0 && player_getBirthYear(&mainPlayers[j]) > player_getBirthYear(¤t)) { + + mainPlayers[j + 1] = mainPlayers[j]; + j--; + comparisons++; + } + + // If there's a tie in birth year, compare by name + while(j >= 0 && player_getBirthYear(&mainPlayers[j]) == player_getBirthYear(¤t)) { + + comparisons += 2; + + if(strcmp(player_getName(&mainPlayers[j]), player_getName(¤t)) > 0) { + + mainPlayers[j + 1] = mainPlayers[j]; + j--; + } + else break; + } + + mainPlayers[j + 1] = current; + + // Check if we need to maintain only the top k elements + if(i >= k) { + + comparisons++; + + for(int l = k; l < i; l++) { + + comparisons++; + + player_setBirthYear(&mainPlayers[l], INT_MAX); // Set birth year to a high value for elements outside the top k + } + + for(int l = k - 1; l >= 0; l--) { + + comparisons += 4; + + if(player_getBirthYear(&mainPlayers[l]) > player_getBirthYear(&mainPlayers[l + 1]) || + (player_getBirthYear(&mainPlayers[l]) == player_getBirthYear(&mainPlayers[l + 1]) && + strcmp(player_getName(&mainPlayers[l]), player_getName(&mainPlayers[l + 1])) > 0)) { + + Player temp = mainPlayers[l]; + mainPlayers[l] = mainPlayers[l + 1]; + mainPlayers[l + 1] = temp; + } + else break; + } + } + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_insercaoParcial.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainPlayers array + for(int i = 0; i < k; i++) player_print(&mainPlayers[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out new file mode 100644 index 0000000..0c625fa --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out @@ -0,0 +1,10 @@ +[683 ## Matt Guokas ## 190 ## 88 ## 1915 ## Saint Joseph's University ## nao informado ## nao informado] +[90 ## Buddy Jeannette* ## 180 ## 79 ## 1917 ## Washington & Jefferson College ## New Kensington ## Pennsylvania] +[3420 ## Armon Johnson ## 196 ## 107 ## 1920 ## Bemidji State University ## Gonvick ## Minnesota] +[39 ## Bob Davies* ## 185 ## 79 ## 1920 ## Seton Hall University ## Harrisburg ## Pennsylvania] +[89 ## Tony Jaros ## 190 ## 83 ## 1920 ## University of Minnesota ## Minneapolis ## Minnesota] +[150 ## Don Otten ## 208 ## 108 ## 1921 ## Bowling Green State University ## Bellefontaine ## Ohio] +[54 ## Joe Fulks* ## 196 ## 86 ## 1921 ## Murray State University ## Birmingham ## Kentucky] +[141 ## Johnny Norlander ## 190 ## 81 ## 1921 ## Hamline University ## Virginia ## Minnesota] +[213 ## Whitey Von ## 193 ## 92 ## 1921 ## nao informado ## nao informado ## nao informado] +[55 ## Bill Gabor ## 180 ## 77 ## 1922 ## Syracuse University ## nao informado ## nao informado] diff --git a/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.in b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.in new file mode 100644 index 0000000..dbaf6f9 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/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/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.out b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.out new file mode 100644 index 0000000..0c625fa --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.out @@ -0,0 +1,10 @@ +[683 ## Matt Guokas ## 190 ## 88 ## 1915 ## Saint Joseph's University ## nao informado ## nao informado] +[90 ## Buddy Jeannette* ## 180 ## 79 ## 1917 ## Washington & Jefferson College ## New Kensington ## Pennsylvania] +[3420 ## Armon Johnson ## 196 ## 107 ## 1920 ## Bemidji State University ## Gonvick ## Minnesota] +[39 ## Bob Davies* ## 185 ## 79 ## 1920 ## Seton Hall University ## Harrisburg ## Pennsylvania] +[89 ## Tony Jaros ## 190 ## 83 ## 1920 ## University of Minnesota ## Minneapolis ## Minnesota] +[150 ## Don Otten ## 208 ## 108 ## 1921 ## Bowling Green State University ## Bellefontaine ## Ohio] +[54 ## Joe Fulks* ## 196 ## 86 ## 1921 ## Murray State University ## Birmingham ## Kentucky] +[141 ## Johnny Norlander ## 190 ## 81 ## 1921 ## Hamline University ## Virginia ## Minnesota] +[213 ## Whitey Von ## 193 ## 92 ## 1921 ## nao informado ## nao informado ## nao informado] +[55 ## Bill Gabor ## 180 ## 77 ## 1922 ## Syracuse University ## nao informado ## nao informado]