Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 26, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/b8c193c92ec204a3f1ad6cdeb40e7ca7 to your computer and use it in GitHub Desktop.
Save thmain/b8c193c92ec204a3f1ad6cdeb40e7ca7 to your computer and use it in GitHub Desktop.
import java.util.*;
public class ReverseArray {
static int [] a;
public static void reverseIteration(){
int start =0;
int end = a.length-1;
while(start<=end){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
}
public static void reverseRecursive(int start, int end){
if(start<=end){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
reverseRecursive(start, end);
}
}
public static void main(String[] args) {
a = new int []{1,2,3,4,5};
System.out.println("Original Array" + Arrays.toString(a));
reverseIteration();
System.out.println("Reversed - Array(Iteration):" + Arrays.toString(a));
reverseRecursive(0,a.length-1);
System.out.println("Reversed Again - Array(Recursion):" + Arrays.toString(a));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment