Trouble getting data from library handler (or something...)

hmmmm…I’m hoping this is one of those “stupid” questions I’m so famous for. But I’m working up a set of scripts that I can reuse as library scripts. On of the libraries has sorting algorithms. The following script works fine if you just run it:

set newList to {}
set theList to {6, 5, 4, 3, 2, 1}
set newList to bubbleSort(theList)
return newList

on bubbleSort(theList)
	script bs
		property alist : theList
	end script
	set theCount to length of bs's alist
	if theCount < 2 then return bs's alist
	repeat with indexA from theCount to 1 by -1
		repeat with indexB from 1 to indexA - 1
			if item indexB of bs's alist > item (indexB + 1) of bs's alist then
				set temp to item indexB of bs's alist
				set item indexB of bs's alist to item (indexB + 1) of bs's alist
				set item (indexB + 1) of bs's alist to temp
			end if
		end repeat
	end repeat
	return bs's alist
end bubbleSort

However, when I try to call it from another program, I get WEIRDNESS (sound of B Sci-FI movie weird music, “oooo-EEEEEE-oooo”). Here’s the calling program:

property myList : {}
property newList : {}
--load the library
set scriptPath to (path to scripts folder from user domain) & "Script Library:"
set sortLib to load script ((scriptPath as text) & "sortLib.scpt") as alias
--choose a sort
set sortList to {"Bubble", "Bubble Swap", "Insertion", "Merge", "Quick"}
set sortName to choose from list sortList without multiple selections allowed and empty selection allowed
--ask for list length
display dialog "Sort how many items?" default answer "1000"
set maxItems to (text returned of the result) as number
--ask for data distribution
display dialog "Random or partially ordered items?" buttons {"Cancel", "Partially Ordered", "Random"} default button "Random"
if the button returned of the result is "Random" then
	repeat with myNum from 1 to maxItems
		set myList to myList & (random number from 1 to maxItems with seed myNum)
	end repeat
else
	beep
	set partitionValue to (maxItems div 2)
	repeat with myNum from 1 to maxItems
		if myNum < partitionValue then
			set myList to myList & (random number from 1 to maxItems with seed myNum)
		else
			set myList to myList & (random number from partitionValue to maxItems with seed myNum)
		end if
	end repeat
end if

set beginTime to current date --save the starting time

if sortName is "Bubble" then --pick the sort and do it
	set newList to sortLib's bubbleSort(myList)
else if sortName is "Bubble Swap" then
	set newList to sortLib's bubbleSwapSort(myList)
else if sortName is "Insertion" then
	set newList to sortLib's insertSort(myList)
else if sortName is "Merge" then
	set newList to sortLib's mergeSort(myList)
else
	set newList to sortLib's quickSort(myList)
end if

set endTime to current date --get the end time

--report the results
display dialog (maxItems as text) & " items sorted in " & (endTime - beginTime) & " seconds using " & sortName & " sort."

return newList

If I say I want to bubble sort 10 random items, I get back this ridiculous mess:

{6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 6, 4, 4, 5, 7, 7, 7, 9, 8, 10, 9, 10, 4, 5, 7, 7, 7, 4, 6, 8, 9, 10, 4, 5, 7, 7, 7, 4, 6, 8, 9, 10, 4, 5, 7, 7, 7, 4, 6, 8}

My other library files and their associated calling scripts don’t seem to have this problem. In fact, if I use another calling program and just give it a static list that I hardcode in myself, like this:

--load the library
set scriptPath to (path to scripts folder from user domain) & "Script Library:"
set sortLib to load script ((scriptPath as text) & "sortLib.scpt") as alias

set newList to {}
set theList to {6, 5, 4, 3, 2, 1}
set newList to sortLib's bubbleSwapSort(theList)
return newList

I get the proper result. I’m at my wit’s end, I’ve tried copying the large calling program’s code to another new file, thinking it was polluted somehow. I’ve checked the unsorted file passed to the library handler, and it’s just a list of unsorted digits (10 of them if I ask for 10 of them)

Am I missing something here?

Hi, Kevin.

In your calling script, ‘myList’ is a property, so you need to reinitialise it to {} on each run, otherwise the list just gets longer every time.

‘sortname’ is set to a list ” unless it’s ‘false’ ” so the choice of sorts always defaults to quicksort in the last ‘if’ block. (‘sortname’ is a list, not one of the strings with which it’s compared.)

When I correct for ‘sortname’, the list gets longer, but is sorted in order. Maybe the “mess” is something to do with your quicksort handler?

That’a the sort of thing I suspected but couldn’t find. Thanks, Nigel!

That explains a lot. When I call the bubble sort by name (without relying on the sortName) it works fine. I forgot that choose from list returns a list answer even if you only select one item. I’ll fix it.

Yeah, quicksort doesn’t work (yet). I got stuck tracking down the other bugs and lost track of qsort.

Thanks a lot, Nigel. Sometimes it helps to have another (trained) set of eyes on your code. They will often see what you have missed because you are too close to the code and know what it “should” do. :slight_smile:

Kevin;

I’m wondering if you have seen the second item in this Code Exchange topic on sorting. It’s a script by Nigel Garvey and Arthur Knapp that beats any I’ve seen for a hybrid QuickSort/replacement sort (switching from one to the other near the end of the sort).

Adam,
Yes, as a matter of fact, I have seen it. Just as an exercise, I’m writing the various sorts myself. However, once I get my version working, I’ll re-look at Nigel’s and see if I can make some improvements to mine. I’m also running comparisons of the various times for each sort, to see if my versions translate correctly into the sort of times one would expect from bubble sort (n-squared), merge sort (logn*n), etc. OK, I’m a geek, what can I say…:wink:

Don’t apologize. It’s a noble profession. :lol:

And, BTW, I’m a strong adherent to the cult of “night person” myself.