VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 307,159 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,528 people online right now. Registration is fast and FREE... Join Now!




counting inversion

 

counting inversion

loloalssum

17 Jun, 2009 - 12:44 PM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2009
Posts: 5

hi

I am trying to write a code about counting inversion algorithm

I finished the code but the result was wrong crazy.gif

I dont no where is the problem but I think it is in the count-merg function blink.gif

can you please see the code and tell me if there is error????

CODE


Public Class Form1
    Dim _list As New List(Of Integer)


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim x As Integer

        x = value.Text
        _list.Add(x)
        value.Clear()

    End Sub


    Function merg_and_count(ByVal A As List(Of Integer), ByVal B As List(Of Integer), ByRef result As List(Of Integer)) As Integer

        Dim i, j, k, count As Integer

        i = 0
        j = 0
        k = 0
        count = 0

        While i < A.Count And j < B.Count
            If A(i) < B(j) Then
                result(k) = A(i)
                Label2.Text = A(i)
                i += 1
            Else
                result(k) = B(j)
                Label3.Text = B(j)
                j += 1
                count += 1
                Label3.Text = count
            End If
            k += 1
        End While

        If i = A.Count Then
            While j < B.Count
                result(k) = B(j)
                j += 1
                k += 1
            End While
        Else
            While i < A.Count
                result(k) = A(i)
                i += 1
                k += 1
            End While

        End If
        Return count

    End Function
    Function sort_and_count(ByRef l As List(Of Integer), ByVal i As Integer, ByVal j As Integer) As Integer


        Dim r1, r2, r As Integer
        Dim A As New List(Of Integer)
        Dim B As New List(Of Integer)

        Dim m As Integer

        If l.Count = 1 Then
            Return 0
        Else
            m = Math.Round(j / 2)


            ' split list into tow list A and B

            A = l.GetRange(0, m + 1)        ' A the lift of list from 0 to m

            B = l.GetRange(m + 1, j - m)    ' B the right of list from m+1 to j


            r1 = sort_and_count(A, 0, m)
            r2 = sort_and_count(B, 0, j - m - 1)

            r = merg_and_count(A, B, l)


        End If

        Return (r1 + r2 + r)

    End Function


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim l As New List(Of Integer)
        Dim j As Integer

        j = _list.Count
        l = _list.GetRange(0, j)
        TextBox1.Text = sort_and_count(l, 0, j - 1)

End Sub


End Class



I hope you can help me...

Thank you smile.gif

This post has been edited by loloalssum: 17 Jun, 2009 - 12:46 PM

User is offlineProfile CardPM
+Quote Post


mark.bottomley

RE: Counting Inversion

17 Jun, 2009 - 02:35 PM
Post #2

D.I.C Addict
****

Joined: 22 Apr, 2009
Posts: 868



Thanked: 148 times
My Contributions
First odd thing scanning it is that the count variable is only incremented in the else side of the first while loop of merg_and_count. I'm not sure of the use of count, so it could be unrelated.

It does not work correctly for odd number of elements and divides unequally for even number of elements.

For the correct operation it maybe should be:

A = l.GetRange(0, m) ' A the lift of list from 0 to m

B = l.GetRange(m, j - m) ' B the right of list from m+1 to j


r1 = sort_and_count(A, 0, m)
r2 = sort_and_count(B, 0, j - m)

I get 11 as the textbox1 output, but I don't know what the means.
(sorted 1,9,2,8,3,7,4,6,5).

User is offlineProfile CardPM
+Quote Post

loloalssum

RE: Counting Inversion

17 Jun, 2009 - 02:50 PM
Post #3

New D.I.C Head
*

Joined: 15 Jun, 2009
Posts: 5

Than you

I solve the problem biggrin.gif

the counter must not increment by 1

it must be

CODE

count+=A.count-i


thank you biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:53PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month