April 2017
Beginner
504 pages
14h 11m
English
To demonstrate that even non-tail recursive methods can be expressed in a non-recursive way, here is the quick sort that way:
public class NonRecursiveQuickSort<E> { // injected final fields and constructor deleted from print private static class Stack { final int begin; final int fin; public Stack(int begin, int fin) { this.begin = begin; this.fin = fin; } } public void qsort(SortableCollection<E> sortable, int start, int end) { final List<Stack> stack = new LinkedList<>(); final Partitioner<E> partitioner = new Partitioner<>(comparator, swapper); stack.add(new Stack(start, end)); int i = 1; while (!stack.isEmpty()) { Stack iter = stack.remove(0); if (iter.begin < iter.fin) { final E pivot = sortable.get(iter.begin); ...Read now
Unlock full access