Question

Need // descriptions for each line of code that is relevant (Example, a description of "arr[i]=r.nextInt(100)+1;".Will...

Need // descriptions for each line of code that is relevant (Example, a description of "arr[i]=r.nextInt(100)+1;".Will give positive rate

import java.util.Random;

public class TestApp {
public static void main(String[] args) {
   int arr[]=new int[11];
   Random r = new Random();

//Random function for generation of random numbers
   for(int i=0;i<arr.length;i++)

//
       arr[i]=r.nextInt(100)+1;
   printArray(arr);
   System.out.println();
   bubbleSort(arr);
   printArray(arr);
   System.out.println();
   System.out.println("Median : "+(arr[arr.length/2]));
}
private static void printArray(int[] aArray) {
   for (int i = 0; i < aArray.length; i++)
       System.out.print(aArray[i] + " ");
}
public static void bubbleSort(int[] list)

{

int temp;

for (int i = list.length - 1; i > 0; i--)

{

for (int j = 0; j < i; j++)

{

if (list[j] > list[j + 1])

{

temp = list[j];

list[j] = list[j + 1];

list[j + 1] = temp;

}

}

}

}
}

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

Following is the answer:

import java.util.Random;

public class TestApp {
    public static void main(String[] args) {
        //Initialize array with size of 11
        int arr[]=new int[11];

        //Creates a random object
        Random r = new Random();

        //Random function for generation of random numbers
        for(int i=0;i<arr.length;i++)
            //assign random number to array
            //generates random number using formula r.nextInt((max - min) + 1) + min
            //this generates random numbers between 1(inclusive) to 100(exclusive)
            arr[i]=r.nextInt(100)+1;

        //calls print array function and pass the array so printArray function prints the array
        printArray(arr);

        //This line is for next line prints in newline
        System.out.println();

        //calls the bubble sort function to sort an array
        bubbleSort(arr);

        //again prints the array after sorting
        printArray(arr);

        //This line is for next line prints in newline
        System.out.println();

        //prints the median of the sorted array using array[length/2]
        System.out.println("Median : "+(arr[arr.length/2]));
    }

    //print array function to print the array
    private static void printArray(int[] aArray) {
        //lopp the array
        for (int i = 0; i < aArray.length; i++)
            //print each element of the array with the space after each element
            System.out.print(aArray[i] + " ");
    }
    
    //sorting function to sort the array
    public static void bubbleSort(int[] list)
    {
        //initialize the temp variable
        int temp;

        //loop through length -1 to 0 and decrement the counter variable
        for (int i = list.length - 1; i > 0; i--)
        {
            //loop through 0 to i and increment counter variable
            for (int j = 0; j < i; j++)
            {
                //compare if arr[j] > arr[j+1] if true then swipes the element using temp variable
                if (list[j] > list[j + 1])
                {
                    //if condition is true them arr[j] is assigned to temp 
                    temp = list[j];
                    //arr[j] is changed to arr[j+1]
                    list[j] = list[j + 1];
                    //temp is assigned to arr[j+1], so elements are swiped
                    list[j + 1] = temp;
                }
            }
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Need // descriptions for each line of code that is relevant (Example, a description of "arr[i]=r.nextInt(100)+1;".Will...
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
  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int...

    import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int temp;                       for (int i = list.length - 1; i > 0; i--)    {    for (int j = 0; j < i; j++)    {    if (list[j] > list[j + 1])    {    temp = list[j];    list[j] = list[j + 1];    list[j + 1] = temp;    }    }    }   ...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • In JAVA Thank You What is the exact output produced by running the method test? /**...

    In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */ public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y); } public void swap (int[] a,...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Below I have done a bubble sort on an array. I need to figure out how...

    Below I have done a bubble sort on an array. I need to figure out how to change the --- bubbleSort() method... with a new method called oddEvenSort() method.. I know I have to do two passes one for odd, one for even, can I have someone help me do this please I keep getting a lot of errors when I attempt to do it. Java code to change is below. I have to sort the array using one pass...

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