Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program in which the recursive version of the bubble sorting method

ID: 3737545 • Letter: W

Question

Write a Java program in which the recursive version of the bubble sorting method with the following signature

public static void r_bubbleSort(int[ ] array, int s, int t)

should be defined. That method arranges a collection of integers in an integer array with beginning index s and terminating index t in ascending order. Your program will ask the user to input a positive integer for the size of an integer array, fill in the array with integers between 0 and 999 inclusively, display the integers in the array, invoke the method r_bubbleSort, and display the integers in the array again. A sample run of your program is as follows.

Input an integer for the size of an array

16

16 numbers are generated and they are

532 972 669 490 244 64 370 578 562 891 855 197 203 526 491 61

16 numbers are sorted by recursive bubble-sorting program and they are

61 64 197 203 244 370 490 491 526 532 562 578 669 855 891 972

Explanation / Answer

RecursiveBubbleSort.java

import java.util.Random;

import java.util.Scanner;

public class RecursiveBubbleSort {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Input an integer for the size of an array:");

int n = scan.nextInt();

int a[] = new int[n];

Random r = new Random();

for(int i=0;i<a.length;i++) {

a[i]=r.nextInt(1000);

}

System.out.println(n+" numbers are generated and they are:");

for(int i=0;i<a.length;i++) {

System.out.print(a[i]+" ");

}

System.out.println();

r_bubbleSort(a,0,n);

System.out.println(n+" numbers are sorted by recursive bubble-sorting program and they are");

for(int i=0;i<a.length;i++) {

System.out.print(a[i]+" ");

}

System.out.println();

}

public static void r_bubbleSort(int[] array, int s, int t) {

if(s > t){

return;

}

if (s == t - 1) {

r_bubbleSort(array, 0, t - 1);

} else if (array[s] > array[s+1]) {

int currentNumber = array[s];

array[s] = array[s + 1];

array[s + 1] = currentNumber;

r_bubbleSort(array, s + 1, t);

} else {

r_bubbleSort(array, s + 1, t);

}

return;

}

}

Output:

Input an integer for the size of an array:
16
16 numbers are generated and they are:
573 454 241 745 751 912 660 475 31 955 748 187 192 931 275 900
16 numbers are sorted by recursive bubble-sorting program and they are
31 187 192 241 275 454 475 573 660 745 748 751 900 912 931 955

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote