import java.util.ArrayList;
import java.util.Set;
public class Project {
/*
* Input is sorted (ascending) to within k positions
i.e. each value in the
* array is within k positions of where it should
be.
* For example:
* k=1 input=[2, 1, 3, 4, 5]
* k=2 input=[3, 2, 1, 4, 5]
* k=3 input=[4, 2, 3, 1, 5]
* More than one element might be off by k! For
example:
* k=2 input = [2, 3, 1, 5, 4]
*
* Return a sorted list. Using extra storage is ok.
Making another array to
* return is also ok.
*/
public int[] sortaSorted(int[] input, int k) {
int[] result = new
int[input.length];
return result;
}
}
Solution:
public class Project { public int[] sortaSorted(int[] input, int k) { for(int index = 1; index < input.length; index++) { int counter = k; int currIndex = index; while(currIndex > 0 && counter > 0 && input[currIndex] < input[currIndex - 1]) { int temp = input[currIndex]; input[currIndex] = input[currIndex - 1]; input[currIndex - 1] = temp; currIndex--; counter--; } } return input; } public static void main(String args[]) { int input[] = {2, 3, 1, 5, 4}; Project project = new Project(); int output[] = project.sortaSorted(input, 2); for(int element: output) System.out.print(element + " "); } }
Note: The main method has been added just to test the sortaSorted() method. (Bubble Sorting technique used)
Screenshot for reference:
![1 public class Project { public int[] sortaSorted (int[] input, int k) { for(int index = 1; index < input.length; index++) {](http://img.homeworklib.com/questions/542f3120-9d60-11eb-8e08-85743b58cd29.png?x-oss-process=image/resize,w_560)
import java.util.ArrayList; import java.util.Set; public class Project { /* * Input is sorted (ascending) to...
import java.util.ArrayList; import java.util.List; public abstract class AbstractBoxPacking { protected List input; protected int boxSize; public AbstractBoxPacking(List input, int boxSize){ this.input = input; this.boxSize = boxSize; } public abstract int getOutput(); public List deepCopy(List boxes){ ArrayList copy = new ArrayList(); for(int i = 0; i < boxes.size(); i++){ copy.add(boxes.get(i).deepCopy()); } return copy; } } I need Help fixing the errors in my java code
Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if the given array is a representation of a binary tree * @param entries 10 array of entries to be test * ereturn true if the input array encodes a binary tree, false otherwise 12 13 14 public static <K extends Comparable K, vs boolean isBinaryTree(List Entry<k,v entries) ( 15 16 17 // TODO: implement this return true 18 19 2e 21 * Checks if the...
Complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * This returns a string containing the char ch, count times. * For example calling with 'a', 5 would return: * "aaaaa" * * @param ch The character to repeat. * @param count The number of the character. * @return A string with count number of ch. */ public static String charToString(char ch, int count) { return null; //TODO }
import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * * DO NOT CHANGE THIS CODE * */ public class SearchMain { public static void main(String[] args) { ArraySearcher arraySearch = new ArraySearchImp(); SecureRandom secureRandom = new SecureRandom(); int[] sortedArray = generateRandomSortedIntArray(10); System.out.println(Arrays.toString(sortedArray)); Set<Integer> intSet = new HashSet<Integer>(); for(int i = 0; i < 5; i++) { ...
import java.util.Arrays; import stdlib.*; public class CSC300Homework4 { /** * As a model for Problem 1, here are two functions to find the minimum value of an array of ints * an iterative version and a recursive version * * precondition: list is not empty /** iterative version */ public static double minValueIterative (int[] list) { int result = list[0]; int i = 1; while (i <...
How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.HashSet; public class RotationCheck { /** * Rotates the elements in the specified list by the specified distance. After calling this * method, the element at index {@code i} will be the element previously at index * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and * {@code...
complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null; //TODO...
What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...