How to get index of m smallest elements from an unsorted array, without changing the actual array?

巧了我就是萌 提交于 2019-12-13 04:15:17

问题


I wanted to create a temporary array to store the input by the user and sort it to get the m smallest elements and refer to the original index of the originalArray and then print the index of the m smallest elements but when I run my code, all I get is -1. My elements should not be out of bounds of the original array as it is taken from originalArray. Why am I getting -1?

import java.util.*;
public class MinimumSelection
{
    public static void main(String[] args)
    {
        //Array and variable declarations
        int[] originalArray;
        int[] tempArray;
        int tempValue;
        int lowestValue;
        int arrayLength;
        String elementValue;

        //Prompt the user for random numbers as an array input
        System.out.println("Please Enter your array length");
        arrayLength = Integer.parseInt(new Scanner(System.in).nextLine());
        //Input feeds as the length of the array as entered by user
        originalArray = new int[arrayLength];
        //Storing the input value in temporary array which will be used to sort
        tempArray = new int[arrayLength];

        //prompt user to enter elements of the orignial array
        for (int element = 0; element < originalArray.length; element++)
        {
            System.out.printf("\n Enter array elements %1$s: " + "\r\n", element + 1);
            elementValue = new Scanner(System.in).nextLine();
            originalArray[element] = Integer.parseInt(elementValue);
        }

        System.arraycopy(originalArray, 0, tempArray, 0, originalArray.length);

        //Sorting the original array
        for (int write = 0; write < tempArray.length; write++)
        {
            for (int sort = 0; sort < tempArray.length - 1 - write; sort++)
            {
                if (tempArray[sort] > tempArray[sort + 1])
                {
                    tempValue = tempArray[sort + 1];
                    tempArray[sort + 1] = tempArray[sort];
                    tempArray[sort] = tempValue;
                }
            }
        }

        //promoting user to enter no. of smallest elements they want this program to display
        System.out.println("Please Enter number of smallest element");
        lowestValue = Integer.parseInt(new Scanner(System.in).nextLine());

        //display output
        System.out.println("Result :");
        for (int loop = 0; loop < lowestValue; loop++)
        {
            int x = tempArray[loop];
            int y = Arrays.asList(originalArray).indexOf(x);
            // Arrays.asList(array).indexOf(4);

            System.out.println((new Integer(y)).toString());
        }
        new Scanner(System.in).nextLine();
    }
}

回答1:


Create a TreeMap where the key is the element and the value is its index in the original array. Then you iterate through the entries of this map and break out of the loop after the first m steps.



来源:https://stackoverflow.com/questions/21515895/how-to-get-index-of-m-smallest-elements-from-an-unsorted-array-without-changing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!