Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to sort negative and positive values ?
#3
Once you have a basic sorting operation, the trick is to realise you can make the "less than" operation mean whatever you want it to mean. Here's a standard sort routine (adapted from the SORTDEMO.bas in the previous response):
Code: (Select All)
Randomize Timer

Dim a(10)
For i = 0 To 10
    a(i) = Int(Rnd * 100) - 50
Next i

Print "Original array"
For i = 0 To 10
    Print a(i);
Next i
Print


QuickSort a(), LBound(a), UBound(a)

Print "Sorted array"
For i = 0 To 10
    Print a(i);
Next i


Function sortrule (a, b)
    sortrule = a < b
End Function


Sub QuickSort (arr() As Single, low, high)
    If low < high Then
        ' Only two elements in this subdivision; swap them if they are out of
        ' order, then end recursive calls:
        If high - low = 1 Then
            If sortrule(arr(high), arr(low)) Then Swap arr(low), arr(high)
        Else

            ' Pick a pivot element at random, then move it to the end:
            pivot = low / 2 + high / 2
            Swap arr(high), arr(pivot)
            partition = arr(high)
            Do

                ' Move in from both sides towards the pivot element:
                I = low: J = high
                Do While (I < J) And (arr(I) = partition Or sortrule(arr(I), partition))
                    I = I + 1
                Loop
                Do While (J > I) And (arr(J) = partition Or sortrule(partition, arr(J)))
                    J = J - 1
                Loop

                ' If we haven't reached the pivot element, it means that two
                ' elements on either side are out of order, so swap them:
                If I < J Then
                    Swap arr(I), arr(J)
                End If
            Loop While I < J

            ' Move the pivot element back to its proper place in the array:
            Swap arr(I), arr(high)

            ' Recursively call the QuickSort procedure (pass the smaller
            ' subdivision first to use less stack space):
            If (I - low) < (high - I) Then
                QuickSort arr(), low, I - 1
                QuickSort arr(), I + 1, high
            Else
                QuickSort arr(), I + 1, high
                QuickSort arr(), low, I - 1
            End If
        End If
    End If
End Sub

The function 'sortrule' defines what it means for a to be 'less than' b. So for your application, you need a sortrule that says negative numbers sort the oppose way to positive numbers:
Code: (Select All)
Function sortrule (a, b)
    If a < 0 And b < 0 Then sortrule = b < a Else sortrule = a < b
End Function

Replace that in the main code above and see it gives output something like this:
Code: (Select All)
Original array
-17 -24  4  37 -40  22 -43  32  41  2 -23
Sorted array
-17 -23 -24 -40 -43  2  4  22  32  37  41

I haven't checked if there's a noticeable speed penalty for using a helper function like this, but if there is you can easily replace all calls to it with the expression inline.
Reply


Messages In This Thread
RE: how to sort negative and positive values ? - by luke - 02-22-2026, 10:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on a Quick Sort routine Dimster 4 1,009 09-25-2023, 12:29 PM
Last Post: bplus
  Having trouble Windows command line SORT via SHELL GTC 19 4,367 08-26-2023, 04:19 AM
Last Post: GTC

Forum Jump:


Users browsing this thread: 1 Guest(s)