AutoHotkey Community

It is currently May 27th, 2012, 9:18 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Sort Question
PostPosted: December 11th, 2011, 6:52 pm 
Offline

Joined: June 9th, 2008, 2:32 am
Posts: 936
Location: Canada
it's been a while since i posted but here goes...

I've been experimenting with recursive programming and I came across a sorting method called "Quick Sort". It sorts lists with an n Log(n) complexity.
Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;Written By SpiderGames


;test := [46,23,99,55,1,2,3,4,5,6,55]
      
;sortedTest := QSort(test,1)
;msgbox % sortedTest.MaxIndex()
;Loop % sortedTest.MaxIndex()
;    MsgBox % sortedTest[A_Index]

QSort(toSort,Direction = 1)
{   
   if (Direction != 1)
      Direction = -1
   if (toSort.MaxIndex() < 2)
      return % toSort
   leftSide := Object()
   rightSide := Object()
   pivotPoint := toSort.Remove(1)      
   loop % toSort.MaxIndex()
   {
      if(toSort[A_Index]**Direction > pivotPoint**Direction)
         leftSide.Insert(toSort[A_Index])
      else
         rightSide.Insert(toSort[A_Index])
   }
leftSide := QSort(leftSide,Direction)
rightSide := QSort(rightSide,Direction)
leftSide.Insert(pivotPoint)
Loop % rightSide.MaxIndex()
    leftSide.Insert(rightSide[A_Index])
return % leftSide
}


However when i ran a test sample of 5000 randomly generated numbers the AHK build in function sort sorted it in 37ms while my QSort took 350ms at least i beat bubble sort :3 which had a time of 5200ms

My question is what is the complexity of "Sort".

also i believe the following block is slowing down my algorithm any suggestions on how to improve it?
Code:
leftSide := QSort(leftSide,Direction)
rightSide := QSort(rightSide,Direction)
leftSide.Insert(pivotPoint)
Loop % rightSide.MaxIndex()
    leftSide.Insert(rightSide[A_Index])
return % leftSide

_________________
Image
I know i have 6 legs. It's cuz I'm special.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 12th, 2011, 4:01 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Wb!

Did you take a look at Laszlo's QSort function? Whatever the native Sort command uses, it's clearly more efficient in general than Quicksort.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 12th, 2011, 4:14 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
either that or it being C++ instead of AHK makes a huge difference. Remember the "no free lunch"

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 12th, 2011, 4:16 am 
The Uberi's function is worth looking into.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 12th, 2011, 5:30 pm 
Offline

Joined: June 9th, 2008, 2:32 am
Posts: 936
Location: Canada
Thanks for the replys

This is Uberi's code
Code:
SortArray(InputArray)
{
 MaxIndex := ObjMaxIndex(InputArray), (MaxIndex = "") ? (MaxIndex := 0) : ""
 If (MaxIndex < 2)
  Return, InputArray
 Middle := MaxIndex >> 1
 SortLeft := ObjClone(InputArray), SortRight := ObjClone(InputArray)
 ObjRemove(SortLeft,Middle + 1,MaxIndex), ObjRemove(SortRight,1,Middle)
 SortArray(SortLeft), SortArray(SortRight), MaxRight := MaxIndex - Middle, LeftIndex := 1, RightIndex := 1
 Loop, %MaxIndex%
  InputArray[A_Index] := (LeftIndex <= Middle && (RightIndex > MaxRight || SortLeft[LeftIndex] < SortRight[RightIndex])) ? SortLeft[LeftIndex ++] : SortRight[RightIndex ++]
}


He's using the same method as me with a center and two sides which you recursively sort each side. His is approx 200ms faster in the 5000 test case. Its because of the way the Array class works. I think I will rewrite mine without using arrays.

_________________
Image
I know i have 6 legs. It's cuz I'm special.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Google Feedfetcher, migz99 and 71 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group