Sunteți pe pagina 1din 2

/***************** MÉTODO DE ORDENAMIENTO SHELL SORT *******************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace AppOrdenamientoShell
{
class Program
{
/*************** INGRESAR UN ELEMENTO A AUN ARREGLO *************/
static int[] ArregloEnteros(int[] Arreglo, int longitud)
{
Random aleatorio = new Random();
for (int i = 0; i < longitud; i++)
{
Arreglo[i] = aleatorio.Next(0, longitud) + 1;
}
return Arreglo;
}

/**************** ORDENAMIENTO SHELL ****************************/


static void OrdenamientoShell(int[] Arreglo, int longitud)
{
int salto = 0;
int sw = 0;
int auxi = 0;
int e = 0;
salto = longitud / 2;
while (salto > 0)
{
sw = 1;
while (sw != 0)
{
sw = 0;
e = 1;
while (e <= (longitud - salto))
{
if (Arreglo[e - 1] > Arreglo[(e - 1) + salto])
{
auxi = Arreglo[(e - 1) + salto];
Arreglo[(e - 1) + salto] = Arreglo[e - 1];
Arreglo[(e - 1)] = auxi;
sw = 1;
}
e++;
}
}
salto = salto / 2;
}
}

/************************ MOSTRAR EL ARREGLO ORDENADO *******************/


static void Mostrar(int[] Arreglo, int longitud)
{
Console.WriteLine("********* ELEMENTOS DEL ARREGLO ORDENADO
**********");
for (int i = 0; i < longitud; i++)
{
Console.Write("{0} ", Arreglo[i]);
}
}

/************************** PROGRAMA PRINCIPAL **************************/


static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();

Console.Write("Ingrese la longitud del vector: ");


int longitud = int.Parse(Console.ReadLine());
int[] Vector = new int[longitud];
ArregloEnteros(Vector, longitud);
OrdenamientoShell(Vector, longitud);
Mostrar(Vector, longitud);

sw.Stop();
Console.WriteLine("\n\n\n\n Time elapsed: {0}",
sw.Elapsed.ToString("hh\\:mm\\:ss\\.fff"));
Console.ReadKey();
}
}
}

S-ar putea să vă placă și