Java, recursively inverse array

I did not find anything with the specific needs of my function to do this, yes, this is for homework.

So, I have a:

public void reverseArray(int[] x) { } 

Condition: x.length> 0

The fact that I cannot have a function returns something, and the only argument is the array, which leaves me at a standstill.

I tried using loops along with recursion, but everything I tried seems to end up with endless instances of the function being executed.

I had an idea / suggestion to use another function along with this, but how to use the original recursively, you are outside of me at the moment.

Any help is appreciated.

+12
java arrays reverse void
Oct 31
source share
13 answers

If I encoded this, I would create a temporary array (possibly with deleting one element?) To recursively call and copy the elements back to the original array before returning from the function. You will also need to find the base register to complete the recursion.

+8
Oct. 31
source share
 void reverseArray(int[] x){ reverse(x, 0, x.length -1); } void reverse(int[] x, int i, int j){ if(i<j){//Swap int tmp = x[i]; x[i] = x[j]; x[j] = tmp; reverse(x, ++i, --j);//Recursive } } 

Test:

 int[] s = new int[]{1,2,3,4,5}; reverseArray(s); System.out.println(Arrays.toString(s));//"5,4,3,2,1" 

Recursive, O (n), no temporary array required.

+10
Oct 31 :
source share

Since this is your homework, I offer an example:

This sequence: 1 2 3 4 5 6 7 8 9 10

You can change to: 10 2 3 4 5 6 7 8 9 1

After that: 10 9 3 4 5 6 7 8 2 1

.....

As you can see, step by step, the sequence is "better" and the problem is "less." So the problem you have to solve is this:

1) How to apply a recursive call for this method. for the original method: reverse(int[] a) . therefore, after the first step, you should create an array b from a[2] --> a[n-1] . and using reverse (int [] b) `.

2) after reverse b , what should we do to reverse? Assign b values ​​to again for.

3) stop condition: what is the stop condition? You see that the elements of array b are smaller than the elements of array a. So what step should we stop?

Hope this help :)

+6
Oct 31 '12 at 2:15
source share

Try something as shown below:

 public void reverseArray(int[] x) { if(x.length ==2){ //if two elements, swap them int first = x[0]; x[0] = x[1]; x[1] = first; }else if(x.length > 2){ //swap first and last int first = x[0]; x[0]= x[x.length-1]; x[x.length-1] = first; //create a copy of middle elements int [] copy = new int[x.length-2]; System.arraycopy( x, 1, copy, 0, x.length-2); //recursive call for middle elements reverseArray(copy); //place the reversed elements back in the original array System.arraycopy( copy, 0, x, 1, copy.length); } } 
+1
Oct. 31 :
source share
 public class RecursiveArray { public static int[] backWardArray(int[] arr, int start, int end) { if (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; backWardArray(arr, start + 1, end - 1); } return arr; } public static void main(String[] args) { int [] arr = {12,4,6,8,9,2,1,0}; int [] reversedArray= backWardArray(arr, 0, arr.length-1); //loop through the reversed array for (int i: reversedArray) { System.out.println(i); } } public RecursiveArray() { } } 
+1
Nov 19 '17 at 22:02
source share

Call reverseArray (0, n, arr) here n is the length of the array

 public void reverseArray(int i, int n, int [] arr) { if(i==n) { return ; } else { reverseArray(i+1, n, arr); System.out.println(arr.at(i)); } } 
+1
Dec 27 '17 at 12:52
source share

// We just do the operation here and call the helper method.

 public void reverseArray(int[] nums){ int[] hold = new int[nums.length]; //just so it will take this argument int[] reversed = recurReverseArray(nums, hold, nums.length - 1, 0); nums = reversed; //not returning just changing nums to be reversed. } public int[] recurReverseArray(int[] nums, int[] reverse, int end, int start){ if(end == 0 && start == nums.length - 1){ reverse[start] = nums[end]; return reverse; //the way out. } reverse[start] = nums[end]; return recurReverseArray(nums, reverse, end - 1, start + 1); } 
0
Apr 12 '15 at 17:47
source share

Here is the main way:

 package main; public class Main { public static void main(String[] args) { StringOps ops = new StringOps(); String string = "Arjun"; // reversing the string recrusively System.out.println(ops.reverseRecursively(string.toCharArray(), 0)); } } 

and here is the recursive function:

 package main; public class StringOps { public char[] reverseRecursively(char[] array, int i) { char[] empty = new char[0]; if (array.length < 1) { System.out.println("you entered empty string"); return empty; } char temp; temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; i++; if (i >= array.length - 1 - i) { return array; } else { reverseRecursively(array, i); return array; } } } 
0
Nov 26 '15 at 6:49
source share
 public class FunWithAlgorthims { public static void main(final String[] args) { String[] array = {"a", "b", "c", "d"}; printArray(array); revereArrayRecusrive(array, 0); printArray(array); } public static void revereArrayRecusrive(final String[] array, int startPointer) { if (startPointer >= (array.length / 2)) { return; } String temp = array[startPointer]; array[startPointer] = array[array.length - 1 - startPointer]; array[array.length - 1 - startPointer] = temp; revereArrayRecusrive(array, ++startPointer); } public static void printArray(final String[] array) { Arrays.stream(array).forEach(a -> System.out.print(a + " ")); System.out.println(); } 

}

0
Oct 13 '16 at 21:18
source share

This is probably the easiest way, not the fastest, but perhaps the easiest.

The whole program will look something like this:

 public static void main(String [] args) { BackwardsArray back = new BackwardsArray(); } public BackwardsArray() { int [] a = {1,2,3,4,5,6,7,8,9}; printBackwards(a); } void printBackwards( int [] b) { print(b,b.length-1); } void print(int [] b, int pos) { System.out.println(b[pos]); // prints last item if(pos != 0) { print(b,pos-1); } } 

Hope this helps!

0
Sep 26 '17 at 22:32
source share
 private static void reversePrint(int[] numbers) { if(numbers.length==0) { return; } int[] a = new int[numbers.length -1]; for(int i =0;i<numbers.length-1;i++) { a[i] = numbers[i+1]; } reversePrint(a); System.out.println(numbers[0]+" "); } 
0
Aug 30 '19 at 13:25
source share

psuedo code

 function revarray(a[1...n]) if a.length == 1 or a.length == 0 do nothing # return a else swap a[1] and a[n] revarray(a[2...n-1]) # return a (The function will not return anything but the contents of a are changed) 
-one
Oct 31
source share

Since there was no indication that loops could not be used:

 void reverseArray(int[] x) { if (x != null) { for (int i = 0; i < length.x / 2; i++) { int j = (length.x - 1) - i; int temp = x[i]; x[i] = x[j]; x[j] = temp; } reverseArray(null); } } 

Probably the fastest of the lot.

-one
Oct 31 '12 at 2:25
source share



All Articles