Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rextester.Program.Main is the entry point for your code. Don't change it.
- //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace Rextester
- {
- public class Program
- {
- public static void imprimirArray(int[] vetor) {
- for (int r = 0; r < vetor.Length; r++)
- {
- Console.Write("{0} ", vetor[r]);
- }
- }
- public static int[] insertSort(int[] vetor) {
- int i = 0;
- int elemento = 0;
- for (int j = 0; j < vetor.Length; j++)
- {
- elemento = vetor[j];
- i = j - 1;
- while (i >= 0 && vetor[i] > elemento)
- {
- vetor[i + 1] = vetor[i];
- i += - 1;
- }//fim while
- vetor[i + 1] = elemento;
- }//fim for
- return vetor;
- }
- public static int[] bubbleSort(int[] vetor) {
- int aux = 0;
- for (int i = 0; i < vetor.Length; i++)
- {
- for (int j = i + 1; j < vetor.Length; j++)
- {
- if (vetor[i] > vetor[j])
- {
- aux = vetor[i];
- vetor[i] = vetor[j];
- vetor[j] = aux;
- }
- }
- }
- return vetor;
- }
- public static int[] selectionSort(int[] vetor) {
- int menor, aux = 0;
- for (int i = 0; i < vetor.Length - 1; i++)
- {
- menor = i;
- for (int j = i + 1; j < vetor.Length; j++)
- {
- if (vetor[menor] > vetor[j])
- {
- menor = j;
- }
- }
- if (menor != i)
- {
- aux = vetor[menor];
- vetor[menor] = vetor[i];
- vetor[i] = aux;
- }
- }//fim do for
- return vetor;
- }
- public static void Main(string[] args)
- {
- int[] vetor = new int[5];
- int novo =0;
- Random random = new Random();
- for (int b = 0; b < vetor.Length; b++)
- {
- vetor[b] = random.Next(1, 500);
- }
- Console.WriteLine(" Insert Sort ");
- imprimirArray(insertSort(vetor));
- Console.WriteLine(" ");
- Console.WriteLine(" Selection Sort ");
- imprimirArray(selectionSort(vetor));
- Console.WriteLine(" ");
- Console.WriteLine(" Bubble Sort ");
- imprimirArray(bubbleSort(vetor));
- int[] vetor2 = new int[vetor.Length+1];
- Console.WriteLine(" ");
- Console.Write("Digite novo número:");
- novo = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine(" ");
- for (int v = 0; v < vetor.Length; v++)
- {
- vetor2[v] = vetor[v];
- }
- vetor2[vetor.Length] = novo;
- Console.WriteLine(" Insert Sort ");
- imprimirArray(insertSort(vetor2));
- Console.WriteLine(" ");
- Console.WriteLine(" Selection Sort ");
- imprimirArray(selectionSort(vetor2));
- Console.WriteLine(" ");
- Console.WriteLine(" Bubble Sort ");
- imprimirArray(bubbleSort(vetor2));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment