hi
I am trying to write a code about counting inversion algorithm
I finished the code but the result was wrong
I dont no where is the problem but I think it is in the count-merg function
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
This post has been edited by loloalssum: 17 Jun, 2009 - 12:46 PM