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

Next, write a program with a single-dimension array that holds 10 integer number

ID: 3818393 • Letter: N

Question

Next, write a program with a single-dimension array that holds 10 integer numbers and sort the array using a bubble sort. Here are the steps your program must accomplish. 1. Generate 10 random integer numbers between 1 and 100, and place each of the random numbers in a different element of the array starting with the first number generated. 2. Display the array's contents in the order the numbers are initially inserted. This is called an unsorted list. 3. Using the bubble sort, now sort the array from smallest integer to the largest. The bubble sort must be in its own method, it cannot be in the main method. 4. Display the array's contents after the bubble sort is completed

Explanation / Answer

SortArrayUsingBubbleSrt.java

import java.util.Random;
import java.util.Scanner;

public class SortArrayUsingBubbleSrt {

   public static void main(String[] args) {
      
       //creating an integer array of size 10
       int arr[]=new int[10];
      
       //calling the method by passing the array as argument
       arr=getArray(arr);
      
       //Displaying the elements of the array before sorting
System.out.print("Displaying the Array Elements Before Sorting :");
       for(int i=0;i<arr.length;i++)
       {
           System.out.print(arr[i]+" ");
       }
      
       //Calling the method by passing the array as argument
       arr=bubbleSort(arr);
      
       //Displaying the elements of an array after sorting
       System.out.print(" Displaying the Array Elements after Sorting :");
           for(int i=0;i<arr.length;i++)
           {
               System.out.print(arr[i]+" ");
           }  
          

   }

  
   //This method is used to sort the elements of an array in ascending order using bubble sort
   private static int[] bubbleSort(int[] arr) {
      
       //This Logic will Sort the Array of elements in Ascending order
       int temp;
       for (int i = 0; i < arr.length; i++)
   {
   for (int j = 1; j < (arr.length-i); j++)
   {
   if (arr[j-1] > arr[j])
   {
   temp = arr[j-1];
   arr[j-1] = arr[j];
   arr[j] = temp;
   }
   }
   }
       return arr;
   }

   /* This method is used to get the values entered
   * by the user and populate those values into an array
   */
   private static int[] getArray(int[] arr) {
      
       //Creating Random class objects
       Random r = new Random();
      
       /* This for loop will Generating Random Numbers between 1 and 100
       * and populate the elements in the array
       * */
       for(int i=0;i<arr.length;i++)
       {
          
           arr[i]=r.nextInt(100) + 1;
       }
       return arr;
   }

}

____________________

Output:

Displaying the Array Elements Before Sorting :11 43 99 61 60 29 2 13 84 47
Displaying the Array Elements after Sorting :2 11 13 29 43 47 60 61 84 99

______________Thank You

Please rate me well.If you area satisfied.

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