HeapSorting II

weeshen

Member
Joined
Oct 1, 2006
Messages
13
Programming Experience
Beginner
So I have sat down and written some code out and looked at my book for some reference and read and read and all of the code looks ok except for a few things which are heapArray, currsize, Heap(SIZE) at bottom of program...if someone can get me in the right direction to getting these to work (since they don't exist obviously) I would be most greatful.:)
VB.NET:
Module Module1
    Public Class CArray
        Private arr() As Integer
        Private numelements As Integer

        Public Sub New(ByVal number As Integer)
            ReDim Preserve arr(number)
            numelements = 0
        End Sub

        Public Sub insert(ByVal item As Integer)
            If (numelements > arr.GetUpperBound(0)) Then
                ReDim Preserve arr(numelements * 1.25)
            End If
            arr(numelements) = item
            numelements += 1
        End Sub

        Public Sub showArray()
            Dim index As Integer
            For index = 0 To numelements - 1
                Console.WriteLine(arr(index) & " ")
            Next
            Console.WriteLine()
        End Sub
    End Class
    Class Node
        Public data As Integer

        Public Sub New(ByVal key As Integer)
            data = key
        End Sub
    End Class

    Public Sub shiftup(ByVal index As Integer)
        Dim parent As Integer = (index - 1) \ 2
        Dim bottom As Node = heapArray(index)
        While (index > 0 And heapArray(parent).data < _
        bottom.data)
            heapArray(index) = heapArray(parent)
            index = parent
            parent = (parent - 1) / 2
        End While
    End Sub

    Public Function insert(ByVal key As Integer) As Boolean
        If (currSize = maxSize) Then
            Return False
        End If
        Dim newNode As New Node(key)
        heapArray(currSize) = newNode
        shiftup(currSize)
        currSize += 1
        Return True
    End Function

    Public Function Remove() As Node
        Dim root As Node = heapArray(0)
        currSize -= 1
        heapArray(0) = heapArray(currSize)
        ShiftDown(0)
        Return root
    End Function

    Public Sub ShiftDown(ByVal index As Integer)
        Dim largerChild As Integer
        Dim top As Node = heapArray(index)
        While (index < (currSize \ 2))
            Dim leftChild As Integer = 2 * index + 1
            Dim rightChild As Integer = leftChild + 1
            If (rightChild < currSize And heapArray(leftChild). _
            data < heapArray(rightChild).data) Then
                largerChild = rightChild
            Else
                largerChild = leftChild
            End If
            If (top.data >= heapArray(largerChild).data_) Then
                Exit While
            End If
            heapArray(index) = heapArray(largerChild)
            index = largerChild
        End While
        heapArray(index) = top
    End Sub
    Sub Main()
        Const SIZE As Integer = 9
        Dim aHeap As New Heap(SIZE)
        Dim sortedHeap(SIZE) As Node
        Dim index As Integer
        For index = 0 To SIZE - 1
            Dim aNode As New Node(Int(100 * Rnd() + 1))
            aHeap.InsertAt(index, aNode)
            aHeap.incSize()
        Next
        Console.WriteLine("Random: ")
        aHeap.showArray()
        Console.WriteLine()
        Console.WriteLine("Heap: ")
        For index = SIZE \ 2 - 1 To 0 Step -1
            aHeap.ShiftDown(index)
        Next
        aHeap.ShowArray()
        For index = SIZE - 1 To 0 Step -1
            Dim bigNode As Node = aHeap.Remove()
            aHeap.InsertAt(index, bigNode)
        Next
        Console.WriteLine()
        Console.WriteLine("Sorted: ")
        aHeap.showArray()
        Console.Read()
    End Sub

End Module
 
Ok, I got the final code done. So for those of you who ever need heap sorting algorithm here you go...enjoy it!
VB.NET:
Module Module1
    Public Class CArray
        Private currSize As Integer
        Private maxSize As Integer
        Private arr() As Integer
        Private numelements As Integer
        Public Sub New(ByVal number As Integer)
            ReDim Preserve arr(number)
            currSize = 0
            numelements = 0
            maxSize = number
        End Sub
        Public Sub showArray()
            Dim index As Integer
            For index = 0 To numelements - 1
                Console.Write(arr(index) & " ")
            Next
            Console.WriteLine()
        End Sub
        Public Sub shiftup(ByVal index As Integer)
            Dim parent As Integer = (index - 1) \ 2
            Dim bottom As Integer = arr(index)
            While (index > 0 And arr(parent) < _
            bottom)
                arr(index) = arr(parent)
                index = parent
                parent = (parent - 1) \ 2
            End While
            arr(index) = bottom
        End Sub
        Public Function Insert(ByVal key As Integer) As Boolean
            If (currSize = maxSize) Then
                Return False
            End If
            arr(currSize) = key
            shiftup(currSize)
            currSize += 1
            numelements += 1
            Return True
        End Function
        Public Function Remove() As Integer
            Dim root As Integer = arr(0)
            'Console.WriteLine("root is" & root)
            currSize -= 1
            arr(0) = arr(currSize)
            ShiftDown(0)
            arr(currSize) = root
            'Console.WriteLine("arr(" & currSize & ") is " & arr(currSize))
            Return root
        End Function
        Public Sub ShiftDown(ByVal index As Integer)
            Dim largerChild As Integer
            Dim top As Integer = arr(index)
            While (index < (currSize \ 2))
                Dim leftChild As Integer = 2 * index + 1
                Dim rightChild As Integer = leftChild + 1
                If (rightChild < currSize And arr(leftChild) _
                < arr(rightChild)) Then
                    largerChild = rightChild
                Else
                    largerChild = leftChild
                End If
                If (top >= arr(largerChild)) Then
                    Exit While
                End If
                arr(index) = arr(largerChild)
                index = largerChild
            End While
            arr(index) = top
        End Sub
    End Class
    Sub Main()
        Randomize()
        Const SIZE As Integer = 10
        Dim theArray As New CArray(SIZE)
        Dim index As Integer
        Console.WriteLine("Random: ")
        For index = 0 To SIZE - 1
            Dim value As Integer = (Int(100 * Rnd() + 1))
            Console.Write(value & " ")
            theArray.Insert(value)
        Next
        Console.WriteLine()
        Console.WriteLine()
        Console.WriteLine("Heap: ")
        theArray.showArray()
        For index = SIZE - 1 To 0 Step -1
            Dim bigNode As Integer = theArray.Remove()
        Next
        Console.WriteLine()
        Console.WriteLine("Sorted: ")
        theArray.showArray()
        Console.Read()
    End Sub
End Module
 
Back
Top