domingo, 16 de junio de 2013

ORDENAMIENTO DE LA BURBUJA

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

namespace Algoritmo_de_la_burbuja
{
    class Program
    {
        class ordenamiento
        {
            int tam, comp, interc;
       
            public static void Burbuja(int[] vec)
            {
                for (int i = 0; i < vec.Length - 1; i++)
                    for (int j = vec.Length - 1; i < j; j--)
                    {
                        if (vec[j - 1] > vec[j])
                        {
                            int aux = vec[j - 1];
                            vec[j - 1] = vec[j];
                            vec[j] = aux;
                        }
                    }
             }
        }

=================================================================================
static void Main(string[] args)
            {
                Console.Write("Datos Desordenados \n");
                int[] vec = { 56, 10, 100, 90, 80, 30, 10, 5, 6 };

                for (int i = 0; i < vec.Length; i++)
                    Console.WriteLine(vec[i]);
                Burbuja(vec);
               

                Console.WriteLine("\n \nDatos Ordenados ");
                for (int i = 0; i < vec.Length; i++)
                    Console.WriteLine(vec[i]);
                Console.ReadKey();

            }
        }
    }

}




Burbuja 2

Este es el mismo metodo solo que la implementacion es diferente


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

namespace Burbuja_Equipo
{
    class Burbuja
    {
        int[] vec;

        public  int[] Ordenar(int[] Vec)
        {
            int aux;

            for (int i = 1; i <= Vec.Length; i++)
                for (int j = Vec.Length - 1; j >= i; j--)
                    if (Vec[j - 1] > Vec[j])
                    {
                        aux = Vec[j - 1];
                        Vec[j - 1] = Vec[j];
                        Vec[j] = aux;
                    }
            return Vec;

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

namespace Burbuja_Equipo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] v = new int[9];

            Random r = new Random();

            Console.WriteLine("Desordenados ");
            for (int i = 0; i < v.Length; i++)
            {
                v[i] = r.Next(99) + 1;
                Console.WriteLine("Vector[" + i.ToString() + "] " + v[i]);
            }

            Burbuja nums = new Burbuja(v);
            v = nums.Ordenar(v);
        
            Console.WriteLine("Datos Ordenados ");
            for (int i = 0; i < v.Length; i++)
                Console.WriteLine(v[i]);

            Console.ReadKey();
        }
    }

}



No hay comentarios:

Publicar un comentario