Recurssion (Java Algorithms)

Link to exercises I am dealing with:

Array: https://www.codecademy.com/courses/java-for-programmers/articles/arrays-java-for-programming

Recursion: https://www.codecademy.com/courses/java-algorithms/lessons/recursion-java/exercises/introduction

Hi everyone I am wondering if there is a more efficient way to write this DisplayArray.java file I have been doing for
the recursion lesson? I experimented from the codecademy lesson below. Any feedback is gladly appreciated :slight_smile:

DisplayArray.java

public class DisplayArray {
	// Mem Variables 
	private int array[]= {25,96,87,41};
	private LinkedStack<Integer> stack = new LinkedStack<Integer>();

	public static void main(String[] args) {

		   DisplayArray dArray = new DisplayArray();
		   dArray.createStack(dArray.array);
		   System.out.print("displayArrayRecursively(): ");
		   dArray.displayArrayRecursively(0,dArray.array.length-1);
		   System.out.println();
		   dArray.displayArrayWithStack(0,dArray.array.length-1);
		}

	private void displayArrayWithStack(int i, int j) {
		System.out.print("displayArrayWithStack(): {");
		while (i < j) {
			System.out.print(stack.pop() + ",");
			i++;
		}
		System.out.print(stack.pop() + "}");
		
	}

	private void displayArrayRecursively(int i, int j) {
		if (i <= j) {
			System.out.print(array[i]+" ");
			displayArrayRecursively(i+1, j);
		}
		
	}

	private void createStack(int[] arr) { // creates a stack that will display in the original order
		for (int i = (array.length - 1); i >= 0; i--) {
			stack.push(arr[i]);
		}
	}

		

	
}

Some other files if needed:

LinkedStack.java below:

/* 
 * 
 * LinkedStack
 * 
 */


import java.util.Arrays;
//import java.util.EmptyStackException;
import java.util.EmptyStackException;

public final class LinkedStack<T> implements StackInterface<T>
{
	//public class Node<T>
	private Node topNode; // References the first node in the chain
	
	//default constructor
	public LinkedStack() {
		topNode = null; 
	}
	
	// Implement the unimplemented methods 
	private class Node{
		T data;
		Node next;
		
		public Node() {
			next = null;
		}
	}// end of node 
	
	public void push(T newEntry) {
		if(topNode == null) {
			topNode = new Node();
			topNode.data = newEntry;
			
		} else {
			Node nNode = new Node();
			nNode.data = newEntry; 
			nNode.next = topNode;
			topNode = nNode;
		}
	}
	public T pop() {
		if(topNode == null) {
			throw new EmptyStackException(); // I removed the strings 
		} else {
			Node node = topNode;
			topNode = topNode.next;
			return node.data;
		}
	}
	
	public T peek() {
		if(topNode == null) {
			throw new EmptyStackException(); // I removed the strings
			
		} else {
			return topNode.data;
		}
	}
	public boolean isEmpty() {
		return topNode == null;
		
	}
	
	public void clear() {
		topNode = null;
	}	
}// end of LinkedStack 

StackInterface.java below:

/**
   An interface for the ADT stack.
*/
public interface StackInterface<T>
{
   /** Adds a new entry to the top of this stack.
       @param newEntry  An object to be added to the stack. */
   public void push(T newEntry);
  
   /** Removes and returns this stack's top entry.
       @return  The object at the top of the stack. 
       @throws  EmptyStackException if the stack is empty before the operation. */
   public T pop();
  
   /** Retrieves this stack's top entry.
       @return  The object at the top of the stack.
       @throws  EmptyStackException if the stack is empty. */
   public T peek();
  
   /** Detects whether this stack is empty.
       @return  True if the stack is empty. */
   public boolean isEmpty();
  
   /** Removes all entries from this stack. */
   public void clear();
} // end StackInterface

What do you mean by efficient?

  • Time complexity while using recursion? Without recursion?
  • More readable?

The recursive display is about is efficient as you’ll get for recursion. You would probably never want to display an array like this in a practical situation when you can iterate (even though they’re both linear in this case). The reason is because of the overhead of the recursive calls is more expensive behind-the-scenes.