Pages

viernes, 25 de enero de 2013

Ordenar creciente o decreciente una lista de N números Codigo Fuente en Visual Basic

Problema
Hacer un programa o algoritmo.
Se desea realizar un algoritmo que realice las siguientes tareas:
  • Leer una lista de números enteros
  • Visualizar dichos números
  • El algoritmo deberá pedir si yo deseo ordenar en sentido decreciente o creciente (burbuja)
Solución
  • Entrada: 10, 15, 20, 8
  • Salida: decreciente 20 15 10 8
Codigo Fuente en Visual Basic
Public Class Form1
    Dim t, lon, num, tem, d As Integer
    Dim cad As String = ""
    Dim sal As String = ""
    Dim c As Integer = 0
    Dim w As Integer = 0
    Dim b As Integer = 0
    Dim N() As Integer
    Public Sub inicializar(ByVal tam As Integer)
        ReDim N(tam - 1)
        For i As Integer = 0 To (tam - 1)
            N(i) = 0
        Next i
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        t = TextBox1.Text
        inicializar(t)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button1.Click
        lon = TextBox1.Text
        num = TextBox2.Text
        If (lon <> c) Then
            N(c) = num
            cad = cad + "  " + Trim(N(c))
            c = c + 1
            TextBox3.Text = cad
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button2.Click
        w = 0
        cad = ""
        d = TextBox1.Text
        While (w <> (d - 1))
            w = 0
            For k As Integer = 0 To (lon - 2)
                If (N(k) >= N(k + 1)) Then
                    w = w + 1
                Else
                    tem = N(k)
                    N(k) = N(k + 1)
                    N(k + 1) = tem
                End If
            Next k
        End While
        For x As Integer = 0 To (lon - 1)
            cad = cad + "  " + Trim(N(x))
        Next x
        TextBox4.Text = cad
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button3.Click
        b = 0
        sal = ""
        d = TextBox1.Text
        While (b <> (d - 1))
            b = 0
            For k As Integer = 0 To (d - 2)
                If (N(k) <= N(k + 1)) Then
                    b = b + 1
                Else
                    tem = N(k)
                    N(k) = N(k + 1)
                    N(k + 1) = tem
                End If
            Next k
        End While
        For x As Integer = 0 To (d - 1)
            sal = sal + "  " + Trim(N(x))
        Next x
        TextBox4.Text = sal
    End Sub
End Class

0 comentarios:

Publicar un comentario