miércoles, 28 de noviembre de 2012

Ordenar en C# por consola


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

namespace ordenar_vector
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[100];
            int n;
            int temp, j;

            Console.WriteLine("ingrese numero:");
            n = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                Console.Write("Elemento" + "[" + (i + 1) + "]: ");
                num[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 1; i < n; i++)
            {
                for (j = n - 1; j >= i; j--)
                {
                    if (num[j - 1] > num[j])
                    {
                        temp = num[j - 1];
                        num[j - 1] = num[j];
                        num[j] = temp;
                    }
                }
            }


            Console.WriteLine("Vector ordenado:");
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Elemento " + "[" + (i + 1) + "]: " + num[i]);
            }

            string x = Console.ReadLine();
        }
    }
}

No hay comentarios:

Publicar un comentario

Nota: solo los miembros de este blog pueden publicar comentarios.