Announcement

Collapse
No announcement yet.

Need some sorting advice

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need some sorting advice

    Hey Guys,

    I have a big problem here. I need a sorting algorithm that is VERY quick and can keep track of a particular row. I am using a quick sort algorithm now but it doesn't work right. when it comes accross two values that are the same on alternate calls it flips the two around.

    Here's my example
    Code:
    #compile exe
    #include "win32api.inc"
     
     
    type CRecord
        lDepth  as long
        lData   as long
    end type
     
     
     
    function SortRecords(Record() as CRecord,byval lRow as long,byval l as long,byval r as long) as long
        dim lDepth as long
        dim i as long
        dim j as long
     
     
        lDepth = Record((l+r)\2).lDepth
        i = l
        j = r
     
     
        while (i <= j)
            while (Record(i).lDepth < lDepth) and (i < r):incr i:wend
            while (Record(j).lDepth > lDepth) and (j > l):decr j:wend
     
            if (i <= j) then
                'Track row number
                if (lRow = i) then
                    lRow = j
                elseif (lRow = j) then
                    lRow = i
                end if
                 
                'Swap records
                swap Record(i),Record(j)
                incr i
                decr j
            end if
        wend
     
     
        if (l < j) then lRow = SortRecords(Record(),lRow,l,j)
        if (i < r) then lRow = SortRecords(Record(),lRow,i,r)
        
        'Return row number
        function = lRow
    end function
     
     
    function pbmain as long
        dim Record(10) as CRecord
        dim strMsg as string
        dim lResult as long
        dim lIndex as long
        dim lPass as long
        dim lRow as long
         
         
        randomize 1'Make sure we all see the same "random" numbers
        
        'Generate random data
        for lIndex = 0 to 10
            Record(lIndex).lDepth   = rnd(0,5)
            Record(lIndex).lData    = lIndex
        next
        
        
        lPass   = 0'First pass means we didn't sort yet
        lRow    = 2'Select row 2
        
            
        do
            'Build dialog message
            strMsg = format$(lRow,"Row = #")
            strMsg = (strMsg & format$(Record(lRow).lDepth," Depth = #"))
            strMsg = (strMsg & $CRLF & $CRLF)
            
            for lIndex = 0 to 10
                strMsg = (strMsg & str$(Record(lIndex).lDepth) & str$(Record(lIndex).lData) & $CRLF)
            next
            
            'Display results and check for cancel
            lResult = msgbox(strMsg,%MB_OKCANCEL,format$(lPass,"Pass #"))
            if (lResult = %IDCANCEL) then exit loop
     
            'Sort records
            lRow = SortRecords(Record(),lRow,0,10)
            
            incr lPass
        loop
    end function

    On the first pass (pass = 0) all the records show up unsorted. On every pass after that they are sorted but every other sorting pass causes depth numbers that are the same to trade spaces. In other words, I dont want that!


    How can I prevent depth values that are the same from swapping with each other? It's probably very simple but I havn't a clue were to begin looking.


    Thanks for any help guys!

    ------------------
    Cheers!

    [This message has been edited by mark smit (edited June 01, 2001).]

  • #2
    I think I've got it...
    Code:
    function SortRecords(Record() as CRecord,byval lRow as long,byval l as long,byval r as long) as long
        dim lDepth as long
        dim i as long
        dim j as long
        
        
        lDepth = Record((l+r)\2).lDepth
        i = l
        j = r
        
        
        while (i <= j)
            while (Record(i).lDepth < lDepth) and (i < r):incr i:wend
            while (Record(j).lDepth > lDepth) and (j > l):decr j:wend
            
            if (i <= j) then
                if (Record(i).lDepth <> Record(j).lDepth) then '**** ADDED THIS ****
                    'Track row number
                    if (lRow = i) then
                        lRow = j
                    elseif (lRow = j) then
                        lRow = i
                    end if
                    
                    'Swap records
                    swap Record(i),Record(j)
                end if                                         '**** AND THIS ****  
                        
                incr i    '**** MOVED THESE HERE
                decr j    '
            end if
        wend
        
        
        if (l < j) then lRow = SortRecords(Record(),lRow,l,j)
        if (i < r) then lRow = SortRecords(Record(),lRow,i,r)
        
        'Return row number
        function = lRow
    end function
    ------------------
    Cheers!

    Comment

    Working...
    X