Question

suppose values is a sorted ArryList of integers. give a pseudocode that describes how a new...

suppose values is a sorted ArryList of integers. give a pseudocode that describes how a new value, X, can be inserted in its proper position so that the resulting ArrayList stays sorted

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;

public class AddSorted {

    public static void addSortedOrder(ArrayList<Integer> list, int item) {
        for (int i = 0; i < list.size(); i++) {
            if (item < list.get(i)) {
                list.add(i, item);
                return;
            }
        }
        list.add(item);
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        addSortedOrder(list, 3);
        addSortedOrder(list, 9);
        addSortedOrder(list, 1);
        addSortedOrder(list, 2);
        addSortedOrder(list, 7);
        addSortedOrder(list, 5);
        System.out.println(list);
    }
}

procedure addSortedOrder(list, item)
    for i from 0 to size(list)
        if (item < list.get(i))
            list.add(i, item)
            return
        end if
    end for
    list.add(item)
end procedure
Add a comment
Know the answer?
Add Answer to:
suppose values is a sorted ArryList of integers. give a pseudocode that describes how a new...
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
  • Question 27 (Mandatory) (1 point) Which sorting algorithm uses the following pseudocode: For each new, unsorted...

    Question 27 (Mandatory) (1 point) Which sorting algorithm uses the following pseudocode: For each new, unsorted value v in an array, search backward through already sorted values in the array. Move values until a proper position is found for v. Place v in its correct position. Bogo Sort Selection Sort Insertion Sort Ascending Sort Bubble Sort

  • 2. The following (adapted from Wikipedia) gives pseudocode for searching a sorted vector A for a ...

    2. The following (adapted from Wikipedia) gives pseudocode for searching a sorted vector A for a specific element T. Given an array A of n elements with values or records A1 An and target value T, the following subroutine uses binary search to find the index of T in A. 1. Set L to 1 and R ton. 2. If L>R, the search terminates as unsuccessful Set m (the position of the middle element) to the floor of (L+R)/2. 3....

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

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • Redo the insertionsort algorithm so that the values are inserted into a linked list rather than...

    Redo the insertionsort algorithm so that the values are inserted into a linked list rather than an array. This eliminates the need to move other values when a new value is inserted, since your algorithm can simply insert a new node where the new value should go. Analyze your algo- rithm to obtain a big-O expression of the worst-case running time. Code your algorithm to produce a complete C++ program that reads in a list of 10 in- tegers, sorts...

  • Now this: Suppose that you have been assigned to take over a project from another programmer...

    Now this: Suppose that you have been assigned to take over a project from another programmer who has just been dismissed for writing buggy code. One of the methods you have been asked to rewrite has the following comment and prototype: /* Method: insertValue(value, array) / • Inserts a new value into a sorted array of integers by * creating a new array that is one element larger than the - original array and then copying the old elements into...

  • 2) Write a recursive procedure in pseudocode to implement the binary search algorithm. 3) Explain, how...

    2) Write a recursive procedure in pseudocode to implement the binary search algorithm. 3) Explain, how the binary search algorithm can be modified, or used, to insert, a new integer element x, into a sorted list of n intgers.

  • Suppose that we have two unsorted lists of integers A and B. The lists are the...

    Suppose that we have two unsorted lists of integers A and B. The lists are the same size, n. a) Write an algorithm that outputs how many integers occur in both lists. An integer occurs at most once in each given list. For example: if A = [1,2,5,7,13,19] and B = [2,9,13,14,19,22], then we can see that elements {2, 13, 19} occur in both lists, so the output will be 3. b) If the lists were sorted, say how we...

  • 8. Consider the following algorithm, which finds the sum of all of the integers in a...

    8. Consider the following algorithm, which finds the sum of all of the integers in a list procedure sum(n: positive integer, a1, a2,..., an : integers) for i: 1 to n return S (a) Suppose the value for n is 4 and the elements of the list are 3, 5,-2,4. List assigned to s as the procedure is executed. (You can list the the values that are values assigned to all variables if you wish) b) When a list of...

  • 7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest va...

    7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest value to the first position on the first pass and so on. The pseudocode for this version is shown below. The procedure swap is used to interchange the values in its two arguments. In the box, rewrite the contents of the list 5, 2, 3, 4, 1 every time it changes, in this5, 2, 3, 4, 1 new version of bubblesort. Walk through each...

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