Question

import java.util.ArrayList; import java.util.Set; public class Project { /*    * Input is sorted (ascending) to...

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;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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++) {

Add a comment
Know the answer?
Add Answer to:
import java.util.ArrayList; import java.util.Set; public class Project { /*    * Input is sorted (ascending) to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.ArrayList; import java.util.List; public abstract class AbstractBoxPacking { protected List input; protected int boxSize; public...

    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...

    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...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    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...

    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; /** *...

    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++) {       ...

  • 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...

    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...

    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>...

  • Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a...

    Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a linear search through the ArrayList list, returning the index of the first occurrence of * valToFind. * * Starting from index 0, check each element in list and return the index of the first element * that matches valToFind. * * @param valToFind The int value to search for. * @param list The ArrayList of Integers to search in. * @return The index of...

  • How can I make a test case with Junit? import java.util.ArrayList; import board.Board; public class Pawn...

    How can I make a test case with Junit? import java.util.ArrayList; import board.Board; public class Pawn extends Piece{    public Pawn(int positionX, int positionY, boolean isWhite) {        super("P", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               //check side, different color pawns go on different ways        if(isWhite) {            if(positionX != 7 && positionY != 0) {           ...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT