public static void Main(string[] args)
{
int [] vector={12,5,3,13,2,9,7};
int menor, temp;
for (int i=0; i< vector.Length; i++){
menor=i;
for(int j=i+1; j< vector.Length ;j++){
if(vector[j]<vector[menor])
menor=j;
}
if (i != menor){
temp=vector[i];
vector[i]=vector[menor];
vector [menor]=temp;
}
}
for (int i=0; i<vector.Length; i++)
Console.Write("{0}-", vector[i]);
Console.ReadKey();
}
Por inserción
static void OrdInsercion (int[]v)
{
int temp, j;
for (int i=1: i<v.Length; i++)
{
j=i;
temp=v[i];
while (j<0 && temp<v[j-1])
{
v[j]=v[j-1];
j--;
}
v=[j]=temp;
}
}
Por burbuja
static void OrdBurbuja(int[]v){
bool ordenado = false;
int pasada = 1, aux;
while (pasada < v.Length && !(ordenado)) {
ordenado = true;
for (int j = 0; j < v.Length - pasada; j++) {
if (v [j] > v [j + 1]) {
ordenado = false;
aux = v [j];
v [j] = v [j + 1];
v [j + 1] = aux;
}
}
pasada++;
}
}
Shell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace PruebaVector
{
class PruebaVector
{
private int[] vector={12,5,3,13,2,9,7};
public void Cargar()
{
Console.WriteLine("Metodo de Shell Sort\n");
Console.Write("Teclee la longitud del vector:");
int cant=int.Parse( Console.ReadLine());
vector = new int[cant];
for (int i = 0; i < vector.Length; i++)
{
Console.Write("Ingrese elemento "+(i+1)+": ");
vector[i] = int.Parse(Console.ReadLine());
}
}
public void Shell()
{
int intervalo,k, j, temp;
intervalo = vector.Length / 2;
while (intervalo > 0) {
for (int i=intervalo;i<vector.Length;i++){
j = i - intervalo;
while (j >= 0) {
k = j + intervalo;
if (vector [j] <= vector [k])
j = -1;
else {
temp = vector [j];
vector [j] = vector [k];
vector [k] = temp;
j -= intervalo;
Thread.Sleep(1000);
Imprimir();
}
}
}//fin for
intervalo=intervalo/2;
}//fin while
}//fin sort shell
public void Imprimir()
{
for (int i = 0; i < vector.Length; i++)
{
Console.Write(vector[i]+" ");
}
Console.WriteLine ();
}
static void Main(string[] args)
{
PruebaVector v = new PruebaVector();
// pv.Cargar();
Console.WriteLine("\nVector inicial:");
v.Imprimir();
v.Shell();
Console.WriteLine("\nVector ordenados en forma ascendente:");
v.Imprimir();
Console.ReadKey ();
}
}
}
0 comentarios:
Publicar un comentario