Jump to content
bgmc

Ordenar numeros

Recommended Posts

bgmc

Boas a todos.

Ainda ando nos primeiros passos de programação em visual basic.

O problema é o seguinte:

Digitar seis numeros, e ordená-los.

Gostaria de saber se existe algum metodo facil para ordenar.

obrigado a todos

Share this post


Link to post
Share on other sites
jpaulino

Por exemplo (usando colecções)

        Dim numList As New List(Of Integer)
        numList.Add(1)
        numList.Add(24)
        numList.Add(12)
        numList.Add(33)
        numList.Add(8)

        numList.Sort()

        For Each num As Integer In numList
            Debug.WriteLine(num)
        Next

Share this post


Link to post
Share on other sites
jpaulino

Ou se preferires um array:

        Dim numList(5) As Integer
        numList(0) = 12
        numList(1) = 1
        numList(2) = 24
        numList(3) = 3
        numList(4) = 8
        numList(5) = 2

        Array.Sort(numList)

        For Each num As Integer In numList
            Debug.WriteLine(num)
        Next

Share this post


Link to post
Share on other sites
ribeiro55

Ou talvez até algo mais "manual", como costumam pedir nos exercicíos:

       Dim Numeros() = {6, 3, 1, 26, 13, 24, 8, 54, 12, 32, 65, 22, 7}

        Dim TempNum As Integer
        For i As Integer = Numeros.Length - 1 To 0 Step -1
            If i >= 1 Then
                If Numeros(i) < Numeros(i - 1) Then
                    TempNum = Numeros(i)
                    Numeros(i) = Numeros(i - 1)
                    Numeros(i - 1) = TempNum
                    i = Numeros.Length
                End If
            End If
        Next


Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Share this post


Link to post
Share on other sites
jpaulino

Ou talvez até algo mais "manual", como costumam pedir nos exercicíos:

Um bubble sort ;)

Share this post


Link to post
Share on other sites
ribeiro55

OFFTOPIC

Mas isto até tem nome?

;):);) eu fui Googlar "bubble sort" :wallbash: Nunca tinha ouvido falar 😳

@bgmc

Diz qualquer coisa! Já tens para aqui material :)


Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.