Question: Using Java, take the array recursion project and add the following recursive array methods to it. Add code to the main method to test each

Using Java, take the array recursion project and add the following recursive array methods to it. Add code to the main method to test each of these methods.

1. A method to find the smallest element in a certain part of the array. Your method should have an int parameter that sets how many numbers in the array to use to find the minimum value. For example, if n=5, the method would find the smallest value in the first 5 elements (indexes 0 to 4) in the array.

2. A method to rotate n elements in the array to the left. If n = 5, only the first 5 elements in the array rotate left.

3. A method to rotate n elements in the array to the right. If n = 5, only the first 5 elements in the array rotate right.

4. A method to determine if the first n elements in an array are already sorted. The method should return true if it is already sorted and false if it isn't already sorted. (This method will not actually do the sorting.)

Recursion Project:

import java.util.Scanner; class Recursion { private int a[]; public Recursion(int[] array) { //Constructor a = array; } public void printF(int n) { //Print Array Forward if(n > 0) { printF(n - 1); System.out.print(a[n - 1]); } } public void printB(int n) { //Print Array Backward if(n > 0) { System.out.print(a[n - 1]); printB(n - 1); } } public int maxA(int n) { //Largest Element of an Array if(n == 1) return a[0]; return Math.max(maxA(n-1), a[n-1]); } public void reverseA(int l, int r) { //Print Array in Reverse if(l < r){ int t = a[l]; a[l] = a[r]; a[r] = t; reverseA(l + 1, r - 1); } } int mode(int n) { //Most Appearing Integer in Array if (n == 1) return a[0]; int m = mode(n-1); int c1 = 0; int c2 = 1; for(int i = 0; i < n - 1; i++) { if(a[i] == m) c1++; else if(a[i] == a[n-1]) c2++; }if (c1 > c2) return m; return a[n-1]; } public void bubble(int n) { //Bubble Sort Array - Inferior if(n > 1){ for(int i = 0; i <= n-2; i++){ if(a[i] > a[i+1]){ int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } bubble(n-1); } } } public void bubbleV2(int n){ //Bubble Sort Array - Improved if(n > 1){ boolean isSwap = false; for(int i = 0; i <= n-2; i++){ if(a[i] > a[i+1]){ int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; isSwap = true; }if(isSwap) bubble(n-1); } } } public void insertion(int n) { //Insertion Sort Array if(n > 1){ insertion(n-1); int temp = a[n-1]; int i; for(i = n-2; i >= 0; i--) { if(a[i] > temp) a[i+1] = a[i]; else break; a[i+1] = temp; } } } public void selection(int n) { //Selection Sort Array if(n > 1) { int max = 0; for(int i = 1; i < n; i++) { if(a[i] > a[max]) max = i; } int temp = a[n-1]; a[n-1] = a[max]; a[max] = temp; selection(n-1); } } public int sequential(int n, int key){ if(n == 0) return -1; int temp = sequential(n-1,key); if(temp != -1) return temp; if(a[n-1] == key) return n-1; return -1; } public int binary(int l, int r, int key){ if(l > r) return -1; int m = (l+r)/2; if(key == a[m]) return m; if(key < a[m]) return binary(l, m-1, key); return binary(m+1, r, key); } } public class A1012a { public static void main(String[] args) { int a[] = {1, 2, 3, 4, 5, 6, 7}; int b[] = {1, 2, 3, 4, 5, 6, 7}; int c[] = {1, 2, 3, 4, 5, 6, 7}; int d[] = {1, 2, 3, 4, 5, 6, 7}; int e[] = {1, 2, 3, 4, 5, 6, 7}; int f[] = {1, 2, 3, 4, 5, 6, 2}; Recursion r1 = new Recursion(a); Recursion r2 = new Recursion(b); Recursion r3 = new Recursion(c); Recursion r4 = new Recursion(e); Recursion r5 = new Recursion(d); Recursion r6 = new Recursion(f); r1.printF(5); System.out.print(" "); r1.printB(5); r1.maxA(5); System.out.println(); r2.bubble(6); r2.printF(6); System.out.println(); r3.bubbleV2(6); r3.printF(6); System.out.println(); r4.insertion(6); r4.printF(6); System.out.println(); r5.selection(6); r5.printF(6); System.out.println(r6.sequential(6, 2)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!