All pastes #937629 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-03-11 02:21 UTC
rendered paste body
# My J sort implementation in Io

jsort := method(to_sort, strand_threshold,
	if(strand_theshold >= 40) then (
		writeln("The strand threshold is larger than recommended.")
	}
	
	if(to_sort size < strand_threshold) then(
		use := 0
	) else(
		use := 1
	)

	ret_list := List clone

# Strand sort
	if(use == 1) then(
# Pleasentries...
		if(to_sort size == 0) then (
			return ret_list
		) elseif (to_sort size == 1) then (
			ret_list = to_sort at(0)
			return ret_list
		)
		elseif(to_sort size == 2) then (
			if(to_sort at(0) > to_sort at(1)) then (
				ret_list = list(to_sort at(1), to_sort at(0))
			) else (
				ret_list = list(to_sort at(0), to_sort at(1))
			)
			return ret_list
		)
		
		sub_list := List clone
		while(to_sort isNotEmpty,
			sub_list empty
			sub_list append(to_sort at(0))
			to_sort removeAt(0)
			to_sort foreach(i,
				if(to_sort at(i) > sub_list at(to_sort size - 1)) then (
					sub_list append(to_sort at(i))
					sub_list removeAt(i)
				)
			)
# Merge the lists (isn't this slow?)
			while(sub_list isNotEmpty,
				ret_list foreach(i,
					if(sub_list at(0) > ret_list at(i) and 
					sub_list at(0) < ret_list at(i + 1)) then (
						ret_list atInsert(i + 1, sub_list at(0))
						break
						)
					)
				)
			)
			return ret_list
		)
# Shuffle sort
	)
)

list := list(5, 2, 7, 2, 8, 9, 23, 7, 3, 2, 54, 93, 11, 84, 33, 193, 11, 3, 44)
jsort(list, 39)
list println