Re: Sorting a List Solution Revisited was Sorting A List - Need Help
Re: Sorting a List Solution Revisited was Sorting A List - Need Help
- Subject: Re: Sorting a List Solution Revisited was Sorting A List - Need Help
- From: Rick Bargerhuff alias cougar <email@hidden>
- Date: Fri, 1 Aug 2003 01:54:27 -0400
On Thursday, Jul 31, 2003, at 21:27 US/Eastern, Nigel Garvey wrote:
Rick Bargerhuff alias cougar wrote on Wed, 31 Dec 1969 23:24:13 -0500:
Hello all,
Hello. Sorry for the late reply. Your message has taken 33 years and 7
months to get here.
Yeah, LOL. I reset logic board today, forgot to reset the date. :D
Gary thanks for the explanation. Can we use the speed techniques and
adapt it to my script?
Rick
Solutions to the original topic worked, but I learned nothing from the
solution. The current QuickSortOnItem implementation
works well but boggles my mind and I have no idea how it works.
Gary Lists wrote on Thu, 31 Jul 2003 19:48:41 -0400:
RE: QuickSort and variants:
I too find it almost impossible to follow...if only I knew what the
single
letter variables _were_ that would help me understand and use it more.
Just curious: Does anyone (Kai?) have a copy that has more
descriptive
variable names for QuickSort?
Why not ask the author or the person who posted it? :-) In its original
form, Arthur Knapp's QuickSort handler sorts a list of items "in
place".
It doesn't return a sorted copy of the list, it sorts the actual list
that's passed to it. It takes three parameters: the list and the
indices
of the first and last items in the section of list you want sorted.
Internally, it uses the "Serge" list referencing technique for speed.
(Let me know if you it posted.)
My modification of it sorts a list of *lists*, sorting on the
same-numbered item in each sublist. It thus needs a fourth parameter
which is the number of this item.
(* QuickSort handler adapted from Arthur Knapp *)
on QuickSortOnItem(theList, leftLimit, rightLimit, sublistItem)
-- Quicksorts a list of lists, sorting on item sublistItem of each
list
local leftIndex, rightIndex, comparisonValue
-- Much thanks to both Serge Belleudy-d'Espinose and Victor Yee
-- for the script-referencing techniques that they helped to
-- refine.
--
script theScriptObject
property listProperty : theList
end script
set leftIndex to leftLimit
set rightIndex to rightLimit
set comparisonValue to theScriptObject's listProperty's item
((leftLimit + rightLimit) div 2)'s item sublistItem
repeat while (rightIndex > leftIndex)
repeat while (theScriptObject's listProperty's item leftIndex's
item sublistItem < comparisonValue)
set leftIndex to leftIndex + 1
end repeat
repeat while (theScriptObject's listProperty's item rightIndex's
item sublistItem > comparisonValue)
set rightIndex to rightIndex - 1
end repeat
if rightIndex leftIndex then
tell theScriptObject's listProperty's item leftIndex
set theScriptObject's listProperty's item leftIndex to
theScriptObject's listProperty's item rightIndex
set theScriptObject's listProperty's item rightIndex to it
end tell
set leftIndex to leftIndex + 1
set rightIndex to rightIndex - 1
end if
end repeat
if (leftLimit < rightIndex) then QuickSortOnItem(theScriptObject's
listProperty, leftLimit, rightIndex, sublistItem)
if (rightLimit > leftIndex) then QuickSortOnItem(theScriptObject's
listProperty, leftIndex, rightLimit, sublistItem)
end QuickSortOnItem
set ListOfLists to {{1, 2, 3, 4, 5}, {23, 75, 1, 0, 7}, {-3, 56, -8,
5,
5}}
-- Sort *all three* sublists on their *third items* - ie. 3, 1, and
-8
QuickSortOnItem(ListOfLists, 1, count ListOfLists, 3)
ListOfLists
-- {{-3, 56, -8, 5, 5}, {23, 75, 1, 0, 7}, {1, 2, 3, 4, 5}}
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.