CLR: .Net / Mono / Boo / Otros CLR > C#

 Metodo De La Burbuja

(1/2) > >>

Chiog19:
lyhil

jcgarza:
Espero que te sirva lo siguiente:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Burbuja
{
   /// <summary>
   /// Summary description for Form1.
   /// </summary>
   public class frmBurbuja : System.Windows.Forms.Form
   {
      private System.Windows.Forms.TextBox txtNumElementos;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Button btnCrearArreglo;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label lblRestantes;
      private System.Windows.Forms.TextBox txtValor;
      private System.Windows.Forms.Button btnIngresar;
      private System.Windows.Forms.Button btnOrdenar;
      private System.Windows.Forms.Button btnOtro;
      private System.Windows.Forms.Button btnSalir;
      private System.Windows.Forms.ListBox lstArreglo;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      public frmBurbuja()
      {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
         if( disposing )
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.txtNumElementos = new System.Windows.Forms.TextBox();
         this.label1 = new System.Windows.Forms.Label();
         this.btnCrearArreglo = new System.Windows.Forms.Button();
         this.label2 = new System.Windows.Forms.Label();
         this.lblRestantes = new System.Windows.Forms.Label();
         this.txtValor = new System.Windows.Forms.TextBox();
         this.btnIngresar = new System.Windows.Forms.Button();
         this.btnOrdenar = new System.Windows.Forms.Button();
         this.lstArreglo = new System.Windows.Forms.ListBox();
         this.btnOtro = new System.Windows.Forms.Button();
         this.btnSalir = new System.Windows.Forms.Button();
         this.SuspendLayout();
         //
         // txtNumElementos
         //
         this.txtNumElementos.Location = new System.Drawing.Point(16, 32);
         this.txtNumElementos.Name = "txtNumElementos";
         this.txtNumElementos.TabIndex = 0;
         this.txtNumElementos.Text = "";
         //
         // label1
         //
         this.label1.Location = new System.Drawing.Point(16, 8);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(120, 23);
         this.label1.TabIndex = 1;
         this.label1.Text = "Numero de elementos";
         //
         // btnCrearArreglo
         //
         this.btnCrearArreglo.Location = new System.Drawing.Point(136, 32);
         this.btnCrearArreglo.Name = "btnCrearArreglo";
         this.btnCrearArreglo.Size = new System.Drawing.Size(96, 23);
         this.btnCrearArreglo.TabIndex = 2;
         this.btnCrearArreglo.Text = "Crear arreglo";
         this.btnCrearArreglo.Click += new System.EventHandler(this.btnCrearArreglo_Click);
         //
         // label2
         //
         this.label2.Location = new System.Drawing.Point(16, 80);
         this.label2.Name = "label2";
         this.label2.TabIndex = 3;
         this.label2.Text = "Valor:";
         //
         // lblRestantes
         //
         this.lblRestantes.Location = new System.Drawing.Point(24, 136);
         this.lblRestantes.Name = "lblRestantes";
         this.lblRestantes.TabIndex = 4;
         //
         // txtValor
         //
         this.txtValor.Enabled = false;
         this.txtValor.Location = new System.Drawing.Point(16, 104);
         this.txtValor.Name = "txtValor";
         this.txtValor.TabIndex = 5;
         this.txtValor.Text = "";
         //
         // btnIngresar
         //
         this.btnIngresar.Enabled = false;
         this.btnIngresar.Location = new System.Drawing.Point(136, 104);
         this.btnIngresar.Name = "btnIngresar";
         this.btnIngresar.Size = new System.Drawing.Size(96, 23);
         this.btnIngresar.TabIndex = 6;
         this.btnIngresar.Text = "Ingresar valor";
         this.btnIngresar.Click += new System.EventHandler(this.btnIngresar_Click);
         //
         // btnOrdenar
         //
         this.btnOrdenar.Enabled = false;
         this.btnOrdenar.Location = new System.Drawing.Point(136, 168);
         this.btnOrdenar.Name = "btnOrdenar";
         this.btnOrdenar.Size = new System.Drawing.Size(96, 23);
         this.btnOrdenar.TabIndex = 7;
         this.btnOrdenar.Text = "Ordenar";
         this.btnOrdenar.Click += new System.EventHandler(this.btnOrdenar_Click);
         //
         // lstArreglo
         //
         this.lstArreglo.Location = new System.Drawing.Point(288, 16);
         this.lstArreglo.Name = "lstArreglo";
         this.lstArreglo.Size = new System.Drawing.Size(144, 95);
         this.lstArreglo.TabIndex = 8;
         //
         // btnOtro
         //
         this.btnOtro.Location = new System.Drawing.Point(320, 128);
         this.btnOtro.Name = "btnOtro";
         this.btnOtro.TabIndex = 9;
         this.btnOtro.Text = "Otro";
         this.btnOtro.Click += new System.EventHandler(this.btnOtro_Click);
         //
         // btnSalir
         //
         this.btnSalir.Location = new System.Drawing.Point(320, 168);
         this.btnSalir.Name = "btnSalir";
         this.btnSalir.TabIndex = 10;
         this.btnSalir.Text = "Salir";
         this.btnSalir.Click += new System.EventHandler(this.btnSalir_Click);
         //
         // frmBurbuja
         //
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(256, 214);
         this.Controls.Add(this.btnSalir);
         this.Controls.Add(this.btnOtro);
         this.Controls.Add(this.lstArreglo);
         this.Controls.Add(this.btnOrdenar);
         this.Controls.Add(this.btnIngresar);
         this.Controls.Add(this.txtValor);
         this.Controls.Add(this.lblRestantes);
         this.Controls.Add(this.label2);
         this.Controls.Add(this.btnCrearArreglo);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.txtNumElementos);
         this.Name = "frmBurbuja";
         this.Text = "Ordenar por burbuja";
         this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
         Application.Run(new frmBurbuja());
      }

      int[] Arreglo;
      int NumElementos;
      int Indice=0;

      private void btnCrearArreglo_Click(object sender, System.EventArgs e)
      {
         try
         {
            NumElementos = int.Parse(txtNumElementos.Text);
            Arreglo = new int[NumElementos];
         }
         catch (Exception Exc)
         {
            MessageBox.Show("Imposible convertir el valor");
         }
         txtValor.Enabled=true;
         btnIngresar.Enabled=true;

         txtNumElementos.Enabled=false;
         btnCrearArreglo.Enabled=false;
      }

      private void btnIngresar_Click(object sender, System.EventArgs e)
      {
         try
         {
            Arreglo[Indice]=int.Parse(txtValor.Text);
            Indice++;
         }
         catch (Exception Exc)
         {
            MessageBox.Show("Imposible convertir el valor");
         }
         if (Indice>=NumElementos)
         {
            txtValor.Enabled=false;
            btnIngresar.Enabled=false;
            btnOrdenar.Enabled=true;
         }
      }

      private void btnOrdenar_Click(object sender, System.EventArgs e)
      {
         int bandera=0;
         int temp;
         while (bandera == 0)
         {
            bandera = 1;
            for(int i =0; i< NumElementos-1; i++)
               if(Arreglo > Arreglo [i+1])
               {
                  temp = Arreglo ;
                  Arreglo = Arreglo [i+1];
                  Arreglo [i+1] = temp;
                  bandera = 0;
               }
         }
         this.Width=460;
         btnOtro.Enabled=true;
         btnSalir.Enabled=true;
         btnOrdenar.Enabled=false;
         lstArreglo.Items.Clear();
         foreach (int i in Arreglo)
         {
            lstArreglo.Items.Add(i);
         }

      }

      private void btnOtro_Click(object sender, System.EventArgs e)
      {
         Indice=0;
         this.Width=264;
         btnSalir.Enabled=false;
         btnOtro.Enabled=false;
         btnCrearArreglo.Enabled=true;
         txtNumElementos.Enabled=true;
      }

      private void btnSalir_Click(object sender, System.EventArgs e)
      {
         Close();
      }
   }
}

