Algoritmo Quicksort, ordenación interna

Algoritmo y código de la búsqueda interna, en base al intercambio, usados para Estructuras de Datos.

El ordenamiento rápido (quicksort en inglés) es un algoritmo basado en la técnica de divide y vencerás, que permite, en promedio, ordenar n elementos en un tiempo proporcional a n log n.

Ejemplo

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Quick_Sort
{
int i, j, X, W;
int N = 25;
public int[] A;

public Quick_Sort()
{
A = new int[N];
}

public void Generar()
{
Random aleatorio = new Random();
for (int c = 0; c < 25; c++)
A[c] = aleatorio.Next(50);
}
public void quick(int L, int R)
{

i = L;
j = R;

X = A[(L + R) / 2];
do
{
while (A[i] < X)
i = i + 1;
while (X < A[j])
j = j - 1;
if (i <= j)
{
W = A[i];
A[i] = A[j];
A[j] = W;
i = i + 1;
j = j - 1;
}
} while (i < j);

if (L < j)
quick(L, j);
if (i < R)
quick(i, R);
}

public void Despliegue()
{
int contador = 1;
for (int j = 0; j < 25; j++)
{
Console.Write("{0}: {1}\t", j + 1, A[j]);
if (contador == 5)
{
Console.WriteLine("\n");
contador = 0;
}
contador++;
}
}
class Program
{
static void Main(string[] args)
{

Quick_Sort Q = new Quick_Sort();
int opcion = 0;

do
{
Console.Clear();
Console.WriteLine("MENU");
Console.WriteLine("1.- Generar numero aleatorios:");
Console.WriteLine("2.- Ordenar");
Console.WriteLine("3.- Desplegar");
Console.WriteLine("4.- Salir");
Console.WriteLine("\n Que opcion deseas realizar?");
opcion = int.Parse(Console.ReadLine());

switch (opcion)
{
case 1:
Console.WriteLine("Lista generada:\n");
Q.Generar();

Q.Despliegue();
Console.ReadLine();
break;
case 2:
Console.WriteLine("Se ordeno la lista");
Console.ReadLine();
Q.quick(0, Q.N - 1);
break;
case 3:
Console.WriteLine("\nLista ordenada:");
Q.Despliegue();
Console.ReadLine();
break;
case 4:
break;
default:
Console.WriteLine("incorrecto");
Console.ReadLine();
break;

}
} while (opcion != 4);
}
}
}
}

Post Relacionados: