Skip to content
This repository was archived by the owner on Jun 14, 2021. It is now read-only.

Commit c2023cb

Browse files
committed
2020.2
1 parent 80b16a1 commit c2023cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+11810
-0
lines changed

Atv-01/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all:
2+
gcc -Wall -Werror -std=c99 main.c -o SortingAlgorithms
3+
run:
4+
./Sortingalgorithms

Atv-01/SortingAlgorithms

16.6 KB
Binary file not shown.

Atv-01/main.c

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/** AUTORES:
2+
* Felipe Moura Madureira - 10748646
3+
* Lucas Viana Vilela - 10748409
4+
* Lucas Henrique Sant'Anna - 10748521
5+
*/
6+
7+
#include <stdio.h>
8+
#include <string.h>
9+
10+
11+
/* Copia oS elemntos do vetor InputVector para o outputVector */
12+
void copyVector(int vectorSize, int inputVector[], int outputVector[]){
13+
for(int i = 0; i < vectorSize; i++){
14+
outputVector[i] = inputVector[i];
15+
}
16+
17+
return;
18+
}
19+
20+
/* Realiza a troca do número em vector[index1] com o do vector[index2] */
21+
void swapElements(int vector[], int index1, int index2){
22+
23+
/** Usar ou não usar 'if(index1 != index2){}'?
24+
* PRO: Utilizar tornaria essa uma versão mais otimizada;
25+
* PRO: O arquivo fornecido para teste indicou que deve ser usado;
26+
* CON: A versão mostrada em aula não utilizava;
27+
*
28+
* Como a saída deve corresponder com o esperado( e também como o arquivo de
29+
* teste de caso fornecido), então provavelmente é melhor utilizar.
30+
*/
31+
32+
if(index1 != index2){
33+
int tmp; /* variável auxiliar */
34+
35+
printf("T %d %d\n", index1, index2); /* T = Troca */
36+
37+
tmp = vector[index1];
38+
vector[index1] = vector[index2];
39+
vector[index2] = tmp;
40+
}
41+
42+
43+
return;
44+
}
45+
46+
void bubbleSort(int vectorSize, int vector[], int output[]){
47+
int hasSwapped; /* Ocorreu uma troca na passagem anterior? 1 = sim / 0 = não */
48+
int lastSwapped; /* O último elementro trocado nessa passagem' index */
49+
int sentinella = vectorSize; /* O índice do último elemento desordenado */
50+
51+
copyVector(vectorSize, vector, output);
52+
53+
do{
54+
hasSwapped = 0; /* Uma nova passagem se inicio quando não há trocas */
55+
56+
for(int i = 1; i < sentinella; i++){ /* Loops através do vetor */
57+
printf("C %d %d\n", i-1, i); /* C = Comparação */
58+
59+
if(output[i-1] > output[i]){ /* Se a posição subsequente tiver um elemento de menor número em comparação a atual, então os elementos trocarão de posição */
60+
swapElements(output,i-1,i);
61+
62+
hasSwapped = 1; /* A troca aconteceu */
63+
lastSwapped = i;
64+
}
65+
}
66+
67+
sentinella = lastSwapped; /* Sentinella é colocado no último elemento que foi trocado na passagem */
68+
}while(hasSwapped == 1); /* O vetor é considerado ordenado quando uma passagem apresenta nenhuma troca*/
69+
70+
return;
71+
}
72+
73+
74+
void selectionSort(int vectorSize, int vector[], int output[]){
75+
int unsortedSubvector; /* Índice do primeiro elemento do vetor não ordenado */
76+
int lowestValueIndex; /* Índice do elemento de menor valor do vetor não ordenado */
77+
78+
copyVector(vectorSize, vector, output);
79+
80+
/* Loop por todo o vetor (cada final do loop incrementa unsortedSubvector em 1) */
81+
for(unsortedSubvector = 0; unsortedSubvector < vectorSize; unsortedSubvector++){
82+
/* No início do loop o elemento atual é usado como referência para o menor valor */
83+
lowestValueIndex = unsortedSubvector;
84+
85+
for(int i = unsortedSubvector+1; i < vectorSize; i++){ /* Loop pelo vetor não ordenado */
86+
printf("C %d %d\n", lowestValueIndex, i); /* C = Comparação */
87+
88+
/* Se o elemento de referência é maior que o elemento atual, então lowestValueIndex recebe o índice do elemento de menor valor */
89+
if(output[i] < output[lowestValueIndex]){
90+
lowestValueIndex = i;
91+
}
92+
}
93+
94+
swapElements(output, unsortedSubvector, lowestValueIndex);
95+
}
96+
}
97+
98+
int main(){
99+
char method[7]; /* "bolha" or "selecao" */
100+
int vectorSize; /* Tamanho do vetor */
101+
102+
scanf("%s",method); /* Lê o método de ordenação do usuário */
103+
scanf("%d",&vectorSize); /* Lê o número de valores a ser digitado pelo usuário */
104+
105+
int input[vectorSize]; /* Vetor não ordenado de números */
106+
int output[vectorSize]; /* Vetor ordenado de números */
107+
108+
/* Lê os valores do usuário */
109+
for(int i = 0; i < vectorSize; i++){
110+
scanf("%d", &input[i]);
111+
}
112+
113+
/* Se method == "bolha", o método de ordenação utilizado é o bubbleSort */
114+
if(strcmp(method,"bolha") == 0){
115+
bubbleSort(vectorSize,input,output);
116+
}
117+
118+
/* Se method == "selecao", o método de ordenação utilizado é o selectionSort */
119+
else if(strcmp(method,"selecao") == 0){
120+
selectionSort(vectorSize,input,output);
121+
}
122+
123+
/* Se o usuário não digitou nenhum dos métodos acima relatados, o programa é fechado com erro */
124+
else{
125+
return 1;
126+
}
127+
128+
/* Imprime o vetor com os valores ordenados (output) */
129+
for(int i = 0; i < vectorSize; i++){
130+
printf("%d ", output[i]);
131+
}
132+
printf("\n");
133+
134+
return 0;
135+
}