Chiog19:
..o

jcgarza:
Jajaja, espero que a tus maestros no les extrañe el codigo...

Para el shell solo sustituye el codigo del event handler btnOrdenar_Clic por lo siguiente

private void btnOrdenar_Click(object sender, System.EventArgs e)
      {
         int i, j, incremento, temp;

         incremento = 3;
         while (incremento > 0)
         {
            for (i=0; i < NumElementos; i++)
            {
               j = i;
               temp = Arreglo;
               while ((j >= incremento) && (Arreglo[j-incremento] > temp))
               {
                  Arreglo[j] = Arreglo[j - incremento];
                  j = j - incremento;
               }
               Arreglo[j] = temp;
            }
            if (incremento/2 != 0)
            {
               incremento = incremento/2;
            }
            else
            {
               if (incremento == 1)
               {
                  incremento = 0;
               }
               else
               {
                  incremento = 1;
               }
            }
         }

         this.Width=460;
         btnOtro.Enabled=true;
         btnSalir.Enabled=true;
         btnOrdenar.Enabled=false;
         lstArreglo.Items.Clear();
         foreach (int k in Arreglo)
         {
            lstArreglo.Items.Add(k);
         }
      }


En cuanto a los demas no los recuerdo, pero seria cuestion de que consiguieras los algoritmos en internet

Chiog19:
ppuil

Navegación

[0] Índice de Mensajes

[#] Página Siguiente

Ir a la versión completa