Question

In: Computer Science

Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...

Java Programm please!

Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.

Solutions

Expert Solution

Java program to sort an array using recursion and backtracking

Code:

import java.util.Scanner;
public class Main
{
    //sortArray recursive method to sort array using recursion and bactracking
    public static void sortArray(int array[], int n)
    {
        //quitting the function if array is sorted from end to start
        if(n==1){
            return;
        }
        //iterating through array
        for(int i=0;i<n-1;i++)
            //changing integer position if array if not sorted
            if(array[i]>array[i+1]){
                int temp=array[i];
                array[i]=array[i+1];
                array[i+1]=temp;
            }
        //calling sortArray recursively 
        sortArray(array, n-1);
    }
    
    //main method
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int size;
            //reading array size
            System.out.print("Enter array size: ");
            size=sc.nextInt();sc.nextLine();
            //declaring array of size entered
            int array[]=new int[size];
            //reading array elements
            for(int i=0;i<size;i++){
                System.out.print("Enter array element "+(i+1)+": ");
                array[i]=sc.nextInt();sc.nextLine();
            }
            //printing array elements before sorting
            System.out.println("Array elements before sorting: ");
            for(int i=0;i<array.length;i++){
                System.out.print(array[i]+" ");
            }
            //calling sortArray method
            sortArray(array, size);
            //printing array elements after sorting
            System.out.println("\nArray elements after sorting: ");
            for(int i=0;i<array.length;i++){
                System.out.print(array[i]+" ");
            }
        }
}

Code Screenshot:

Output:


Related Solutions

Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3...
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3 numbers a, b, c, from the user, and prints the 3 numbers in ascending order
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT