DEV Community

Panchi
Panchi

Posted on

ORDENAMIENTO POR SELECCION CON C#

El algoritmo de ordenamiento por selección requiere de operaciones para ordenar una lista de x números. Siguiendo lo siguientes parámetros:

  1. Buscas el elemento más pequeño de la lista.
  2. Lo intercambias con el elemento ubicado en la primera posición de la lista.
  3. Buscas el segundo elemento más pequeño de la lista.
  4. Lo intercambias con el elemento que ocupa la segunda posición en la lista.
  5. Repites este proceso hasta que hayas ordenado toda la lista. NOTA: Al final de los pasos encontraras el código completo. 😉

PASO 1. Creación de lista a ordenar
// Definir lista con valores a ordenar.
int [] selection = new int[]{ 2, 4, 23, 8, 5, 9, 4, 6, 1 };

PASO 2. Definición de variables para almacenar procesos
// DEFINICION DE VARIABLES QUE SE USARAN PARA ALMACENAR PROCESOS
int aux, minimo;

PASO 3. Imprimir lista
// LISTA ORIGINAL
Console.WriteLine("LISTA ORIGINAL");
for (int i = 0; i < selection.Length; i++)
{
Console.Write(selection[i]+" ");
}

PASO 4. Proceso para ordenamiento por selección

for (int i = 0; i < selection.Length; i++)
{
    min = i;  //  min se iguala a la posición de i.

    //  BUSCA EL VALOR MAS PEQUEÑO
    for (int j = i+1; j < selection.Length; j++)  //  i+1 -> SUMA MAS UNO A LA POSICION DEL VECTOR
    {
        //  VALIDAR EL VALOR MENOR
        if (selection[j] < selection[min]) //  VALIDA LOS VALORES EN LAS POSICIONES DEL VECTOR
        {
            min = j;        //  min SE IGUALA A LA POSICION DE j
        }
    }
    //  INTERCAMBIANDO VALORES
    aux = selection[i];
    selection[i] = selection[min];
    selection[min] = aux;

}
Enter fullscreen mode Exit fullscreen mode

PASO 5. Impresión de datos ordenados

//  IMPRESION DE DATOS ORDENADOS
Console.WriteLine("\nDATOS ORDENADOS");
for (int i = 0; i < selection.Length; i++)
{
    Console.Write(selection[i]+" ");
}
Enter fullscreen mode Exit fullscreen mode

EJECUCION:

Image description


Código

using System.Diagnostics;

Stopwatch timeMeasure = new Stopwatch();

int[] secuencial = new int[] { 2, 4, 23, 8, 5, 9, 4, 6, 1 };

int aux, min;

Console.WriteLine("LISTA ORIGINAL");
for (int i = 0; i < secuencial.Length; i++)
{
    Console.Write(secuencial[i]+" ");
}

timeMeasure.Start();
for (int i = 0; i < secuencial.Length; i++)
{
    min = i;

    //  INTERCAMBIAR VALORES MEDIANTE POSICIONES
    for (int j = i+1; j < secuencial.Length; j++)
    {
        //  VALIDAR DATOS MENORES

        if (secuencial[j] < secuencial[min])
        {
            min = j;
        }
    }
    //  INTERCAMBIANDO VALORES
    aux = secuencial[i];
    secuencial[i] = secuencial[min];
    secuencial[min] = aux;

}


//  IMPRESION DE DATOS ORDENADOS
Console.WriteLine("\nDATOS ORDENADOS");
for (int i = 0; i < secuencial.Length; i++)
{
    Console.Write(secuencial[i]+" ");
}

timeMeasure.Stop();
Console.WriteLine($"\nTiempo de ejecucion: {timeMeasure.Elapsed.TotalMilliseconds} ms");
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay