Pages

miércoles, 23 de enero de 2013

Insertar n números en un arreglo ordenado Codigo Fuente en Visual Basic

Problema
Hacer un programa o algoritmo. Se tiene un arreglo "A" donde se encuentra 10 números ordenados en forma ascendente, y se pide que el algoritmo me permita el ingreso de un número y lo coloque en la posición donde debería de estar con respecto al orden del arreglo.

Codigo Fuente en Visual Basic
Public Class Form1
    Dim lista() As Integer = {1, 25, 28, 31, 56, 98, 132, 195, 282, 502}
    Dim cola As New Queue()
    Dim cont As Integer = 0
    Dim sal As String = ""
    Private Sub Button1_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim w As Integer = 0
        Dim cad As String = ""
        Dim C, T, tem As Integer
        For it As Integer = 0 To 9
            cola.Enqueue(lista(it))
        Next it
        C = TextBox1.Text
        T = C + 10
        Dim lista1() As Object = cola.ToArray()
        While (w <> (T - 1))
            w = 0
            For k As Integer = 0 To (T - 2)
                If (lista1(k) <= lista1(k + 1)) Then
                    w = w + 1
                Else
                    tem = lista1(k)
                    lista1(k) = lista1(k + 1)
                    lista1(k + 1) = tem
                End If
            Next k
        End While
        For p As Integer = 0 To (T - 1)
            cad = cad + " " + Trim(lista1(p))
        Next p
        TextBox4.Text = cad
        cola.Clear()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
        For z As Integer = 0 To 9
            sal = sal + " " + Trim(lista(z))
        Next z
        TextBox3.Text = sal
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button2.Click
        Dim algo As Integer
        algo = TextBox1.Text
        If (cont <> algo) Then
            cola.Enqueue(TextBox2.Text)
            cont = cont + 1
        End If
    End Sub
End Class

0 comentarios:

Publicar un comentario en la entrada