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

Java program. Create a generic buble sort method that can sort an array of any t

ID: 645500 • Letter: J

Question

Java program.
Create a generic buble sort method that can sort an array of any type of objects that implements the comparable interface. The method is also to have a boolean parameter called isAscending to indicate whether the array is to be sorted in ascending or descending order. Create a main method that creates an array of 1000 integers. The integer are to be randomly selected from 0 to 99. Also an array of 1000 strings that are randomly generated of only lowercase letters in length from 1 to 10. Finally display both arrays before sorting, after sorting in ascending order and then again in descending order.

Explanation / Answer

import java.util.Random;

public class BubbleSortClass {
   public static <T extends Comparable<? super T>> void BubbleSort(T arr[], boolean isAscending) {
       int v;
       if(isAscending)
           v = 1;
       else
           v = -1;
       for(int i = 0; i < arr.length - 1; ++i){
           for(int j = 0; j < arr.length - 1; ++j){
               if(v * arr[j].compareTo(arr[j + 1]) > 0){
                   T temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
           }
       }
   }

   public static void main(String args[]) {
       Random r = new Random();
       Integer arr[] = new Integer[1000];
       System.out.println("Integer Array: ");
       int k = 0;
       for(int i = 0; i < arr.length; ++i){
           arr[i] = r.nextInt(1000);
           System.out.print(arr[i] + " ");
           ++k;
           if(k == 10){
               k = 0;
               System.out.println();
           }
       }
       System.out.println();
       String str[] = new String[1000];
       System.out.println("String Array: ");
       k = 0;
       for(int i = 0; i < str.length; ++i){
           String temp = "";
           int s = r.nextInt(10) + 1;
           for(int j = 0; j < s; ++j){
               temp += (char)(r.nextInt(26) + 'a');
           }
           str[i] = temp;
           System.out.print(str[i] + " ");
           ++k;
           if(k == 10){
               k = 0;
               System.out.println();
           }
       }
       System.out.println();
       BubbleSort(arr, true);
       BubbleSort(str, true);
       System.out.println("Integer Array sorted in ascending order: ");
       k = 0;
       for(int i = 0; i < arr.length; ++i){
           System.out.print(arr[i] + " ");
           ++k;
           if(k == 10){
               k = 0;
               System.out.println();
           }
       }
       System.out.println();
       System.out.println("String Array sorted in ascending order: ");
       k = 0;
       for(int i = 0; i < str.length; ++i){
           System.out.print(str[i] + " ");
           ++k;
           if(k == 10){
               k = 0;
               System.out.println();
           }
       }
       System.out.println();
       BubbleSort(arr, false);
       BubbleSort(str, false);
       System.out.println("Integer Array sorted in descending order: ");
       k = 0;
       for(int i = 0; i < arr.length; ++i){
           System.out.print(arr[i] + " ");
           ++k;
           if(k == 10){
               k = 0;
               System.out.println();
           }
       }
       System.out.println();
       System.out.println("String Array sorted in descending order: ");
       k = 0;
       for(int i = 0; i < str.length; ++i){
           System.out.print(str[i] + " ");
           ++k;
           if(k == 10){
               k = 0;
               System.out.println();
           }
       }
       System.out.println();
   }
}

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