Método de inserción directa
jrdzApuntes12 de Octubre de 2015
4.947 Palabras (20 Páginas)380 Visitas
Método de inserción directa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PruebaVector
{
class PruebaVector
{
private int[] vector;
public void Cargar()
{
Console.WriteLine("Metodo de insercion directa");
Console.Write("Cuantos longitud del vector:");
string linea;
linea = Console.ReadLine();
int cant;
cant = int.Parse(linea);
vector = new int[cant];
for (int f = 0; f < vector.Length; f++)
{
Console.Write("Ingrese elemento " + (f + 1) + ": ");
linea = Console.ReadLine();
vector[f] = int.Parse(linea);
}
}
public void InsercionDirecta()
{
int auxili;
int j;
for (int i = 0; i < vector.Length; i++)
{
auxili = vector[i];
j = i - 1;
while (j >= 0 && vector[j] > auxili)
{
vector[j + 1] = vector[j];
j--;
}
vector[j + 1] = auxili;
}
}
public void Imprimir()
{
Console.WriteLine("Vector ordenados en forma ascendente");
for (int f = 0; f < vector.Length; f++)
{
Console.Write(vector[f] + " ");
}
Console.ReadKey();
}
static void Main(string[] args)
{
PruebaVector pv = new PruebaVector();
pv.Cargar();
pv.InsercionDirecta();
pv.Imprimir();
}
}
}
Shell Sort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PruebaVector
{
class PruebaVector
{
private int[] vector;
public void Cargar()
{
Console.WriteLine("Metodo de Shell Sort");
Console.Write("Cuantos longitud del vector:");
string linea;
linea = Console.ReadLine();
int cant;
cant = int.Parse(linea);
vector = new int[cant];
for (int f = 0; f < vector.Length; f++)
{
Console.Write("Ingrese elemento " + (f + 1) + ": ");
linea = Console.ReadLine();
vector[f] = int.Parse(linea);
}
}
public void Shell()
{
int salto = 0;
int sw = 0;
int auxi = 0;
int e = 0;
salto = vector.Length / 2;
while (salto > 0)
{
sw = 1;
while (sw != 0)
{
sw = 0;
e = 1;
while (e <= (vector.Length - salto))
{
if (vector[e - 1] > vector[(e - 1) + salto])
{
auxi = vector[(e - 1) + salto];
vector[(e - 1) + salto] = vector[e - 1];
vector[(e - 1)] = auxi;
sw = 1;
}
e++;
}
}
salto = salto / 2;
}
}
public void Imprimir()
{
Console.WriteLine("Vector ordenados en forma ascendente");
for (int f = 0; f < vector.Length; f++)
{
Console.Write(vector[f] + " ");
}
Console.ReadKey();
}
static void Main(string[] args)
{
PruebaVector pv = new PruebaVector();
pv.Cargar();
pv.Shell();
pv.Imprimir();
}
}
}
Quick Sort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace quicksort
{
class Class
{
static void Main()
{
int n;
Console.WriteLine("Metodo de Quick Sort");
Console.Write("Cuantos longitud del vector: ");
n = Int32.Parse(Console.ReadLine());
llenar b = new llenar(n);
}
}
class llenar
...