Jump to content

[Help] Quicksorting 2d Array by String Comparison Recusion Exceeded


Recommended Posts

Hey there,

I'm having some issues quick-sorting my 2d array imported from a database. Im trying to sort the array based on the name that would be returned in $array[$n][2]. The code i posted works for smaller arrays but for some reason when i try to sort my imported array (around 9000 indexes), I get "Recursion level has been exceeded". I understand that this is maybe due to lack of returns but i couldn't find an ideal spot to stick em and again it seems to work with smaller bits of code. Could it be that 9000 is too much?

I would normally just trial and error it until i figured it out but due to the length of time to load and buffer my array it's become too time consuming. Really I'm just hoping there is a quick fix that someone with more experience happens to know.

Thank you

 

;;---------------------------------------------This Works----------------------------------------------------

Local $a[7][2] = [ _
        ["1", "asdfashks"], _
        ["2", "SubStrlkghjing1"], _
        ["3", "jdfghjsergh"], _
        ["4", "nertynert"], _
        ["5", "cvbncvjkrt"], _
        ["6", "avbncvjkrt"], _
        ["7", "oytuoyuop"]]
Quicksort($a,1,0,6)
_ArrayDisplay($a)

;;------------------------------------------This Does Not---------------------------------------------------------

Quicksort($aLargeData, 1, 0, Ubound($aLargeData) - 1)
_ArrayDisplay($aLargeData)


;;----------------------------------------Quicksort Function------------------------------------------------------
Func Quicksort(ByRef $Array, $secondIndex, $First, $Last)
    Local $pivot, $i, $j, $temp
    If $First < $Last Then
        $pivot = $First
        $i = $First
        $j = $Last
        While ($i < $j)
            While (StringCompare($Array[$i][$secondIndex], $Array[$pivot][$secondIndex]) <= 0) And ($i < $Last)
                $i += 1
            WEnd
            While (StringCompare($Array[$j][$secondIndex], $Array[$pivot][$secondIndex]) > 0)
                $j -= 1
            WEnd
            If ($i < $j) Then _ArraySwap($Array, $i, $j)
            _ArraySwap($Array,$pivot,$j)
            Quicksort($Array, $secondIndex, $First,$j-1)
            Quicksort($Array, $secondIndex, $j+1,$Last)
        WEnd
    EndIf
EndFunc

 

Link to comment
Share on other sites

Link to comment
Share on other sites

Sideways question: can't you just use an order by clause when exporting data from the DB engine?

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

What happens if you use _ArraySort() instead of the QuickSort() function you posted? Using the same array, could you tell me if the following also produces a recursion error? - Thanks. (ArrayWorkshop.au3 can be found in my signature)

#include 'ArrayWorkshop.au3'

Local $aArray ; replace with your array

_ArraySortXD($aArray, Default, Default, Default, 0, 1)

 

Link to comment
Share on other sites

1 hour ago, czardas said:

What happens if you use _ArraySort() instead of the QuickSort() function you posted? Using the same array, could you tell me if the following also produces a recursion error? - Thanks. (ArrayWorkshop.au3 can be found in my signature)

#include 'ArrayWorkshop.au3'

Local $aArray ; replace with your array

_ArraySortXD($aArray, Default, Default, Default, 0, 1)

 

Yay it works!!!

Thank you so much. Would u mind if i used the functions from ur file?

Edited by gononono64
Link to comment
Share on other sites

Thanks for the report. We now know that there is a flaw in the logic in the QuickSort() function you posted (probably a comparison failure). Of course you can use my functions. If you need any help with them, let me know.

Did you test the standard _ArraySort() function?

Edited by czardas
Link to comment
Share on other sites

18 minutes ago, czardas said:

Thanks for the report. We now know that there is a flaw in the logic in the QuickSort() function you posted (probably a comparison failure). Of course you can use my functions. If you need any help with them, let me know.

Did you test the standard _ArraySort() function?

Yes it didnt suit my needs. Thank you and much appreciated :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...