简体   繁体   中英

How do I remove the item with the highest value in an unsorted array?

So right now I am trying to code a function that will remove the highest value in an unsorted array.

Currently the code looks like this:

    @Override
public void remove() throws QueueUnderflowException {
    if (isEmpty()) {
        throw new QueueUnderflowException();
    } else {
        int priority = 0;
        for (int i = 1; i < tailIndex; i++) {
            while (i > 0 && ((PriorityItem<T>) storage[i - 1]).getPriority() < priority)
            storage[i] = storage[i + 1];
            i = i - 1;
        }
        /*int max = array.get(0);

        for (int i = 1; i < array.length; i++) {
        if (array.get(i) > max) {
        max = array.get(i);
        }*/
    }
        tailIndex = tailIndex - 1;
    }

Here I have my attempt at this:

int priority = 0;
    for (int i = 1; i < tailIndex; i++) {
        while (i > 0 && ((PriorityItem<T>) storage[i - 1]).getPriority() < priority)
        storage[i] = storage[i + 1];
        i = i - 1;

The program runs no bother but still deletes the first item in the array instead of the highest number. This code was given my my college lecturer for a different solution but unfortunately it doesn't work here.

Would this solution work with enough altercations? Or is there another solution I should try?

Thanks.

Step 1
Find the highest index.

int[] array;
int highIndex = 0;
for (int i = 1; i < highIndex.size(); i++)
    if (array[highIndex] < array[highIndex])
        highIndex = i;

Step 2
Create new array with new int[array.size() - 1]

Step 3
Move all values of array into new array (except the highest one).

My hint: When its possible, then use a List . It reduces your complexity.

You can find the largest Number and it's index then copy each number to its preceding number. After that, you have two options:

  • Either add Length - 1 each time you iterate the array.
  • Or copy the previous array and don't include removed number in it.

Working Code:

import java.util.Arrays;

public class stackLargest
{
    public static void main(String[] args)
    {
        int[] unsortedArray = {1,54,21,63,85,0,14,78,65,21,47,96,54,52};
        int largestNumber = unsortedArray[0];
        int removeIndex = 0;
        // getting the largest number and its index
        for(int i =0; i<unsortedArray.length;i++)
        {
            if(unsortedArray[i] > largestNumber)
            {
                largestNumber = unsortedArray[i];
                removeIndex = i;
            }
        }

        //removing the largest number
        for(int i = removeIndex; i < unsortedArray.length -1; i++)
            unsortedArray[i] = unsortedArray[i + 1];

        // now you have two options either you can iterate one less than the array's size
        // as we have deleted one element
        // or you can copy the array to a new array and dont have to add " length - 1" when iterating through the array
        // I am doing both at once, what you lke you can do
        int[] removedArray = new int[unsortedArray.length-1];
        for(int i =0; i<unsortedArray.length-1;i++)
        {
            System.out.printf(unsortedArray[i] + " ");
            removedArray[i] = unsortedArray[i];
        }
    }
}

Note: Use List whenever possible, it will not only reduce complexity, but, comes with a very rich methods that will help you a big deal.

The code snippet in the question can be updated to below code, while keeping the same data structure ie queue and this updated code has 3 steps - finding the index of largest element, shifting the elements to overwrite the largest element and finally set the tailIndex to one less ie decrease the size of the queue.

    @Override
public void remove() throws QueueUnderflowException {
    if (isEmpty()) {
        throw new QueueUnderflowException();
    } else {
        int priority = 0;
        int largeIndex = 0;
        for (int i = 0; i < tailIndex; i++) {
          if (((PriorityItem<T>) storage[i]).getPriority() > priority) {
            priority = ((PriorityItem<T>) storage[i]).getPriority();
            largeIndex = i ;
          }
        }

        for(int i = largeIndex; i < (tailIndex - 1) ; i++)
          storage[i] = storage[i + 1];
    }
    tailIndex = tailIndex - 1;
  }

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM