Mostrando entradas con la etiqueta ejercicios de métodos de ordenamiento en c#. Mostrar todas las entradas
Mostrando entradas con la etiqueta ejercicios de métodos de ordenamiento en c#. Mostrar todas las entradas

Ordenar Vectores en C#

El ordenamiento de un vector se logra intercambiando las componentes de manera que:

vec[0] <= vec[1] <= vec[2] ..<=vec[n]

El contenido de la componente vec[0] sea menor o igual al contenido de la componente vec[1] y así sucesivamente.

Si se cumple lo dicho anteriormente decimos que el vector está ordenado de menor a mayor. Igualmente podemos ordenar un vector de mayor a menor.
Se puede ordenar tanto vectores con componentes de tipo int, float como string. En este último caso el ordenamiento es alfabético.

Veamos algunos ejemplos de ordenamiento de vectores, ya que podemos ordenar ascendente o decentemente los valores de un vector.

Ejercicios de Ordenamiento en C#

Ejemplo 1

Realizar un programa que dado un vector y que almacene 5 números para luego ordenar los números acendentemente.

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

namespace PruebaVector
{
    class PruebaVector
    {
        private int[] numeros;

        public void Cargar()
        {
            numeros = new int[5];
            for (int f = 0; f < numeros.Length; f++)
            {
                Console.Write("Ingrese numero "+(f+1)+": ");
                string linea = Console.ReadLine();
                numeros[f] = int.Parse(linea);
            }
        }

        public void Ordenar()
        {
            for (int k = 0; k < 4; k++)
            {
                for (int f = 0; f < 4 - k; f++)
                {
                    if (numeros[f] > numeros[f + 1])
                    {
                        int aux;
                        aux = numeros[f];
                        numeros[f] = numeros[f + 1];
                        numeros[f + 1] = aux;
                    }
                }
            }
        }

        public void Imprimir()
        {
            Console.WriteLine("Ordenado Ascendentemante");
            for (int f = 0; f < numeros.Length; f++)
            {
                Console.Write(numeros[f]+"  ");
            }
            Console.ReadKey();
        }

        static void Main(string[] args)
        {
            PruebaVector pv = new PruebaVector();
            pv.Cargar();
            pv.Ordenar();
            pv.Imprimir();
        }
    }
}


Al ejecutar el código muestra el siguiente resultado


Ejemplo 2

Definir un vector donde almacenar los nombres de 5 personas. Realizar un programa que ordene alfabéticamente

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

namespace PruebaVector
{
    class PruebaVector
    {
        private string[] nombre;

        public void Cargar()
        {
            nombre = new string[5];
            for (int f = 0; f < nombre.Length; f++)
            {
                Console.Write("Ingrese el nombre "+(f+1)+": ");
                nombre[f] = Console.ReadLine();
            }
        }

        public void Ordenar()
        {
            for (int k = 0; k < 4; k++)
            {
                for (int f = 0; f < 4 - k; f++)
                {
                    if (nombre[f].CompareTo(nombre[f + 1]) > 0)
                    {
                        string aux;
                        aux = nombre[f];
                        nombre[f] = nombre[f + 1];
                        nombre[f + 1] = aux;
                    }
                }
            }
        }

        public void Imprimir()
        {
            Console.WriteLine("Los nombres ordenados en forma alfabéticamente");
            for (int f = 0; f < nombre.Length; f++)
            {
                Console.WriteLine(nombre[f]);
            }
            Console.ReadKey();
        }


        static void Main(string[] args)
        {
            PruebaVector pv = new PruebaVector();
            pv.Cargar();
            pv.Ordenar();
            pv.Imprimir();
        }
    }
}

Al ejecutar el código muestra el siguiente resultado


 Ejemplo 3

Ingresar un vector de n elementos de tipo entero. Ordenar posteriormente el vector en forma ascendente

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

namespace PruebaVector
{
    class PruebaVector
    {
        private int[] vector;

        public void Cargar()
        {
            Console.Write("Cuantos longitud del vector:");
            string linea;
            linea = Console.ReadLine();
            int cant;
            cant = int.Parse(linea);
            vector = new int[cant];
            for (int f = 0; f < vector.Length; f++)
            {
                Console.Write("Ingrese elemento "+(f+1)+": ");
                linea = Console.ReadLine();
                vector[f] = int.Parse(linea);
            }
        }

        public void Ordenar()
        {
            for (int k = 0; k < vector.Length; k++)
            {
                for (int f = 0; f < vector.Length - 1 - k; f++)
                {
                    if (vector[f] > vector[f + 1])
                    {
                        int aux;
                        aux = vector[f];
                        vector[f] = vector[f + 1];
                        vector[f + 1] = aux;
                    }
                }
            }
        }

        public void Imprimir()
        {
            Console.WriteLine("Vector ordenados en forma ascendente");
            for (int f = 0; f < vector.Length; f++)
            {
                Console.Write(vector[f]+"  ");
            }
            Console.ReadKey();
        }


        static void Main(string[] args)
        {
            PruebaVector pv = new PruebaVector();
            pv.Cargar();
            pv.Ordenar();
            pv.Imprimir();
        }
    }
}
  
Al ejecutar el código muestra el siguiente resultado