Atv-01/saudavel

16.7 KB
Binary file not shown.

Atv-01/saudavel.c

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
void preencheVetor(int n, int *vetor){
6+
int i;
7+
for(i=0;i<n;i++){
8+
scanf("%d",&vetor[i]);
9+
}
10+
}
11+
12+
void imprimeVetor(int n, int *vetor){
13+
int i;
14+
for(i=0;i<n;i++){
15+
printf("%d",vetor[i]);
16+
if(i!=n-1){
17+
printf(" ");
18+
}
19+
else {printf("\n");}
20+
}
21+
}
22+
23+
void bolha(int tamanhoVetor, int *vetor){
24+
int restam,i,aux;
25+
for(restam = tamanhoVetor-1; restam>0; restam--){
26+
for(i=0;i<restam;i++){
27+
printf("C %d %d\n",i,i+1);
28+
if(vetor[i]>vetor[i+1]){
29+
printf("T %d %d\n",i,i+1);
30+
aux=vetor[i];
31+
vetor[i]=vetor[i+1];
32+
vetor[i+1]=aux;
33+
}
34+
}
35+
}
36+
}
37+
38+
void selecao(int tamanhoVetor, int *vetor){
39+
int i,j,candidato,posicaoCandidato,aux;
40+
for(i=0;i<(tamanhoVetor-1);i++){
41+
candidato=vetor[i];
42+
posicaoCandidato=i;
43+
for(j=i+1;j<tamanhoVetor;j++){
44+
printf("C %d %d\n",posicaoCandidato,j);
45+
if(vetor[j]<candidato){
46+
candidato=vetor[j];
47+
posicaoCandidato=j;
48+
}
49+
}
50+
if(posicaoCandidato==i){
51+
continue;
52+
}
53+
printf("T %d %d\n",i,posicaoCandidato);
54+
aux = vetor[i];
55+
vetor[i] = candidato;
56+
vetor[posicaoCandidato] = aux;
57+
}
58+
}
59+
60+
int main(){
61+
char tipo[10], moldeBolha[10]="bolha", moldeSelecao[10]="selecao";
62+
int tamanhoVetor, *vetor;
63+
64+
//strcpy(moldeBolha,"bolha");
65+
//strcpy(moldeSelecao,"selecao");
66+
scanf("%s",tipo);
67+
scanf("%d",&tamanhoVetor);
68+
vetor=(int*)malloc(tamanhoVetor*sizeof(int));
69+
preencheVetor(tamanhoVetor,vetor);
70+
if(strcmp(moldeBolha,tipo)==0){
71+
bolha(tamanhoVetor,vetor);
72+
}
73+
else if(strcmp(moldeSelecao,tipo)==0){
74+
selecao(tamanhoVetor,vetor);
75+
}
76+
else{
77+
return 0;
78+
}
79+
imprimeVetor(tamanhoVetor,vetor);
80+
free(vetor);
81+
return 0;
82+
}

Atv-02/1.in

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
3
2+
4 8 16
3+
3 6 5 2
4+
4 8 2 1 9 0 2 3
5+
1 3 2 7 5 5 2 7 2 9 3 0 8 3 1 4

Atv-02/2.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2
2+
4 8
3+
60 77 26 184
4+
58 94 96 32 49 180 11 21

Atv-02/InsertionMerge

16.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)