Last active
May 26, 2018 22:33
-
-
Save thmain/b8c193c92ec204a3f1ad6cdeb40e7ca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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