From c642f53df90861fe7b199d9ec87a779d27d4d2f4 Mon Sep 17 00:00:00 2001 From: antonioN313 <47403160+antonioN313@users.noreply.github.com> Date: Fri, 23 Jun 2023 13:07:50 -0300 Subject: [PATCH 1/2] Adicionando Versoes otimizadas de Stable Sort --- .../Exercises-BinaryInsertionSort.c | 56 +++++++++++ Sorting/Stable Sort/Exercises-MergeSort.c | 93 +++++++++++++++++++ .../Exercises-OptimizedBubbleSort.c | 42 +++++++++ .../Exercises-OptimizedInsertionSort.c | 32 +++++++ 4 files changed, 223 insertions(+) create mode 100644 Sorting/Stable Sort/Exercises-BinaryInsertionSort.c create mode 100644 Sorting/Stable Sort/Exercises-MergeSort.c create mode 100644 Sorting/Stable Sort/Exercises-OptimizedBubbleSort.c create mode 100644 Sorting/Stable Sort/Exercises-OptimizedInsertionSort.c diff --git a/Sorting/Stable Sort/Exercises-BinaryInsertionSort.c b/Sorting/Stable Sort/Exercises-BinaryInsertionSort.c new file mode 100644 index 0000000..106ad26 --- /dev/null +++ b/Sorting/Stable Sort/Exercises-BinaryInsertionSort.c @@ -0,0 +1,56 @@ +#include +#define TAM 100 + +/*a constante TAM tem um valor maior comparada com outros códigos, +para demonstrar a pesquisa e troca feita pela Busca Binária*/ + +int buscaBinaria(int *vetor, int esquerda, int direita, int Aux) { + int meio; + + while (esquerda <= direita) { + meio = (esquerda + direita) / 2; + if (vetor[meio] == Aux) + return meio; + else if (vetor[meio] < Aux) + esquerda = meio + 1; + else + direita = meio - 1; + } + + return esquerda; +} + +int main() +{ + int vetor[TAM]; + int iFor, jFor, Aux,posicao; + + printf("Insira os elementos do vetor:"); + + for(iFor = 0; iFor < TAM; iFor++){ + scanf("%d", &vetor[iFor]); + } + + //Inicio do algoritmo INSERÇÃO DIRETA BINARIA: + + for(iFor = 1; iFor < TAM; iFor++){ + Aux = vetor[iFor]; + jFor = iFor - 1; + posicao = buscaBinaria(vetor,0,jFor,Aux); + while (jFor >= posicao) { + vetor[jFor + 1] = vetor[jFor]; + jFor--; + } + vetor[jFor + 1] = Aux; + + } + + printf("Elementos do vetor em ordem ascedente:\n"); + for (iFor = 0; iFor < TAM; iFor++) { + printf("%4d", vetor[iFor]); + } + return 0; +} + + + diff --git a/Sorting/Stable Sort/Exercises-MergeSort.c b/Sorting/Stable Sort/Exercises-MergeSort.c new file mode 100644 index 0000000..f24eb79 --- /dev/null +++ b/Sorting/Stable Sort/Exercises-MergeSort.c @@ -0,0 +1,93 @@ +#include +#define TAM 30 + +void merge(int*,int,int,int); +void mergeSort(int*,int,int); + +int main() +{ + int vetor[TAM]; + int iFor; + + printf("Insira os elementos do vetor:\n"); + + for(iFor = 0; iFor < TAM; iFor++){ + scanf("%d",&vetor[iFor]); + } + + mergeSort(vetor,0,TAM-1); + + + printf("Elementos do vetor em ordem ascendente:\n"); + for (iFor = 0; iFor < TAM; iFor++) { + printf("%d ",vetor[iFor]); + } + + return 0; +} + +void mergeSort(int *vetor,int Lvetor,int Rvetor) +{ + if (Lvetor < Rvetor) + { + int meio = (Lvetor + Rvetor) / 2; + mergeSort(vetor,Lvetor,meio); + mergeSort(vetor,meio+1,Rvetor); + + merge(vetor,Lvetor,meio,Rvetor); + + } +} + +void merge(int *vetor,int Lvetor,int meio,int Rvetor) +{ + int iFor,jFor,kFor; + + const int n1 = meio - Lvetor + 1; + const int n2 = Rvetor - meio; + + int Laux[n1]; + int Raux[n2]; + + for (iFor = 0; iFor < n1; iFor++) + { + Laux[iFor] = vetor[Lvetor + iFor]; + } + for (jFor = 0; jFor < n2; jFor++) + { + Raux[jFor] = vetor[meio + 1 + jFor]; + } + + iFor = 0; + jFor = 0; + kFor = Lvetor; + + while (iFor < n1 && jFor < n2) + { + if (Laux[iFor] <= Raux[jFor]) + { + vetor[kFor] = Laux[iFor]; + iFor++; + } + else + { + vetor[kFor] = Raux[jFor]; + jFor++; + } + kFor++; + } + + while (iFor < n1) + { + vetor[kFor] = Laux[iFor]; + iFor++; + kFor++; + } + + while (jFor < n2) + { + vetor[kFor] = Raux[jFor]; + jFor++; + kFor++; + } +} diff --git a/Sorting/Stable Sort/Exercises-OptimizedBubbleSort.c b/Sorting/Stable Sort/Exercises-OptimizedBubbleSort.c new file mode 100644 index 0000000..2e82952 --- /dev/null +++ b/Sorting/Stable Sort/Exercises-OptimizedBubbleSort.c @@ -0,0 +1,42 @@ +#include +#define TAM 10 + +int main(void) +{ int Numeros [10]; + int vFor, Aux, Contador,flag = 0; + + printf("Digite 10 números: "); + + for(vFor = 0; vFor < TAM; vFor++){ + scanf("%d", &Numeros[vFor]); + } + + //Inicio do algoritmo BUBBLESORT: + + for(Contador = 1; Contador < TAM; Contador++){ + + + for (vFor = 0; vFor < TAM - 1; vFor++){ + + if (Numeros[vFor] > Numeros[vFor + 1]){ + + Aux = Numeros[vFor]; + Numeros[vFor] = Numeros [vFor + 1]; + Numeros[vFor + 1] = Aux; + flag=1; + + } + if (!flag) //Se o vetor estiver ordenado (flag = 0) ele parará de pesquisar + { + break; + } + + } + } + + printf("Elementos do array em ordem crescente:\n"); + for (vFor = 0; vFor < TAM; vFor++) { + printf("%4d", Numeros[vFor]); + } + return 0; +} \ No newline at end of file diff --git a/Sorting/Stable Sort/Exercises-OptimizedInsertionSort.c b/Sorting/Stable Sort/Exercises-OptimizedInsertionSort.c new file mode 100644 index 0000000..98e85c1 --- /dev/null +++ b/Sorting/Stable Sort/Exercises-OptimizedInsertionSort.c @@ -0,0 +1,32 @@ +#include +#define TAM 5 + +int main(void) +{ int vetor[TAM]; + int iFor, jFor, Aux; + + printf("Insira os elementos do vetor:"); + + for(iFor = 0; iFor < TAM; iFor++){ + scanf("%d", &vetor[iFor]); + } + + //Inicio do algoritmo INSERÇÃO DIRETA: + + for(iFor = 1; iFor < TAM; iFor++){ + Aux = vetor[iFor]; + jFor = iFor - 1; + while (jFor >= 0 && vetor[jFor] > Aux) + { + vetor[iFor + 1] = vetor[jFor]; + jFor--; + } + vetor[jFor + 1] = Aux; + } + + printf("Elementos do vetor em ordem ascedente:\n"); + for (iFor = 0; iFor < TAM; iFor++) { + printf("%4d", vetor[iFor]); + } + return 0; +} \ No newline at end of file From 14ca4a004b3138a443ec55da2f18d5a82500e4a4 Mon Sep 17 00:00:00 2001 From: antonioN313 <47403160+antonioN313@users.noreply.github.com> Date: Sun, 2 Jul 2023 12:39:31 -0300 Subject: [PATCH 2/2] ArrayList e Ordenacoes estaveis e instaveis --- .../Exercises-ArraylistPurchaseList.c | 125 ++++++++++++++++++ .../Exercises-StableSelectionSort.c | 44 ++++++ .../Unstable Sort/Exercises-ArrayHeapSort.c | 69 ++++++++++ Sorting/Unstable Sort/Exercises-QuickSort.c | 59 +++++++++ 4 files changed, 297 insertions(+) create mode 100644 Linears Lists/Contiguous Lists/Exercises-ArraylistPurchaseList.c create mode 100644 Sorting/Stable Sort/Exercises-StableSelectionSort.c create mode 100644 Sorting/Unstable Sort/Exercises-ArrayHeapSort.c create mode 100644 Sorting/Unstable Sort/Exercises-QuickSort.c diff --git a/Linears Lists/Contiguous Lists/Exercises-ArraylistPurchaseList.c b/Linears Lists/Contiguous Lists/Exercises-ArraylistPurchaseList.c new file mode 100644 index 0000000..b7e382e --- /dev/null +++ b/Linears Lists/Contiguous Lists/Exercises-ArraylistPurchaseList.c @@ -0,0 +1,125 @@ +/*Implementacao de uma Lista linear Contigua Dinamicamente (ArrayList)*/ + +/*O desafio para o dia é implementar uma versão simplificada de uma lista de compras usando arrays. +A lista deve permitir adicionar novos itens, remover itens e listar todos os itens. +Ao adicionar um novo item,o usuário deve inserir o nome do produto e a quantidade desejada. +Ao remover um item, o usuário deve especificar o nome do produto. +Por fim, ao listar todos os itens, a lista deve exibir o nome do produto e a quantidade em um formato legível.*/ + +#include +#include +#include + +#define TRUE 1 +#define FALSE 0 + +typedef struct { + char nome[50]; + int quantidade; +}Item; + +typedef struct{ + + Item *compras; + int tamanho; + int capacidade; + +}ListaDeCompras; + +ListaDeCompras *criarListadeCompras() { + ListaDeCompras *lista = (ListaDeCompras *) malloc(sizeof(ListaDeCompras)); + lista->compras = NULL; + lista->tamanho = 0; + lista->capacidade = 0; + return lista; +} + +void adicionarItem(ListaDeCompras **lista, char *nome, int quantidade) { + for (int i = 0; i < (*lista)->tamanho; i++) { + if (strcmp((*lista)->compras[i].nome, nome) == 0) { + printf("O produto já está na lista de compras.\n"); + return; + } + } + + if ((*lista)->tamanho == (*lista)->capacidade) { + (*lista)->capacidade += 1; + (*lista)->compras = (Item *) realloc((*lista)->compras, (*lista)->capacidade * sizeof(Item)); + } + Item item; + strcpy(item.nome, nome); + item.quantidade = quantidade; + (*lista)->compras[(*lista)->tamanho++] = item; +} + +void removerItem(ListaDeCompras **lista, char *nome) { + int indice = -1; + for (int i = 0; i < (*lista)->tamanho; i++) { + if (strcmp((*lista)->compras[i].nome, nome) == 0) { + indice = i; + break; + } + } + if (indice != -1) { + for (int i = indice; i < (*lista)->tamanho - 1; i++) { + (*lista)->compras[i] = (*lista)->compras[i+1]; + } + (*lista)->tamanho--; + } +} + +void listarItens(ListaDeCompras **lista) { + printf("\nLista de compras:\n"); + for (int i = 0; i < (*lista)->tamanho; i++) { + printf("%s - %d\n", (*lista)->compras[i].nome, (*lista)->compras[i].quantidade); + } + printf("\n"); +} + +int main() +{ + ListaDeCompras *lista = criarListadeCompras(); + char produto[50]; + int quantidade,opcao,inicio; + + inicio = TRUE; + + while (inicio) + { + printf("\tLista de Compras\n"); + printf("1-Adicionar Produto\n"); + printf("2-Remover Produto\n"); + printf("3-Mostrar Lista De Compras\n\n"); + printf("4-Sair e Excluir Lista de Compras"); + printf("\nDigite uma opcao:"); + scanf("%d",&opcao); + + switch (opcao) + { + case 1: + printf("Nome do Produto: "); + scanf("%s",produto); + printf("Quantidade do produto: "); + scanf("%d",&quantidade); + adicionarItem(&lista,produto,quantidade); + break; + case 2: + printf("Nome do Produto: "); + scanf("%s",produto); + removerItem(&lista,produto); + break; + case 3: + listarItens(&lista); + break; + if (opcao == 4) + { + inicio = FALSE; + break; + } + + } + free(&(*lista)); + + return 0; +} +} \ No newline at end of file diff --git a/Sorting/Stable Sort/Exercises-StableSelectionSort.c b/Sorting/Stable Sort/Exercises-StableSelectionSort.c new file mode 100644 index 0000000..3708747 --- /dev/null +++ b/Sorting/Stable Sort/Exercises-StableSelectionSort.c @@ -0,0 +1,44 @@ +/*Normalmente, a Ordenação por Seleção Direta +(Selection Sort) ela é instavel, ou seja, +os elementos que são iguais em um vetor por exemplo, +nao sao ordenados pela ordem de entrada, porém é possivel tornar o selection sort estavel.*/ + +#include + +int main(void) +{ + int numeros[5]; + int cont1, cont2, menor, aux; + + cont1 = 1; + + while (cont1 <= 5) + { + scanf("%d", &numeros[cont1]); + cont1++; + } + for (cont1 = 0; cont1 < 5 - 1; cont1++) + { + menor = cont1; + for (cont2 = cont1 + 1; cont2 < 5; cont2++) + { + if (numeros[menor] > numeros[cont2]) + menor = cont2; + } + + aux = numeros[menor]; + for (int k = menor; k > cont1; k--) + { + numeros[k] = numeros[k - 1]; + numeros[cont1] = aux; + } + } + + cont1 = 1; + while(cont1 <= 5){ + printf("%4d", numeros[cont1]); + cont1 = cont1 + 1; + } + + return 0; +} \ No newline at end of file diff --git a/Sorting/Unstable Sort/Exercises-ArrayHeapSort.c b/Sorting/Unstable Sort/Exercises-ArrayHeapSort.c new file mode 100644 index 0000000..2daa170 --- /dev/null +++ b/Sorting/Unstable Sort/Exercises-ArrayHeapSort.c @@ -0,0 +1,69 @@ +/*Demonstração do Heap Sort em Vetor +Normalmente, a ordenação é feita com arvore binaria completa*/ + +#include +#define TAM 10 + +void troca(int*,int*); +void heapSort(int[], int n); +void heapify(int[],int,int); + +int main() +{ + int numeros[TAM]; + + printf("Insira %d numeros para o vetor\n",TAM); + for (int i = 0; i < TAM; i++) + { + scanf("%d", &numeros[i]); + } + + heapSort(numeros,TAM); + + printf("Vetor Ordenado:\n"); + for (int i = 0; i < TAM; i++) + { + scanf("%d ", &numeros[i]); + } + printf("\n"); + return 0; +} + +void troca(int *a, int *b) { + int temp = *a; + *a = *b; + *b = temp; +} + +void heapify(int vetor[], int n, int i) { + + int maior = i; + int esquerda = 2 * i + 1; + int direita = 2 * i + 2; + + if (esquerda < n && vetor[esquerda] > vetor[maior]) + maior = esquerda; + + if (direita < n && vetor[direita] > vetor[maior]) + maior = direita; + + + if (maior != i) { + troca(&vetor[i], &vetor[maior]); + heapify(vetor, n, maior); + } +} + +void heapSort(int vetor[], int n) { + + for (int i = n / 2 - 1; i >= 0; i--) + heapify(vetor, n, i); + + // Heap sort + for (int i = n - 1; i >= 0; i--) { + troca(&vetor[0], &vetor[i]); + + + heapify(vetor, i, 0); + } + } diff --git a/Sorting/Unstable Sort/Exercises-QuickSort.c b/Sorting/Unstable Sort/Exercises-QuickSort.c new file mode 100644 index 0000000..b10a9e2 --- /dev/null +++ b/Sorting/Unstable Sort/Exercises-QuickSort.c @@ -0,0 +1,59 @@ +#include +#include +#define TAM 30 + +void quickSort(int [],int,int); + +int main(){ + + int vetor[TAM]; + int iFor; + + printf("Insira os elementos do vetor:\n"); + + for(iFor = 0; iFor < TAM; iFor++){ + scanf("%d",&vetor[iFor]); + } + quickSort(vetor,0,TAM-1); + + printf("Elementos do vetor em ordem ascendente:\n"); + for (iFor = 0; iFor < TAM; iFor++) { + printf("%d ",vetor[iFor]); + } + + return 0; +} + +void quickSort(int arr[], int esquerda, int direita) { + + int i = esquerda, j = direita; + + int temporario; + + int pivo = arr[(esquerda + direita) / 2]; //Para melhores casos de Ordenacao, usa-se o meio do vetor como pivo + + while (i <= j) { + + while (arr[i] < pivo) + i++; + while (arr[j] > pivo) + j--; + if (i <= j) { + + temporario = arr[i]; + + arr[i] = arr[j]; + + arr[j] = temporario; + i++; + j--; + + } + + }; + + if (esquerda < j) + quickSort(arr, esquerda, j); + if (i < direita) + quickSort(arr, i, direita); +} \ No newline at end of file