Generation

generate functionWed, 25 Jan 2023

# Write a program that does the following to achieve sorting of n data. Receives an int-type array consisting of n elements and an int-type value representing "ascending order" (1) and "descending order" (-1) as arguments, and sorts the elements in the array in ascending or descending order. Create a method. An int type array with n elements is generated by generating pseudo-random numbers using the Random class, and the seed value and the number of elements n (n ≤ 100) for generating random numbers are given in advance. to generate an array. Enter the seed value for random number generation and the number of data n (n ≤ 100) in the command line input , the sorting mode (ascending/descending) is entered in this order, the number of elements n Generate an array of random numbers, and use the created sorting method to sort n Sort the data. Output the sorted result to the standard output. When creating this program, please create it so that it meets the following requirements: [Method] A method that sorts data in an array in ascending or descending order has the following specification: Method name: sort argument: Sort mode (ascending/descending order) - int type ("1" for ascending order, "-1" for descending order) Target array - an array of type int Return value: None Contents: Sorts the data in the array received as an argument in ascending or descending order according to the value specified for the mode. For the sorting method, use the “bubble sort” shown in the lecture. Methods other than the sort method can be created as needed. 【input】 Enter the data required to run the program in the command line input. Enter the following data in this order, separated by a single space: Seed value for random number generation Number of data to sort n (n ≤ 100) Sort mode ("1" or "-1"): "1" for ascending order, "-1" for descending order 【output】 Output to stdout is sorted n prints integer values, one per line. When outputting, all single-byte numbers are used, and each integer value is output with left justification. Output from the left end of the line without inserting a space at the beginning of the line. 【others】 number of elements n(n≤100) Generating an array of type int with a random number of obeys the following requirements: Use the Random class to generate random numbers. If the seed value for random number generation entered in the command line input is stored in the int type variable seed, for example, the sequence of random number generation is 　　Random rand = new Random(seed); is determined as In this case, in the following, random numbers are generated through the variable rand of the Random class. Generate a random number between 0 and 999 through the declared Random class variable. Given the variable rand declared above, the following expression generates random numbers: 　　rand.nextInt(1000) The above random number generation is repeated n times, and the generated random numbers are substituted in order from the top element of the array.

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

public class Main {

public static void main(String[] args) {

// Read in the input
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

// Call insertionSort method for sorting
insertionSort(arr);

// Print the sorted array
printArray(arr);
}

public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}

public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int value = arr[i];
int j = i - 1;
while (j >= 0``````