4 Replies - 814 Views - Last Post: 19 January 2012 - 07:33 AM Rate Topic: -----

#1 steph1389  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-January 12

[Question] Sorting and array

Posted 19 January 2012 - 04:03 AM

Hello, I am quite new at VB.NET and just need helo on sorting algorithms, prferbly the fastest (quick sort) but bubble would so. It is for sorting and array for 12 numbers, which are being enter by the user via a input box. These are then transfered onto a listbox for visual aid.

so the steps involved would be:

    (already done)
  • 1) Enter then inputs into array via inputbox
  • 2) Input to sppear in listbox
    (not done)
  • 3) Sort the array ascending/decending
  • 4) print results into the listbox again.

In my code there is also a total and a average, but that doesnt need any help on it.

Public Class Form1
    Dim total As Integer
    Dim array(11) As Integer

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnMonthlyAmmount.Click
        Dim count As Integer
        Dim element As Integer
        Dim Month As Integer
        Dim ValueEntered As Integer
        Dim ValueFormatted As Object
        count = 0
        Month = 1

        While count <= UBound(array)
            ValueEntered = Int(InputBox("Please enter the monthly total: "))
            ValueFormatted = ("Month " & Month & ": " & ValueEntered)
            array(count) = ValueEntered
            ListBox1.Items.Add(ValueFormatted)
            count = (count + 1)
            Month = Month + 1
        End While

        If count > UBound(array) Then
            btnMonthlyAmmount.Enabled = False
        End If

        For Each element In array
            total = total + element
        Next element

    End Sub

    Private Sub btnYearlyUsageCalc_Click(sender As System.Object, e As System.EventArgs) Handles btnYearlyUsageCalc.Click
        lblTotalUseageYear.Text = total
    End Sub

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        lblMonthlyUsedAverage.Text = total / array.Length
    End Sub
End Class



Thanks in advance.

ps. have looked at other code example via google, but can't get my head around the.

Is This A Good Question/Topic? 0
  • +

Replies To: [Question] Sorting and array

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: [Question] Sorting and array

Posted 19 January 2012 - 05:40 AM

We have a code snippet which will help you to see how to sort array of numbers. Try it and tell us if you have any problem implementing it.
Was This Post Helpful? 1
  • +
  • -

#3 steph1389  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-January 12

Re: [Question] Sorting and array

Posted 19 January 2012 - 07:00 AM

Juts follwed that code snippet now, and everything seems to work like it should. Numbers have been sorted via the watch mode in the array, so thanks for that!

But now I am wondering two other things:

1) How would I reverse this? Would I be changing the ">" in this line of code
If array(x) > array(j) Then
to "<"

2) If I wanted to print this whole array out into a ListBox, is there any easier way than a loop with a conter in it?

Thanks again,
Stephan.
Was This Post Helpful? 0
  • +
  • -

#4 steph1389  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-January 12

Re: [Question] Sorting and array

Posted 19 January 2012 - 07:07 AM

Removed lazyness and tried out the switching of the cmaparing operator, and all worked out well.

Just wondering now about the second question; is there an easier way to print the array into a ListBox without using a loop witha counter?
Was This Post Helpful? 0
  • +
  • -

#5 steph1389  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-January 12

Re: [Question] Sorting and array

Posted 19 January 2012 - 07:33 AM

Ignore this now, sorted it all out.

Public Class Form1
    Dim total As Integer
    Dim array(11) As Integer

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnMonthlyAmmount.Click
        Dim count As Integer
        Dim element As Integer
        Dim Month As Integer
        Dim ValueEntered As Integer
        Dim ValueFormatted As Object
        count = 0
        Month = 1

        While count <= UBound(array)
            ValueEntered = Int(InputBox("Please enter the monthly total: "))
            ValueFormatted = ("Month " & Month & ": " & ValueEntered)
            array(count) = ValueEntered
            ListBox1.Items.Add(ValueFormatted)
            count = (count + 1)
            Month = Month + 1
        End While

        If count > UBound(array) Then
            btnMonthlyAmmount.Enabled = False
        End If

        For Each element In array
            total = total + element
        Next element

    End Sub

    Private Sub btnYearlyUsageCalc_Click(sender As System.Object, e As System.EventArgs) Handles btnYearlyUsageCalc.Click
        lblTotalUseageYear.Text = total
    End Sub

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        lblMonthlyUsedAverage.Text = total / array.Length
    End Sub

    Private Sub btnAscending_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAscending.Click
        Dim AscendCount As Integer = 0
        Dim AscendCountFormatted As Object
        Dim AscendValue As Integer
        Dim Month As Integer

        Month = 1

        ListBox1.Items.Clear()

        For j As Integer = 0 To array.Length - 1
            For x As Integer = 0 To array.Length - 1

                ' Compare each numbers of the array 
                ' with the comparator
                If array(x) > array(j) Then

                    ' If a number preceeding the comparator
                    ' is greater than the comparator
                    ' exchange/switch the numbers
                    array(x) = array(x) + array(j)
                    array(j) = array(x) - array(j)
                    array(x) = array(x) - array(j)
                End If
            Next
        Next

        For i = 0 To UBound(array)
            AscendValue = array(AscendCount)
            AscendCountFormatted = ("Month " & Month & ": " & AscendValue)
            ListBox1.Items.Add(AscendCountFormatted)
            AscendCount = AscendCount + 1
            Month = Month + 1
        Next

    End Sub

    Private Sub btnSoerDecendingly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSoerDecendingly.Click

        Dim AscendCount As Integer = 0
        Dim AscendCountFormatted As Object
        Dim AscendValue As Integer
        Dim Month As Integer

        Month = 12

        ListBox1.Items.Clear()

        For j As Integer = 0 To array.Length - 1
            For x As Integer = 0 To array.Length - 1

                ' Compare each numbers of the array 
                ' with the comparator
                If array(x) < array(j) Then

                    ' If a number preceeding the comparator
                    ' is greater than the comparator
                    ' exchange/switch the numbers
                    array(x) = array(x) + array(j)
                    array(j) = array(x) - array(j)
                    array(x) = array(x) - array(j)
                End If
            Next
        Next

        For i = 0 To UBound(array)
            AscendValue = array(AscendCount)
            AscendCountFormatted = ("Month " & Month & ": " & AscendValue)
            ListBox1.Items.Add(AscendCountFormatted)
            AscendCount = AscendCount + 1
            Month = Month - 1
        Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class



If anyone knows of more efficent ways then please do say, but thank you smohd for your help, youve been +1'ed :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1