Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


sorting +VE & -VE numbers

tembsstembss Member Posts: 1
Hi all.
I am trying to sort numbers that are mixed, i.e. positive numbers and negative numbers (with a'-' in front to indicate -ve). I am using the following code that I found on the forum to test:
Public Sub OrderNumbers(ByRef iInputArray() As Integer, ByVal iArraySize As Integer)
Dim iArray() As Integer
Dim iOrdered() As Integer
Dim iCounter As Integer
ReDim iArray(0 To iArraySize - 1)
ReDim iOrdered(0 To iArraySize - 1)

For iCounter = 0 To iArraySize - 1
iArray(iCounter) = iInputArray(iCounter)


For iCounter = 0 To iArraySize - 1
iOrdered(iCounter) = GetLargest(iArray, iArraySize)
iArray(buffer) = 0

For iCounter = 0 To UBound(iOrdered)
iInputArray(iCounter) = iOrdered(iCounter)
End Sub

Private Function GetLargest(ByVal iInputArray() As Integer, ByVal iInputSize As Integer) As Integer
Dim i As Integer
Dim n As Integer
Dim iBuffArray() As Integer
ReDim iBuffArray(0 To iInputSize - 1)

iBuffArray = iInputArray

For i = 0 To iInputSize - 1
If iBuffArray(i) > n Then
n = iBuffArray(i)
buffer = i
End If
GetLargest = n

I noted that when I put a single negative number with a "-" and debugged, the algorithm 'cannot read' the value and therefore gives me a value of "0".

Any thoughts around this would be appreciated!!!



  • Malcolm_McLeanMalcolm_McLean Member Posts: 254 ✭✭
    What is happening is that your sort algorithm sets the largest element of the array to zero. This is fine for positive integers, but not when you include negatives.
    Unfortunately I'm not familiar with the language. In C you would solve this problem by setting to INT_MIN instead of 0. There will be an equivalent that is the lowest possible negative number representable by an integer. If there isn't just set to somethign very negative like -1000000
Sign In or Register to comment.