diff --git a/t1/main.c b/t1/main.c index 38486833deeb8460f063a9eb1887240220ce63c5..a7d6169b75ecf1fe42d48d1f1faf98fefd999e7c 100644 --- a/t1/main.c +++ b/t1/main.c @@ -6,7 +6,7 @@ void imprimeVetor(int v[], int tam) { printf("\n["); - for (int i=0;i<10;i++) { + for (int i=0;i<20;i++) { printf("%d ", v[i]); } printf("...%d]\n", v[tam-1]); @@ -14,7 +14,7 @@ void imprimeVetor(int v[], int tam) { void geraVetorAleatorio(int v[], int tam) { for (int i=0;i<tam;i++) { - v[i] = rand() % 100000 + 1; + v[i] = rand() % 100 + 1; } } @@ -34,8 +34,8 @@ int main(){ //Dica: somente é posśivel criar vetores grandes utilizando alocação dinâmica de memória //Veja um exemplo de alocação dinâmica a seguir - int tamVetor = 10000; - int* vetor = malloc(tamVetor * sizeof(int)); + int tam = 10; + int* vetor = malloc(10000 * sizeof(int)); getNome(nome); printf("Trabalho de %s\n", nome); printf("GRR %u\n", getGRR()); @@ -45,11 +45,6 @@ int main(){ return 1; } - int* vetor1 = malloc(tamVetor * sizeof(int)); - if(vetor1 == NULL){ - printf("Falha fatal. Impossível alocar memoria."); - return 1; - } printf("----------------------------------------------------\n"); printf("Comecando os teste com os vetores de busca...\n"); @@ -59,79 +54,79 @@ int main(){ //TESTE 1 numComp=0; - printf("\nbuscando o valor -6 no vetor..."); - geraVetor(vetor,100); - imprimeVetor(vetor, 100); + printf("\nbuscando o valor -5 no vetor..."); + geraVetor(vetor,tam); + imprimeVetor(vetor, tam); start = clock();//start recebe o "ciclo" corrente - idxBusca = buscaSequencial(vetor,100, -6, &numComp); + idxBusca = buscaSequencial(vetor,tam, -5, &numComp); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; printf("(Encontrado no indice: %d | Nº de comp: %d | Tempo total: %f)\n", idxBusca, numComp, total); - + //TESTE 2 numComp=0; - printf("\nbuscando o valor 100 no vetor..."); - geraVetor(vetor,1000); - imprimeVetor(vetor, 1000); + printf("\nbuscando o valor 14 no vetor..."); + geraVetor(vetor,5*tam); + imprimeVetor(vetor, 5*tam); start = clock();//start recebe o "ciclo" corrente - idxBusca = buscaSequencial(vetor,1000, 100, &numComp); + idxBusca = buscaSequencial(vetor,5*tam, 14, &numComp); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; printf("(Encontrado no indice: %d | Nº de comp: %d | Tempo total: %f)\n", idxBusca, numComp, total); - + //TESTE 3 numComp=0; - printf("\nbuscando o valor 7500 no vetor..."); - geraVetor(vetor,10000); - imprimeVetor(vetor, 10000); + printf("\nbuscando o valor 43 no vetor..."); + geraVetor(vetor,10*tam); + imprimeVetor(vetor, 10*tam); start = clock();//start recebe o "ciclo" corrente - idxBusca = buscaSequencial(vetor,10000, 7500, &numComp); + idxBusca = buscaSequencial(vetor,10*tam, 43, &numComp); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; printf("(Encontrado no indice: %d | Nº de comp: %d | Tempo total: %f)\n", idxBusca, numComp, total); - + printf("\n--->BUSCA BINARIA\n"); - geraVetor(vetor,100); + geraVetor(vetor,tam); //TESTE 1 numComp=0; printf("\nbuscando o valor -6 no vetor..."); - imprimeVetor(vetor, 100); + imprimeVetor(vetor, tam); start = clock();//start recebe o "ciclo" corrente - idxBusca = buscaBinaria(vetor,100, -6, &numComp); + idxBusca = buscaBinaria(vetor,tam, -6, &numComp); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; printf("(Encontrado no indice: %d | Nº de comp: %d | Tempo total: %f)\n", idxBusca, numComp, total); - + //TESTE 2 numComp=0; - printf("\nbuscando o valor 100 no vetor..."); - geraVetor(vetor,1000); - imprimeVetor(vetor, 1000); + printf("\nbuscando o valor 14 no vetor..."); + geraVetor(vetor,5*tam); + imprimeVetor(vetor, 5*tam); start = clock();//start recebe o "ciclo" corrente - idxBusca = buscaBinaria(vetor,1000, 100, &numComp); + idxBusca = buscaBinaria(vetor,5*tam, 14, &numComp); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; printf("(Encontrado no indice: %d | Nº de comp: %d | Tempo total: %f)\n", idxBusca, numComp, total); - + //TESTE 3 numComp=0; - printf("\nbuscando o valor 2500 no vetor..."); - geraVetor(vetor,10000); - imprimeVetor(vetor, 10000); + printf("\nbuscando o valor 43 no vetor..."); + geraVetor(vetor,10*tam); + imprimeVetor(vetor, 10*tam); start = clock();//start recebe o "ciclo" corrente - idxBusca = buscaBinaria(vetor,10000, 2500, &numComp); + idxBusca = buscaBinaria(vetor,10*tam, 43, &numComp); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; printf("(Encontrado no indice: %d | Nº de comp: %d | Tempo total: %f)\n", idxBusca, numComp, total); - + printf("\n----------------------------------------------------\n"); printf("Comecando os teste com os vetores de ordenacao...\n"); @@ -140,38 +135,50 @@ int main(){ printf("\n--->INSERTION SORT\n"); //TESTE 1 - geraVetorAleatorio(vetor,10); - printf("\nOrdenando um vetor tam = 100"); - imprimeVetor(vetor, 10); + geraVetorAleatorio(vetor,tam); + numComp=0; + printf("\nOrdenando um vetor de tam = 10"); + imprimeVetor(vetor, tam); start = clock();//start recebe o "ciclo" corrente - numComp = insertionSort(vetor,10); + numComp = insertionSort(vetor,tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 10); + imprimeVetor(vetor, tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 2 - geraVetorAleatorio(vetor,1000); - printf("\nOrdenando um vetor tam = 1000"); - imprimeVetor(vetor, 1000); + geraVetorAleatorio(vetor,5*tam); + printf("\nOrdenando um vetor tam = 50"); + imprimeVetor(vetor, 5*tam); start = clock();//start recebe o "ciclo" corrente - numComp = insertionSort(vetor,1000); + numComp = insertionSort(vetor,5*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 1000); + imprimeVetor(vetor, 5*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 3 - geraVetorAleatorio(vetor,10000); - printf("\nOrdenando um vetor tam = 10000"); - imprimeVetor(vetor, 10000); + geraVetorAleatorio(vetor,10*tam); + printf("\nOrdenando um vetor tam = 100"); + imprimeVetor(vetor, 10*tam); start = clock();//start recebe o "ciclo" corrente - numComp = insertionSort(vetor,10000); + numComp = insertionSort(vetor,10*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 10000); + imprimeVetor(vetor, 10*tam); + printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); + + //TESTE 4 + geraVetorAleatorio(vetor,50*tam); + printf("\nOrdenando um vetor tam = 500"); + imprimeVetor(vetor, 50*tam); + start = clock();//start recebe o "ciclo" corrente + numComp = insertionSort(vetor,50*tam); + end = clock();//end recebe o "ciclo" corrente + total = ((double)end - start)/CLOCKS_PER_SEC; + imprimeVetor(vetor, 50*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); printf("\n"); @@ -180,38 +187,49 @@ int main(){ printf("\n--->SELECTION SORT\n"); //TESTE 1 - geraVetorAleatorio(vetor,100); - printf("\nOrdenando um vetor tam = 100"); - imprimeVetor(vetor, 100); + geraVetorAleatorio(vetor,tam); + printf("\nOrdenando um vetor tam = 10"); + imprimeVetor(vetor, tam); start = clock();//start recebe o "ciclo" corrente - numComp = selectionSort(vetor,100); + numComp = selectionSort(vetor,tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 100); + imprimeVetor(vetor, tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 2 - geraVetorAleatorio(vetor,1000); - printf("\nOrdenando um vetor tam = 1000"); - imprimeVetor(vetor, 1000); + geraVetorAleatorio(vetor,5*tam); + printf("\nOrdenando um vetor tam = 50"); + imprimeVetor(vetor, 5*tam); start = clock();//start recebe o "ciclo" corrente - numComp = selectionSort(vetor,1000); + numComp = selectionSort(vetor,5*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 1000); + imprimeVetor(vetor, 5*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 3 - geraVetorAleatorio(vetor,10000); - printf("\nOrdenando um vetor tam = 10000"); - imprimeVetor(vetor, 10000); + geraVetorAleatorio(vetor,10*tam); + printf("\nOrdenando um vetor tam = 100"); + imprimeVetor(vetor, 10*tam); + start = clock();//start recebe o "ciclo" corrente + numComp = selectionSort(vetor,10*tam); + end = clock();//end recebe o "ciclo" corrente + total = ((double)end - start)/CLOCKS_PER_SEC; + imprimeVetor(vetor, 10*tam); + printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); + + //TESTE 4 + geraVetorAleatorio(vetor,50*tam); + printf("\nOrdenando um vetor tam = 500"); + imprimeVetor(vetor, 50*tam); start = clock();//start recebe o "ciclo" corrente - numComp = selectionSort(vetor,10000); + numComp = selectionSort(vetor,50*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 10000); + imprimeVetor(vetor, 50*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); printf("\n"); @@ -219,84 +237,105 @@ int main(){ printf("\n--->MERGE SORT\n"); //TESTE 1 - geraVetorAleatorio(vetor,100); - printf("\nOrdenando um vetor tam = 100"); - imprimeVetor(vetor, 100); + geraVetorAleatorio(vetor,tam); + printf("\nOrdenando um vetor tam = 10"); + imprimeVetor(vetor, tam); start = clock();//start recebe o "ciclo" corrente - numComp = mergeSort(vetor,100); + numComp = mergeSort(vetor,tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 100); + imprimeVetor(vetor, tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 2 - geraVetorAleatorio(vetor,1000); - printf("\nOrdenando um vetor tam = 1000"); - imprimeVetor(vetor, 1000); + geraVetorAleatorio(vetor,5*tam); + printf("\nOrdenando um vetor tam = 50"); + imprimeVetor(vetor, 5*tam); start = clock();//start recebe o "ciclo" corrente - numComp = mergeSort(vetor,1000); + numComp = mergeSort(vetor,5*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 1000); + imprimeVetor(vetor, 5*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 3 - geraVetorAleatorio(vetor,10000); - printf("\nOrdenando um vetor tam = 10000"); - imprimeVetor(vetor, 10000); + geraVetorAleatorio(vetor,10*tam); + printf("\nOrdenando um vetor tam = 100"); + imprimeVetor(vetor, 10*tam); start = clock();//start recebe o "ciclo" corrente - numComp = mergeSort(vetor,10000); + numComp = mergeSort(vetor,10*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 10000); + imprimeVetor(vetor, 10*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); + //TESTE 4 + geraVetorAleatorio(vetor,50*tam); + printf("\nOrdenando um vetor tam = 500"); + imprimeVetor(vetor, 50*tam); + start = clock();//start recebe o "ciclo" corrente + numComp = mergeSort(vetor,50*tam); + end = clock();//end recebe o "ciclo" corrente + total = ((double)end - start)/CLOCKS_PER_SEC; + imprimeVetor(vetor, 50*tam); + printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); + printf("\n"); printf("\n--->QUICK SORT\n"); //TESTE 1 - geraVetorAleatorio(vetor,100); - printf("\nOrdenando um vetor tam = 100"); - imprimeVetor(vetor, 100); + geraVetorAleatorio(vetor, tam); + printf("\nOrdenando um vetor tam = 10"); + imprimeVetor(vetor, tam); start = clock();//start recebe o "ciclo" corrente - numComp = quickSort(vetor,100); + numComp = quickSort(vetor, tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 100); + imprimeVetor(vetor, tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - + //TESTE 2 - geraVetorAleatorio(vetor,1000); - printf("\nOrdenando um vetor tam = 1000"); - imprimeVetor(vetor, 1000); + geraVetorAleatorio(vetor,5*tam); + printf("\nOrdenando um vetor tam = 50"); + imprimeVetor(vetor, 5*tam); start = clock();//start recebe o "ciclo" corrente - numComp = quickSort(vetor,1000); + numComp = quickSort(vetor,5*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 1000); + imprimeVetor(vetor, 5*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); - - + + //TESTE 3 - geraVetorAleatorio(vetor,10000); - printf("\nOrdenando um vetor tam = 10000"); - imprimeVetor(vetor, 10000); + geraVetorAleatorio(vetor,10*tam); + printf("\nOrdenando um vetor tam = 100"); + imprimeVetor(vetor, 10*tam); start = clock();//start recebe o "ciclo" corrente - numComp = quickSort(vetor,10000); + numComp = quickSort(vetor,10*tam); end = clock();//end recebe o "ciclo" corrente total = ((double)end - start)/CLOCKS_PER_SEC; - imprimeVetor(vetor, 10000); + imprimeVetor(vetor, 10*tam); printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); + //TESTE 4 + geraVetorAleatorio(vetor,50*tam); + printf("\nOrdenando um vetor tam = 500"); + imprimeVetor(vetor, 50*tam); + start = clock();//start recebe o "ciclo" corrente + numComp = quickSort(vetor,50*tam); + end = clock();//end recebe o "ciclo" corrente + total = ((double)end - start)/CLOCKS_PER_SEC; + imprimeVetor(vetor, 50*tam); + printf("(Nº de comp: %d | Tempo total: %f)\n",numComp, total); + printf("\n"); //É obrigatório que você libere a memória alocada dinâmicamente via fee free(vetor); - free(vetor1); return 0; } diff --git a/t1/main.o b/t1/main.o index 324e61451b6ea13fa7d561eb207846e763003ac6..74333eb9a7492ad220eaee0324cafd606e7e546b 100644 Binary files a/t1/main.o and b/t1/main.o differ diff --git a/t1/makefile b/t1/makefile index 2ed9e7f9438a6411ac8f2e2b5d8d4051982fed3b..9d2882577aea5d8728051dd8765e218c64a3e8b2 100644 --- a/t1/makefile +++ b/t1/makefile @@ -1,5 +1,5 @@ parametrosCompilacao=-Wall #-Wshadow -nomePrograma=trab1grr20206873 +nomePrograma=trab all: $(nomePrograma) diff --git a/t1/ordenacao.c b/t1/ordenacao.c index 9ce1ed02aae8b9b5ffd0b378d103410d7e1bf4e5..b5a20d12886c05c2feef543576a855d7789caf75 100644 --- a/t1/ordenacao.c +++ b/t1/ordenacao.c @@ -6,7 +6,7 @@ void getNome(char nome[]){ //substitua por seu nome - strncpy(nome, "Vinicius Fontoura de Abreu", MAX_CHAR_NOME); + strncpy(nome, "Vinicius Fontoura & Carla Capurro", MAX_CHAR_NOME); nome[MAX_CHAR_NOME-1] = '\0';//adicionada terminação manual para caso de overflow } @@ -41,11 +41,14 @@ int buscaSequencial(int vetor[], int tam, int valor, int* numComparacoes){ //usada para processar a busca binaria corretamente int buscaBinariaCompleta(int vetor[], int tam, int inicio, int valor, int* numComparacoes) { int meio; + + //caso base if (inicio > tam) return -1; meio = (tam+inicio)/2; + //confere no meio if (vetor[meio] == valor) { *numComparacoes+=1; return meio; @@ -66,10 +69,12 @@ int buscaBinaria(int vetor[], int tam, int valor, int* numComparacoes){ a = 0; b = tam; meio = tam/2; + //caso base if (a > b) { return -1; } + //confere no meio if (vetor[meio] == valor) { *numComparacoes+=1; return meio; @@ -85,22 +90,52 @@ int buscaBinaria(int vetor[], int tam, int valor, int* numComparacoes){ return buscaBinariaCompleta(vetor,b,meio+1,valor,numComparacoes); } +int busca(int vetor[], int a, int b, int valor, int* numComparacoes) { + int meio; + //caso base + if (a>b) { + if (valor > vetor[a]) { + *numComparacoes+=1; + return a+1; + } + return a; + } + + meio = (a+b)/2; + //confere no meio + if (vetor[meio] == valor) { + *numComparacoes+=1; + return meio+1; + } + + // busca a esquerda + if (vetor[meio] > valor) { + *numComparacoes+=1; + return busca(vetor,a,meio-1,valor,numComparacoes); + } + //busca a direita + *numComparacoes+=1; + return busca(vetor,meio+1,b,valor,numComparacoes); + +} void troca(int vetor[], int a, int b) { int x; x = vetor[a]; vetor[a] = vetor[b]; vetor[b] = x; + //printf("troca de %d com %d\n", vetor[a], vetor[b]); } void insere(int vetor[], int a, int b,int* numComparacoes) { int posicao,i; //busca a posicao correta do ultimo elemento - posicao = buscaSequencial(vetor,b-1,vetor[b],numComparacoes); + posicao = busca(vetor, a, b, vetor[b], numComparacoes); + //printf("a posição correta de %d é no indice %d\n", vetor[b], posicao); i = b; //enquanto o ultimo elemento for maior que o elemento na sua posicao correta - while (i > posicao+1) { + while (i > posicao) { //faz a troca ate chegar na posicao correta troca(vetor,i,i-1); i--; @@ -109,11 +144,11 @@ void insere(int vetor[], int a, int b,int* numComparacoes) { int insertionSortCompleto(int vetor[], int a, int b, int* numComparacoes) { if (a >= b) - return b; + return a; //vai ser chamado ate chegar no vetor de tamanho 1 insertionSortCompleto(vetor,a,b-1,numComparacoes); //insere cada elemento na posicao correta - insere(vetor,a,b,numComparacoes); + insere(vetor,a,b-1,numComparacoes); return *numComparacoes; } @@ -121,11 +156,12 @@ int insertionSortCompleto(int vetor[], int a, int b, int* numComparacoes) { int insertionSort(int vetor[], int tam){ int numComparacoes; numComparacoes = 0; + int a = 0; //caso base - if (0 >= tam) - return tam; + if (a >= tam) + return a; //faz a primeira chamada - insertionSortCompleto(vetor,0,tam,&numComparacoes); + insertionSortCompleto(vetor, a, tam, &numComparacoes); return numComparacoes; } diff --git a/t1/ordenacao.o b/t1/ordenacao.o index 274fefcd09e92e1a11fac79618779b8ff9bfbae3..3c330f3c4de1b12bd427ff85e0a85936f811e7b1 100644 Binary files a/t1/ordenacao.o and b/t1/ordenacao.o differ diff --git a/t1/trab b/t1/trab new file mode 100755 index 0000000000000000000000000000000000000000..2c2f2482cccf472f2cd9ee7474170f07893d1485 Binary files /dev/null and b/t1/trab differ