Language: Javapublic int[] sort(int[] numbers, int length)if (length < 2) {}return numbers;int mid length / 2;6int[] left = new int[id]:7int[] right new int[length mid]:9for (int i=0; i<mid; i++) {1011}1213left[i] numbers[i];for (int i-mid; i < length; i++) {INSERT MISSING CODE14}151617sort(left, mid);sort(right, length - mid);1819return merge(numbers, left, right, mid, length - mid);20}Image not displaying?Complete the sort function by replacing // INSERT MISSING CODE with the correct line of code.
Question
Language: Javapublic int[] sort(int[] numbers, int length)if (length < 2) {}return numbers;int mid length / 2;6int[] left = new int[id]:7int[] right new int[length mid]:9for (int i=0; i<mid; i++) {1011}1213left[i] numbers[i];for (int i-mid; i < length; i++) {INSERT MISSING CODE14}151617sort(left, mid);sort(right, length - mid);1819return merge(numbers, left, right, mid, length - mid);20}Image not displaying?Complete the sort function by replacing // INSERT MISSING CODE with the correct line of code.
Solution
public int[] sort(int[] numbers, int length) {
if (length < 2) {
return numbers;
}
int mid = length / 2;
int[] left = new int[mid];
int[] right = new int[length - mid];
for (int i = 0; i < mid; i++) {
left[i] = numbers[i];
}
for (int i = mid; i < length; i++) {
right[i - mid] = numbers[i]; // INSERT MISSING CODE
}
sort(left, mid);
sort(right, length - mid);
return merge(numbers, left, right, mid, length - mid);
}
Similar Questions
answer this question in simple code for beginners using intellJ: 4. Write a Java program to sort an array of given integers using the Merge Sort Algorithm.
What is the output of the following code snippet?int[ ] numbers = {3, 5, 2, 1, 4};Arrays.sort(numbers);System.out.println(numbers[numbers.length - 1]);Question 27Answera.1b.2c.4d.5
sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.Constraints:N/AExample:Input:[1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456] Output:[1254, 1456, 1456, 1457, 1458, 1472, 1789, 1899, 2013, 2035, 2165, 2365, 2456, 2458]
Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900
Write a class to sort the given set of integers in descending order. Include a try block tolocate the array index out of bounds exception and catch it.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.