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...
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>...
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...
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;...
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...
use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...
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]) {...
I have a java project that I need help trying to finish. I have most of it completed but the issue I am running into is adding numbers with different lengths. Project requirements: . Use a Stack ADT with the implementation of your choice (Array or Link), it should not make a difference 2.Read two “integer” numbers from the user. Hint: You don’t need to use an int type when you read, it may be easier to parse the input...