Intercalación Simple

Es un ejemplo del algoritmo y el código en c#, de la ordenación Externa de Estructuras de Datos mediante la intercalación directa.



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

namespace ConsoleApplication1
{
class Intercalacion_Simple
{
public int[] A;
public int[] B;
int[] C;
int[] TempA;
int[] TempB;
int N = 10;
int M = 10;
int i, j, x, w;
int I, J, K, X;

public Intercalacion_Simple()
{
A = new int[N];
B = new int[M];
C = new int[A.Length+B.Length];
TempA = new int[A.Length];
TempB = new int[B.Length];
}
public void Generar()
{
Random Aleatorio = new Random();
for (int i = 0; i < N; i++)
{
A[i] = Aleatorio.Next(10, 99);
TempA[i] = A[i];
}
for (int i = 0; i < M; i++)
{
B[i] = Aleatorio.Next(10, 99);
TempB[i] = B[i];
}
}
public void quicksortA(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)
quicksortA(L, j);
if (i < R)
quicksortA(i, R);
}
public void quicksortB(int L, int R)
{
i = L;
j = R;
x = B[(L + R) / 2];
do
{
while (B[i] < x)
i = i + 1;
while (x < B[j])
j = j - 1;
if (i <= j)
{
w = B[i];
B[i] = B[j];
B[j] = w;
i = i + 1;
j = j - 1;
}
} while (i < j);
if (L < j)
quicksortB(L, j);
if (i < R)
quicksortB(i, R);
}
public void Intercalar()
{

I = 0;
J = 0;
K = 0;
X = 0;

while (I < N && J < M)
{
if (A[I] <= B[J])
{
C[K] = A[I];
I = I + 1;
K = K + 1;
}
else
{
C[K] = B[J];
J = J + 1;
K = K + 1;
}
}
if (I > N)
{
for (X = J; X < M; X++)
{
C[K] = B[X];
K = K + 1;
}
}
else
{
for (X = I; X < N; X++)
{
C[K] = A[X];
K = K + 1;
}
}
}
public void DesplegarDesA()
{
int Cont = 1;
Console.Write("Vector A\n\n");
for (int i = 0; i < TempA.Length; i++)
{
Console.Write("{0}: {1}\t\t", i + 1, TempA[i]);
if (Cont == 5)
{
Console.Write("\n\n");
Cont = 0;
}
Cont++;
}
}
public void DesplegarDesB()
{
int Cont = 1;
Console.Write("\nVector B\n\n");
for (int i = 0; i < TempB.Length; i++)
{
Console.Write("{0}: {1}\t\t", i + 1, TempB[i]);
if (Cont == 5)
{
Console.Write("\n\n");
Cont = 0;
}
Cont++;
}
}
public void DesplegarOrdA()
{
int Cont = 1;
Console.Write("Vector A\n\n");
for (int i = 0; i < A.Length; i++)
{
Console.Write("{0}: {1}\t\t", i + 1, A[i]);
if (Cont == 5)
{
Console.Write("\n\n");
Cont = 0;
}
Cont++;
}
}
public void DesplegarOrdB()
{
int Cont = 1;
Console.Write("\nVector B\n\n");
for (int i = 0; i < B.Length; i++)
{
Console.Write("{0}: {1}\t\t", i + 1, B[i]);
if (Cont == 5)
{
Console.Write("\n\n");
Cont = 0;
}
Cont++;
}
}
public void DesplegarIntercalado()
{
int Cont = 1;
for (int i = 0; i < C.Length; i++)
{
Console.Write("{0}: {1}\t\t", i + 1, C[i]);
if (Cont == 5)
{
Console.Write("\n\n");
Cont = 0;
}
Cont++;
}
}
}
class Program
{
static void Main(string[] args)
{
int Opcion;
Intercalacion_Simple I = new Intercalacion_Simple();
do
{
Console.Clear();
Console.WriteLine("Menú");
Console.WriteLine("1.- Generar numeros aleatorios");
Console.WriteLine("2.- Ordenamiento de vectores");
Console.WriteLine("3.- Intercalar");
Console.WriteLine("4.- Salir");
Console.Write("\nQue opcion desea realizar: ");
Opcion = Int32.Parse(Console.ReadLine());
Console.Clear();
switch (Opcion)
{

case 1:
I.Generar();
Console.WriteLine("\nValores generados\n");
I.DesplegarDesA();
I.DesplegarDesB();
break;
case 2:
Console.WriteLine("\nDespliegue Ordenado\n");
I.quicksortA(0, I.A.Length-1);
I.quicksortB(0, I.B.Length-1);
I.DesplegarOrdA();
I.DesplegarOrdB();
break;
case 3:
Console.WriteLine("\nDespliegue Intercalado\n");

I.Intercalar();
I.DesplegarIntercalado();
break;
case 4:
break;
default:
Console.WriteLine("\nOpción Incorrecta");
break;
}
Console.ReadLine();
} while (Opcion != 4);

}
}
}

Post Relacionados: