1

# Bubble Sort y Selection Sort en C#

Aquí les comparto los dos algoritmos del curso, pero en c#:

``````static void imprimirArray(int[] myArray)
{
string mensaje = "";for (int k = 0; k < myArray.Length; k++)
mensaje += myArray[k] + ", ";

Console.WriteLine(mensaje+"\n");
}
static void bubbleSort(int[] myArray)
{
int aux;int ciclos=0;
bool hayComparacion;for (int i = 0; i < myArray.Length; i++)
{
imprimirArray(myArray);
hayComparacion = false;for (int j = 0; j < myArray.Length - 1 - i; j++)
{
ciclos++;if (myArray[j] > myArray[j + 1])
{
aux = myArray[j];
myArray[j] = myArray[j + 1];
myArray[j + 1] = aux;
hayComparacion |= true;
}
}
if (!hayComparacion)
break;
}
Console.WriteLine("\n" + ciclos + "\n");
}

static void SelectionSort(int[] myArray)
{
int aux;int indexAux;int ciclos = 0;for (int i = 0; i < myArray.Length; i++)
{
imprimirArray(myArray);
indexAux = i;for (int j = i+1; j < myArray.Length; j++)
{
ciclos++;if (myArray[indexAux] > myArray[j])
{
indexAux = j;
}
}
aux = myArray[i];
myArray[i] = myArray[indexAux];
myArray[indexAux] = aux;
}
Console.WriteLine("\n"+ ciclos+"\n");
}

int[] myArray = { 1, 5, 4, 3, 2, 6, 8, 3, 5, 9, 13, 10};int[] myArray2 = { 1, 5, 4, 3, 2, 6, 8, 3, 5, 9, 13, 10};
DateTime date1 = DateTime.Now;
Console.WriteLine("Bubble Sort");
Console.WriteLine(DateTime.Now - date1);
bubbleSort(myArray);

Console.WriteLine(DateTime.Now - date1);
date1 = DateTime.Now;
Console.WriteLine("Extrange Sort");
SelectionSort(myArray2);
Console.WriteLine(DateTime.Now - date1);`````````
Escribe tu comentario
+ 2