Tried numerous times but I think I'm doing this problem wrong. Will give thumb's up for help.


JAVA starter code:
import java.util.Scanner;
public class ArraySorter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in k, which represents the maximum
// distance between a number's current position
// and sorted position
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
// Sort the list
sort(numbers, k);
// Print the sorted list
StringBuilder resultSb = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
resultSb.append(new Integer(numbers[i]).toString());
if (i < numbers.length - 1) {
resultSb.append(" ");
}
}
System.out.println(resultSb.toString());
}
public static void sort(int[] numbers, int k) {
// TODO implement this function
throw new UnsupportedOperationException();
}
}
import java.util.Scanner;
public class ArraySorter {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
// Read in k, which represents
the maximum
// distance between a number's
current position
// and sorted position
System.out.println("Enter k
");
int k =
Integer.parseInt(sc.nextLine());
// Read in the list of
numbers
System.out.println("Enter numbers
seperated by spaces ");
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new
int[0];
} else {
String[]
numberStrings = input.split(" ");
numbers = new
int[numberStrings.length];
for (int i = 0;
i < numberStrings.length; i++) {
numbers[i] =
Integer.parseInt(numberStrings[i]);
}
}
// Sort the list
sort(numbers, k);
// Print the sorted list
StringBuilder resultSb = new
StringBuilder();
for (int i = 0; i <
numbers.length; i++) {
resultSb.append(new Integer(numbers[i]).toString());
if (i <
numbers.length - 1) {
resultSb.append(" ");
}
}
System.out.println("The sorted list
is : " + resultSb.toString());
}
public static void sort(int[] numbers, int k)
{
int[] heapArray = new int[k +
1];
for (int i = 0; i <= k
&& i < numbers.length; i++)
heapArray[i] =
numbers[i];
Minheap minHeap = new
Minheap(heapArray, k + 1);
for (int i = k + 1, j = 0; j < numbers.length; i++, j++) {
if (i <
numbers.length)
numbers[j] =
minHeap.replaceMinimumElement(numbers[i]);
else
numbers[j] =
minHeap.extractMinimumElement();
}
}
static class Minheap {
int[] heapArray;
int heapSize;
// Constructor
Minheap(int array[], int size) {
heapArray = array;
heapSize = size;
int i = (heapSize - 1) / 2;
while (i >= 0) {
MinHeapify(i);
i--;
}
}
// function to heapify a tree
void MinHeapify(int root) {
int leftChild =
getLeftChild(root);
int rightChild =
getRightChild(root);
int smallestChild = root;
if (leftChild < heapSize
&& heapArray[leftChild] < heapArray[root])
smallestChild =
leftChild;
if (rightChild < heapSize
&& heapArray[rightChild] <
heapArray[smallestChild])
smallestChild =
rightChild;
if (smallestChild != root) {
int temp =
heapArray[root];
heapArray[root]
= heapArray[smallestChild];
heapArray[smallestChild] = temp;
MinHeapify(smallestChild);
}
}
// helper function to get index of left child
int getLeftChild(int i) {
return (2 * i + 1);
}
// helper function to to get index of right
child
int getRightChild(int i) {
return (2 * i + 2);
}
// function to replace minimum element with x
int replaceMinimumElement(int x) {
int root = heapArray[0];
heapArray[0] = x;
if (root < x)
MinHeapify(0);
return root;
}
// function to extract minimum element and heapify
again
int extractMinimumElement() {
int root = heapArray[0];
if (heapSize > 1) {
heapArray[0] =
heapArray[heapSize - 1];
heapSize--;
MinHeapify(0);
}
return root;
}
}
}
// Output

Tried numerous times but I think I'm doing this problem wrong. Will give thumb's up for...
import java.util.*;
public class PairFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the value of k
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
//method that...
It is 2040, and you’ve just begun your new position as a Director of Engineering at Facebook. You have interviewed n candidates to be your executive assistant, and assigned each one a score for organization and a score for punctuality. Using merge sort, you must sort your list of candidates in descending order by job suitability, which we here define to be 0.8*organization + 0.2*punctuality. If candidates A and B are tied for job suitability, and candidate A comes before...
Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add(Event evt) Add an event to the calendar Event get(int i) Fetch the ith Event added to the calendar Event get(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...
Can someone explain me parts of this code I do not fully understand what its doing or what its purpose is. I have highlighted the parts I'm confused over. Please explain thoroughly. import java.util.Scanner; class A1Stats { public static void main(String args[]) { double min, max, sum, mean; int n; Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String[] numbers = input.split(" "); double value[] = new double[numbers.length]; if(numbers.length > 0){ for (int i = 0; i < numbers.length;...
Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public void sortImprovement1(int[] numbers) { for (int i = 1; i < numbers.length; i++) { for (int j = 0; j < numbers.length - i; j++) { if (numbers[j] > numbers[j + 1]) { swap(numbers, j, j + 1); } } } } Snippet 2.3: Bubble sort improvement 1 Change the bubble sort method so that it stops sorting if the array is untouched after...
The prime factorization of a number is the unique list of prime numbers that, when multiplied, gives the number. For example, the prime factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5. In this problem you must write code to recursively find and return the prime factorization of the given number. You must print these in ascending sorted order with spaces in between. For example, if your input is: 120 then you should print the following output: 2...
Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...
Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...
You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; } There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...
public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex. public static Integer[] selectSort(Integer[] incoming) { Integer[] ret = new Integer[incoming.length]; for (int i = 0; i < incoming.length; i++) { ret[i] = incoming[i]; } int temp = 0; for (int i = 0; i < ret.length - 1; i++) { if (ret[i] > ret[i + 1]) {...