Upload
marcelocosta
View
217
Download
0
Embed Size (px)
Citation preview
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
1/10
Classificao e Pesquisa
Atividade Menu Ordenao
Insero, Seleo e Bubble Sort
Nome: Marcelo Aparecido Costa
RA: 2013101220
Perodo: Quarto
Curso: Cincia da Computao
1Cdigo Fonte
1.1Program.cs2
//Classificao e Pesquisa3
//Ttulo: Ordenao Menu4 //Assunto: Ordenao de vetores
5 //Aluno: Marcelo Aparecido Costa6 //RA: 20131012207 //Data: 30/08/2014
8 usingSystem;9 usingSystem.Collections.Generic;10 usingSystem.Linq;
11 usingSystem.Text;1213 namespaceMenu
14 {
15
classProgram16 {
17 staticvoidMain(string[] args){18
intmenu = 1;
19 int[] vet, ordem;2021
while(menu != 0)
22 {23
//Limpar tela do console24 Console.Clear();
25 //Menu26 Console.WriteLine("Ordenao");27 Console.WriteLine("\n1: Seleo\n2: Insero\n3: Bubble
Sort\n\n0: Sair");28 //Receber Valor29 menu = int.Parse(Console.ReadLine());
30
switch(menu)31 {32 case1:
33 Console.WriteLine("\n\nVetor Aleatorio");34 Aleatorioaleselecao = newAleatorio();35 //Metodo staticAleatorio.vetorAleatorio(10);
36 vet = aleselecao.vetorAleatorio(10);37 foreach(intvalor invet)38
{
39 Console.WriteLine(valor);40 }
41
Console.WriteLine("\n\nVetor Ordenado");42 Ordenaordselecao = newOrdena();
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
2/10
43 ordem = ordselecao.vetOrdena(vet);
44 foreach(intordenar inordem)45 Console.WriteLine(ordenar);46
Console.ReadKey();
47 break;4849
case2:
50
Console.WriteLine("\n\nVetor Aleatorio");51 Aleatorioaleinsercao = newAleatorio();52 //Metodo staticAleatorio.vetorAleatorio(10);
53 vet = aleinsercao.vetorAleatorio(10);54
foreach(intvalor invet)55 {
56 Console.WriteLine(valor);57 }58 Console.WriteLine("\n\nVetor Ordenado");
59
Insercaoordinsercao = newInsercao();60 ordem = ordinsercao.vetInsercao(vet);61 Console.WriteLine("\n\nOrdem:\n\n");
62 foreach(intordenar inordem)
63
Console.WriteLine(ordenar);64 Console.ReadKey();65 break;66
67 case3:68 Console.WriteLine("\n\nVetor Aleatorio");69 AleatorioaleBubbleSort = newAleatorio();
70 //Metodo staticAleatorio.vetorAleatorio(10);71 vet = aleBubbleSort.vetorAleatorio(10);72 foreach(intvalor invet)
73 {74 Console.WriteLine(valor);75
}
76
Console.WriteLine("\n\nVetor Ordenado");77
Bubble_SortordBubbleSort = newBubble_Sort();78 ordem = ordBubbleSort.vetBubbleSort(vet);
79 Console.WriteLine("\n\nOrdem:\n\n");80
foreach(intordenar inordem)81 Console.WriteLine(ordenar);
82 Console.ReadKey();83 break;84 }
85 }86
87
}88 }89 }
1.2 Ordena.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;
usingSystem.Text;
namespaceMenu
{classOrdena{
publicint[] vetOrdena(int[] vetor){
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
3/10
intmin, aux, tamVetorDesordenado;
tamVetorDesordenado = vetor.Length;for(inti = 0; i < tamVetorDesordenado - 1; i++){
min = i;for(intj = (i + 1); j < tamVetorDesordenado; j++){
if(vetor[j] < vetor[min])min = j;
}
aux = vetor[min];vetor[min] = vetor[i];vetor[i] = aux;
}returnvetor;
}
}}
1.3 Insercao.cs
usingSystem;
usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;
namespaceMenu{
classInsercao{
publicint[] vetInsercao(int[] vetD)
{inti, j, escolha, tamanhodovetordesordenado;tamanhodovetordesordenado = vetD.Length;
for(i = 1; i < (tamanhodovetordesordenado); i++){
escolha = vetD[i];
j = i - 1;Console.WriteLine("\n\nVarivel Comparativa: "+ escolha);while((j >= 0) && (escolha < vetD[j]))
{Console.WriteLine("Enquanto "+ j + " >= "+ 0 + " e "+ "
Varivel Comparataiva = "+ escolha + " < "+ vetD[j]);Console.WriteLine(vetD[j + 1] + " ser trocado por "+
vetD[j]);
vetD[j + 1] = vetD[j];j = j - 1;
}if(j != (i - 1)){
Console.WriteLine("Se "+ j + " diferente "+ (i - 1));
Console.WriteLine(vetD[j + 1] + " ser trocado pela varivelcomaparativa "+ escolha);
vetD[j + 1] = escolha;
}}returnvetD;
}}
}
1.4 Bubble_Sort.cs
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
4/10
usingSystem;
usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;
namespaceMenu{
classBubble_Sort{
publicint[] vetBubbleSort(int[] vetBS)
{inti, j, escolha, tamanhoDoVetorDesordenado;tamanhoDoVetorDesordenado = vetBS.Length;
for(i = (tamanhoDoVetorDesordenado - 1); i >= 1; i--){
if(i == (tamanhoDoVetorDesordenado - 1))
{for(j = 0; j < i; j++){
Console.WriteLine("Vetor "+ vetBS[j] + " > "+ vetBS[j +
1] + "?");if(vetBS[j] > vetBS[j + 1]){
Console.WriteLine("Sim, realizando a troca!");
escolha = vetBS[j];vetBS[j] = vetBS[j + 1];vetBS[j + 1] = escolha;
}else
Console.WriteLine("No!");
}}else
{for(j = 0; j vetBS[j + 1])
{Console.WriteLine("Sim, realizando a troca!");escolha = vetBS[j];
vetBS[j] = vetBS[j + 1];vetBS[j + 1] = escolha;
}else
Console.WriteLine("No!");
}}
}
returnvetBS;}
}
}
1.5 Aleatorio.cs
usingSystem;usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceMenu
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
5/10
{
classAleatorio{
publicint[] vetorAleatorio(intn)
{int[] vet = newint[n];Randomrdm = newRandom();
for(inti = 0; i < n; i++)vet[i] = rdm.Next(0, 9999);
returnvet;
}}
}
2 Ordenao
2.1 Seleo 1
2.2 Insero 2
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
6/10
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
7/10
2.3 Bubble Sort 3
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
8/10
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
9/10
8/11/2019 Classificao e Pesquisa - Menu Classe - Marcelo Aparecido Costa
10/10
2.4 Sair - 0