Question

In: Computer Science

Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.

Java programming:


Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.

Now re-do this exercise and name it ExInsertionSort. This time use an insertion sort as given in this chapter to sort the array and then remove the item with the array sorted. Create a method for the insertion sort. Please only provide the code for ExInsertionSort.

Solutions

Expert Solution

public class ExInsertionSort {

    /*Function to sort array using insertion sort*/
    public static void Insertionsort(int arr[])
    {
        int n = arr.length;
        for (int i=1; i=0 && arr[j] > key)
            {
                arr[j+1] = arr[j];
                j = j-1;
            }
            arr[j+1] = key;
        }
    }

}
import java.util.Scanner;

public class RemoveFromSorted
{
    // binary search
    static int binarySearch(int arr[], int low, int high, int key)
    {
        if (high < low)
            return -1;
        int mid = (low + high)/2;
        if (key == arr[mid])
            return mid;
        if (key > arr[mid])
            return binarySearch(arr, (mid + 1), high, key);
        return binarySearch(arr, low, (mid -1), key);
    }

    /* Function to delete an element */
    static int removeItem(int arr[], int n, int key)
    {
        // Find position of element to be deleted
        int pos = binarySearch(arr, 0, n-1, key);

        if (pos==-1)
        {
            System.out.println("Element not found");
            return n;
        }

        // Deleting element
        int i;
        for (i=pos; i arr[j+1])
                {
                    // swap temp and arr[i]
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
    }

    /* Driver program to test above function */
    public static void main (String[] args)
    {

        int i;
        int arr[] = {20, 10, 50, 40, 70, 89};

        int n = arr.length;

        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter value to remove: ");
        int key = sc.nextInt();

        ExInsertionSort.Insertionsort(arr); // sorting

        System.out.print("Array before deletion:\n");
        for (i=0; iSystem.out.print(arr[i] + " ");

        System.out.println();

        n = removeItem(arr, n, key);

        System.out.print("\n\nArray after deletion:\n");
        for (i=0; iSystem.out.print(arr[i] + " ");
    }
}

Output:

Enter value to remove: 54
Array before deletion:
10 20 40 50 70 89
Element not found


Array after deletion:
10 20 40 50 70 89
Process finished with exit code 0


Related Solutions

Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
In.java In this program write a method called upDown that takes three integers as arguments and...
In.java In this program write a method called upDown that takes three integers as arguments and returns one of these 3 strings: "increasing" if they are in strictly increasing order (note that 3,3,4 - are not strictly increasing), "decreasing" if they are in strictly decreasing order. "none" otherwise. I recommend you use a complex condition to check for this (that is, have a single if statement with one big question). In the main method do the following: read three integers...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT