How to reverse a Java List: recursion, Multithreading, Permutations and parallel hardware

One of the annoying tests clients are increasingly using to screen candidates asked for ways to reverse a list. Since I have been trying to learn to think like an architect this gave me an excuse to play with different ways of reversing a list because the first way of doing something is rarely the best[1]. A quick Google search revealed some interest in this problem, so it may be useful to others. I used Java and the code should work with anything that implements the List interface, and some will work with any Collection, but the techniques used should work in any language, and may well be more concise and perhaps more efficient in (say) Python.

The coding itself is trivial, but the ideas behind the code get more complex as the article progresses. The progression is from simple reversal to threading to permutations and finally the role of massively parallel hardware is discussed.

The code samples here have not been optimised for performance, and have deliberately not been made generic. They assume a basic knowledge of Java and the ability to understand Java code.

Recursive and non recursive reversal

These two methods are simple. For recursion swap the two ends of the list, adjust the position of the ends and repeat on the remainder of the list. Stop when the two ends coincide or pass each other. This is an in-place reversal and needs minimal memory. The code should be self explanatory. Some micro optimisation may be possible at the cost of readability. The non recursive reversal is almost identical to the recursive version. Both take about the same time to run. The non recursive version is a little more intuitive.

Non recursive reversal

	public static void reverseList( List<String> theList, int start, int end)
	{
    		int startpoint = start;
    		int endpoint = end;
        	while(startpoint<endpoint)
        		{
         		String startString = theList.get(startpoint);
         		String endString  = theList.get(endpoint);
      	 		theList.set(startpoint,endString);
      	 		theList.set(endpoint, startString); 
      	 		startpoint++;
      	 		endpoint--;
        		}	
	}

Recursive reversal

public static void recursivereverse( List<String> theList, int start, int end)
	{
        if(start>=end) return;
        String startpoint = theList.get(start);
        String endpoint = theList.get(end);
        
        theList.set(start,endpoint);
        theList.set(end, startpoint);
        
        start++;
        end--;

        recursivereverseCollection(theList, start, end);    
	}

Stack Based Reversal

This is not an in-place reversal so it takes more memory. It was a lot less efficient and the execution time was very variable. This may mean that the time taken was long enough that the probability of a garbage collection sweep became high. It is elegant but not efficient.

public static Collection<String> reverseByStack( Collection<String> theList)
		{
		   	Stack<String>theStack = new Stack<String>();
			Collection<String> reversed = new ArrayList<String>();
     
			for(String str : theList) { theStack.push(str); }			 
			while(!theStack.isEmpty()) { reversed.add(theStack.pop()); }
	
			return reversed;
		}

Producer and consumer threads

The List may be read into a Queue, starting from the end and the contents of the Queue processed by another thread. This is an example of the Producer-consumer pattern. I did a similar thing in Python to find cycles in the behaviour of an automaton.

The Consumer and producerThread classes have identical fields and identical constructors that take a Queue and a List. I omitted the constructors and accessors for clarity

The producer thread implements Runnable and a synchronised run() method. The constructor takes a Queue. The run method just populates the queue in reverse order.

The consumer thread populates its collection with the list stored in the Queue. The run method is

Producer Thread code

public class ProducerThread implements Runnable
{	
	private Queue<String> itsqueue;
	private List<String> itscollection;
	
// Add the collection to the queue in reverse order
	@Override
	public synchronized void run()
	{	
	  for( int index = itscollection.size()-1; index>=0; index--)
	  	{
		  itsqueue.add(itscollection.get(index)); 
	  	}
	}
}

Run method of consumer thread

	@Override
	public synchronized void run()
	{
	 itscollection.clear();
	  while(!itsqueue.isEmpty())
	  {
		  itscollection.add(itsqueue.remove()); 
	  }	  
	}

permutation based reversals

The next step is to treat reversal as a permutation, which it is. This needed a way to apply a given permutation to the list and led to a consideration of Parallel Data transforms which I will discuss later.

To apply a permutation simply specify where each element of the list is to go. This cannot be done in place and trades efficiency for flexibility.

List permutation code

/**
 * Performs the specified permutation on the given list 
 * List and permutation must be same size and not null
 * @param theList
 * @param permutation (of {0....theList.size()-1} )
 * @return permuted list
 */
	public static List<String> permute(List<String> theList,List<Integer> permutation)
	{
		
		List<String> result = new ArrayList<String>(permutation.size());
		for(int ii= 0; ii< permutation.size(); ii++)
		{
			result.add(null);
		}
		String value=null;
    for( int i=0; i < permutation.size(); i++ )
    {
    	value = theList.get(i);
    	int destination = permutation.get(i).intValue();
    	result.set(destination, value);    	  	
    }
    
    return result;
	}

Shuffle based reversals

One useful permutation is a shuffle. As a concrete example consider a set of 8 elements

{0,1,2,3,4,5,6,7}

There are two ways to interleave the elements as in a perfect card shuffle

{0,4,1,5,2,6,3,7}

and

{4,0,5,1,6,2,7,3}

The respective permutations are {0,2,4,6,1,3,5,7} and {1,3,5,7,0,2,4,6};

The second shuffle is of interest here since all elements change position. The important point is that after three applications of this permutation to the original set the order of the elements is reversed. Which brings us to parallel data transform

Parallel Data Transforms

Parallel Data transforms were originally developed for the ICL Distributed Array Processor, a 64 by 64 grid of single bit processors with nearest neighbour connections which functioned as a Single Instruction Multiple Data computer. They were originally devised by Stuart Reddaway who deserves great respect not only for devising them but retaining his sanity while doing so. Reddaway also managed to find efficient ways to implement the shuffles above.

The idea behind parallel data transforms is simple. The indices in an array of 2N elements may be represented as N-bit unsigned integers. A useful subgroup of the permutations of the elements is the permutation group obtained by performing a common operation on the bits of each of the indices. For example the first shuffle above is obtained by a cyclic shift of the bits, and the second by flipping the most significant bit and following this with a cyclic shift of the bits. Reversal simply means flipping all bits of each index. The operations on the N bits of the indices yield N! Permutations without bit flipping and when bit flipping is allowed the total number of permutations rises to N!log2N.

Now it is obvious that if we keep flipping the most significant bit of an N-digit number and cyclically shifting the result then N repetitions are the same as a bitwise NOT and the net result is a reversal. On a serial machine this will take N Log2N operations but if there is an efficient hardware unit available to carry out the shuffles in negligible time, for a fixed array of 2N elements then reversal will take Log2N parallel operations. Of course there is limited demand for reversal but these shuffles have been used as the basis for parallel sorting algorithms and, as I recall, for parallel Fast Fourier Transform algorithms.

And for now this is about as far as I want to take List reversal though it would be fun to look again at more permutations using Parallel Data Transforms and in particular multidimensional versions.

The important point to note, especially for Architects, is that use of specialised hardware could radically change the characteristics of algorithms which in turn will affect the design of the system.

Summing up

Various methods of reversing a List in Java have been given. They sidestep problems with reversing Linked Lists and start with simple reversal, which is o(N) and proceed to more elegant methods. On a standard serial architecture the complexity of the algorithms varies from o(N) to o( Nlog2N). By creating dedicated hardware to perform shuffles the complexity can be reduced to log2N. The take home points for architects are that they need to experiment with different ways of carrying out a task “Is there another way”[1] and that dedicated hardware may radically alter the complexity of a task. Dedicated hardware can also reduce total lifecycle cost by eliminating maintenance for the tasks carried out by the hardware, once the hardware has been thoroughly debugged.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working