class QuickSortLispLists { public static > LispList sort(LispList ls) // Returns a list containing the elements of ls in natural order // Sorts using quick sort onto an accumulator. { return sort(ls,LispList.empty()); } private static > LispList sort(LispList ls1,LispList ls2) // Returns a list containing the elements of ls1 in natural order joined to ls2. { if(ls1.isEmpty()) return ls2; T pivot = ls1.head(); ls1 = ls1.tail(); LispList smaller = LispList.empty(); LispList greater = LispList.empty(); for(; !ls1.isEmpty(); ls1=ls1.tail()) { T h = ls1.head(); if(h.compareTo(pivot)<0) smaller = smaller.cons(h); else greater = greater.cons(h); } greater = sort(greater,ls2); return sort(smaller,greater.cons(pivot)); } }