Question

Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recur

can someone please help me with this. I need to use java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Coding

import java.util.Arrays;
import java.util.Scanner;
public class rec
{
    static void insertionSortRecursive(int arr[], int n)   // Recursive function to sort an array using
      {
        // Base case
        if (n <= 1)
            return;
     
        insertionSortRecursive( arr, n-1 );//decrease length
     
        // Insert last element at its correct position
        // in sorted array.
        int last = arr[n-1];
        int j = n-2;
     
        /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        while (j >= 0 && arr[j] > last)
        {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = last;
    }
    
    // Driver Method
    public static void main(String[] args)
    {
       int i;
        Scanner scan = new Scanner(System.in);
        System.out.println("Please Enter Number Here :");
       int num = scan.nextInt();
       int arr[]=new int[num];
       for(i=0;i<num;i++)
       {
       System.out.print("Please Enter Value for arr["+ String.valueOf(i)+"]::");
       arr[i] = scan.nextInt();
       }
       System.out.println("Array before Sorting");
       System.out.println(Arrays.toString(arr));
       insertionSortRecursive(arr, arr.length);
        System.out.println("Array After Sorting");
       System.out.println(Arrays.toString(arr));
    }
}

output:

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........

Add a comment
Know the answer?
Add Answer to:
can someone please help me with this. I need to use java. Recursion Write and run...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT