Question

In: Computer Science

. Consider the following sorting procedure. This procedure uses nested loops to make several passes through...

. Consider the following sorting procedure. This procedure uses nested loops to make several passes through the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the sorting procedure leaves the values as they are. If a pair is in decreasing order, the sorting procedure swaps their values in the array.  The first pass compares the first two elements of the array and swaps their values if necessary.  It then compares the second and third elements in the array.  The end of this pass compares the last two elements in the array and swaps them if necessary.  After one pass, the largest element will be in the last index.  After two passes, the largest two elements will be in the last two indices.  After n – 1 pases (where n is the size of the array), the largest n – 1 elements will be in the last n – 1 indices and we have a sorted array.(java

Solutions

Expert Solution

thanks for the question, the steps given in the question are the algorithm steps of a bubble sort. Below is the code in Java that contains the sort() method, the sort() method takes in an int array and sorts the numbers based on the steps given in the question.

In order to demonstrate, at the end of every pass i have printed the array in order to visualize how the largest numbers are getting shifted to the end.

Here is the code and screenshot. You can remove or comment the print statements if needed

===========================================================================

import java.util.Arrays;

public class Main {

    public static void sort(int[] array) {

        for (int pass = 0; pass < array.length; pass++) {

            for (int j = 0; j < array.length - 1 - pass; j++) {

                if (array[j] > array[j + 1]) {
                    int swap = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = swap;
                }
            }
            System.out.println("Pass - " + (pass + 1));
            System.out.println(Arrays.toString(array));
        }
    }

    public static void main(String[] args) {


        int num[] = {5, 6, 1, 2, 8, 9, 3, 4, 7, 0};
        sort(num);


    }


}

===========================================================================


Related Solutions

Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Design a program that uses Nested loops to collect data and calculate the average rainfall over...
Design a program that uses Nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the numbers of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
After three passes through the outer loop of a sorting algorithm, an array changes from 43...
After three passes through the outer loop of a sorting algorithm, an array changes from 43 16 48 37 81 54 71 29 to 16 29 37 48 81 54 71 43 What sorting algorithm is being used to sort the array? A. Selection sort B. Bubble sort C. Insertion sort
In procedure 2: suppose red light passes through a double slit and falls on a screen....
In procedure 2: suppose red light passes through a double slit and falls on a screen. In the diffraction pattern, the distance from the central maximum to the first maximum is 5 mm. a) The distance from the first minimum (dark spot) to the second minimum in the diffraction pattern is between 7.5 mm and 10 mm less than 2.5 mm     more than 10 mm between 2.5 mm and 5 mm exactly 2.5 mm between 5 mm and 7.5 mm...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2...
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ####12 ###123 ##1234 #12345
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT