hercioneto

Exercício 11-09-18

Sep 11th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. //Rextester.Program.Main is the entry point for your code. Don't change it.
  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Rextester
  10. {
  11.     public class Program
  12.     {
  13.         public static void imprimirArray(int[] vetor) {
  14.             for (int r = 0; r < vetor.Length; r++)
  15.             {
  16.                 Console.Write("{0}  ", vetor[r]);
  17.             }
  18.        
  19.         }
  20.        
  21.         public static int[] insertSort(int[] vetor) {
  22.            
  23.             int i = 0;
  24.             int elemento = 0;
  25.            
  26.             for (int j = 0; j < vetor.Length; j++)
  27.             {
  28.                 elemento = vetor[j];
  29.                 i = j - 1;
  30.                 while (i >= 0 && vetor[i] > elemento)
  31.                 {
  32.                     vetor[i + 1] = vetor[i];
  33.                     i += - 1;
  34.                 }//fim while
  35.  
  36.                 vetor[i + 1] = elemento;                                
  37.             }//fim for
  38.             return vetor;
  39.        
  40.         }
  41.        
  42.         public static int[] bubbleSort(int[] vetor) {
  43.         int aux = 0;
  44.         for (int i = 0; i < vetor.Length; i++)
  45.             {
  46.                 for (int j = i + 1; j < vetor.Length; j++)
  47.                 {
  48.                     if (vetor[i] > vetor[j])
  49.                     {
  50.                         aux = vetor[i];
  51.                         vetor[i] = vetor[j];
  52.                         vetor[j] = aux;
  53.                    
  54.                     }
  55.                 }
  56.             }
  57.             return vetor;
  58.         }
  59.        
  60.         public static int[] selectionSort(int[] vetor) {
  61.         int menor, aux = 0;
  62.             for (int i = 0; i < vetor.Length - 1; i++)
  63.             {
  64.                 menor = i;
  65.  
  66.                 for (int j = i + 1; j < vetor.Length; j++)
  67.                 {
  68.                     if (vetor[menor] > vetor[j])
  69.                     {
  70.                         menor = j;
  71.                     }    
  72.                 }
  73.  
  74.                 if (menor != i)
  75.                 {
  76.                     aux = vetor[menor];
  77.                     vetor[menor] = vetor[i];
  78.                     vetor[i] = aux;
  79.                 }
  80.  
  81.  
  82.             }//fim do for
  83.             return vetor;
  84.  
  85.         }
  86.        
  87.         public static void Main(string[] args)
  88.         {
  89.             int[] vetor = new int[5];
  90.             int novo =0;
  91.  
  92.             Random random = new Random();
  93.             for (int b = 0; b < vetor.Length; b++)
  94.             {
  95.                 vetor[b] = random.Next(1, 500);
  96.             }
  97.  
  98.            
  99.             Console.WriteLine(" Insert Sort ");
  100.             imprimirArray(insertSort(vetor));    
  101.             Console.WriteLine("  ");
  102.            
  103.             Console.WriteLine(" Selection Sort ");
  104.             imprimirArray(selectionSort(vetor));
  105.             Console.WriteLine("  ");
  106.            
  107.             Console.WriteLine(" Bubble Sort ");
  108.             imprimirArray(bubbleSort(vetor));
  109.             int[] vetor2 = new int[vetor.Length+1];
  110.            
  111.             Console.WriteLine(" ");
  112.             Console.Write("Digite novo número:");
  113.             novo = Convert.ToInt32(Console.ReadLine());
  114.             Console.WriteLine(" ");
  115.            
  116.             for (int v = 0; v < vetor.Length; v++)
  117.             {
  118.                 vetor2[v] = vetor[v];
  119.             }
  120.             vetor2[vetor.Length] = novo;
  121.            
  122.             Console.WriteLine(" Insert Sort ");
  123.             imprimirArray(insertSort(vetor2));    
  124.             Console.WriteLine("  ");
  125.             Console.WriteLine(" Selection Sort ");
  126.             imprimirArray(selectionSort(vetor2));
  127.             Console.WriteLine(" ");
  128.             Console.WriteLine(" Bubble Sort ");
  129.             imprimirArray(bubbleSort(vetor2));
  130.                        
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment