#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Estrutura para o nó da árvore binária
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Funções para inserção da árvore binária
struct Node* newNode(int data) {
struct Node
* node
= (struct Node
*)malloc(sizeof(struct Node
)); node->data = data;
node->left = node->right = NULL;
return node;
}
struct Node* insert(struct Node* node, int data) {
if (node == NULL) return newNode(data);
if (data < node->data)
node->left = insert(node->left, data);
else
node->right = insert(node->right, data);
return node;
}
// Função de ordenação: Selection Sort
void selectionSort(int arr[], int n, int* comparisons) {
int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
(*comparisons)++;
if (arr[j] < arr[min_idx])
min_idx = j;
}
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Função para gerar um vetor de inteiros únicos aleatórios
void generateRandomArray(int arr[], int size) {
int i, num, exists;
for (i = 0; i < size; i++) {
do {
exists = 0;
for (int j = 0; j < i; j++) {
if (arr[j] == num) {
exists = 1;
break;
}
}
} while (exists);
arr[i] = num;
}
}
int main() {
// Parte 1: Exibir nomes e matrículas
printf("Endryo Tavares - 202051054689\n"); printf("Pedro Guilherme - 202308418858\n"); printf("Pedro Vanderlei - 202402821865\n");
// Parte 2: Geração e Ordenação de Vetor
int arr[100];
int comparisons = 0;
generateRandomArray(arr, 100);
// Calcula o tempo de execução
clock_t start, end;
// Realizando Selection Sort
printf("\nUtilizando Selection Sort para ordenação...\n"); selectionSort(arr, 100, &comparisons);
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
// Parte 3: Apresentar resultados
for (int i = 0; i < 100; i++) {
}
printf("\nComparações realizadas: %d\n", comparisons
); printf("Tempo de execução: %f segundos\n", time_spent
);
// Parte 4: Construção da Árvore Binária
struct Node* root = NULL;
for (int i = 0; i < 100; i++) {
root = insert(root, arr[i]);
}
// Finalização
printf("\n\nAgradecemos a presença de todos!\n");
return 0;
}
I2luY2x1ZGUgPHN0ZGl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AgICAgbWluX2lkeCA9IGk7CiAgICAgICAgZm9yIChqID0gaSArIDE7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgKCpjb21wYXJpc29ucykrKzsKICAgICAgICAgICAgaWYgKGFycltqXSA8IGFyclttaW5faWR4XSkKICAgICAgICAgICAgICAgIG1pbl9pZHggPSBqOwogICAgICAgIH0KICAgICAgICB0ZW1wID0gYXJyW21pbl9pZHhdOwogICAgICAgIGFyclttaW5faWR4XSA9IGFycltpXTsKICAgICAgICBhcnJbaV0gPSB0ZW1wOwogICAgfQp9CgovLyBGdW7Dp8OjbyBwYXJhIGdlcmFyIHVtIHZldG9yIGRlIGludGVpcm9zIMO6bmljb3MgYWxlYXTDs3Jpb3MKdm9pZCBnZW5lcmF0ZVJhbmRvbUFycmF5KGludCBhcnJbXSwgaW50IHNpemUpIHsKICAgIGludCBpLCBudW0sIGV4aXN0czsKICAgIGZvciAoaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBkbyB7CiAgICAgICAgICAgIGV4aXN0cyA9IDA7CiAgICAgICAgICAgIG51bSA9IHJhbmQoKSAlIDEwMDAgKyAxOwogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGk7IGorKykgewogICAgICAgICAgICAgICAgaWYgKGFycltqXSA9PSBudW0pIHsKICAgICAgICAgICAgICAgICAgICBleGlzdHMgPSAxOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfSB3aGlsZSAoZXhpc3RzKTsKICAgICAgICBhcnJbaV0gPSBudW07CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgLy8gUGFydGUgMTogRXhpYmlyIG5vbWVzIGUgbWF0csOtY3VsYXMKICAgIHByaW50ZigiQWx1bm9zOlxuIik7CiAgICBwcmludGYoIkVuZHJ5byBUYXZhcmVzIC0gMjAyMDUxMDU0Njg5XG4iKTsKICAgIHByaW50ZigiUGVkcm8gR3VpbGhlcm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