Hi Guys. I hope everyone in doing great today:-)
I have some problem, this coe does not produce mi in aplhabetical oreder.
I am using Heapsort as a sorting algo...and please help me
Hope to hear from you guys;-)
Public Class MyForm
Private MyTable As New DataTable
Private MySortedTable As New DataTable
Private CounterToMySortedTable As Integer
Private Sub SwapSmallest()
Dim ParentNode, ResultOfCompare As Integer
Dim temp As String
Try
ParentNode = 0
ResultOfCompare = StrComp(MyTable.Rows(ParentNode + 1)("ChildName"), MyTable.Rows(ParentNode + 2)("ChildName"))
If ResultOfCompare = -1 Then
temp = MyTable.Rows(ParentNode + 1)("ChildName")
MyTable.Rows(ParentNode + 1)("ChildName") = MyTable.Rows(0)("ChildName")
MyTable.Rows(0)("ChildName") = temp
Else
temp = MyTable.Rows(ParentNode + 2)("ChildName")
MyTable.Rows(ParentNode + 2)("ChildName") = MyTable.Rows(0)("ChildName")
MyTable.Rows(0)("ChildName") = temp
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Promote(ByVal PromotedChild As String)
Try
MyTable.Rows(0)("ChildName") = PromotedChild
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub PutToSortedDataTable(ByVal id As Integer, ByVal EntryName As String)
Try
MySortedTable.Rows.Add(id, EntryName)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Function ArrangeInAlphabeticalOrder() As DataTable
Dim i, Id As Integer
Try
For i = MyTable.Rows.Count - 1 To 0 Step -1
Id += 1
PutToSortedDataTable(Id, MyTable.Rows(0)("ChildName"))
Promote(MyTable.Rows(i)("ChildName"))
SwapSmallest()
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
ArrangeInAlphabeticalOrder = MySortedTable
End Function
Private Sub HeapifyMyTable()
Dim Parent As Integer
Dim ResultCompare, Last As Integer
Dim temp As String
Try
Last = MyTable.Rows.Count - 1
Parent = (Last - 1) / 2
Do
If MyTable.Rows.Count > 0 Then
ResultCompare = StrComp(MyTable.Rows(Parent)("ChildName"), MyTable.Rows(Last)("ChildName"))
If ResultCompare = 1 Then
temp = MyTable.Rows(Last)("ChildName")
MyTable.Rows(Last)("ChildName") = MyTable.Rows(Parent)("ChildName")
MyTable.Rows(Parent)("ChildName") = temp
Last = Parent
Parent = (Last - 1) / 2
End If
End If
Loop Until ResultCompare = -1 Or ResultCompare = 0
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub GetNameFromOldDataGridView()
Dim EntryName As String
Dim i As Integer
Dim row As DataRow = MyTable.NewRow
MyTable.Columns.Add("ChildNameID")
MyTable.Columns.Add("ChildName")
Try
MySortedTable.Columns.Add("ChildNameID")
MySortedTable.Columns.Add("ChildName")
For i = 0 To OldDataGridView.Rows.Count - 1
EntryName = OldDataGridView.Rows(i).Cells(1).Value
MyTable.Rows.Add(i + 1, EntryName)
HeapifyMyTable()
Next
MySortedTable = ArrangeInAlphabeticalOrder()
NewDataGridView.DataSource = MySortedTable
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ChildNameTableAdapter.Fill(TestDataSet.ChildName)
End Sub
Private Sub ArrangeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ArrangeButton.Click
Call GetNameFromOldDataGridView()
End Sub
End Class
`