-
Return Minimum Index Java카테고리 없음 2020. 1. 23. 20:06
The idea is to guess that the first element is the maximum and then if you see a number greater, then update that to be the new maximum. codeint findMaxIndex(int arr) int max = arr0; int maxIdx = 0; for(int i = 1; i < arr. First element in the sorted list. // would be minimum. Return sortedlist.get( 0 ). // function return maximum value in an unsorted. // list in Java using Collection.
Find Minimum Value In Array Java
The best way to 'translate the value of the element to the index' is to simply keep track of what index you are looking at. To do a 'swap' (switch two values in the array) all you need are three pieces of information: The two indexes you want to swap, and a temporary variable to hold the value of the overwritten index.Soarray5 = array4;array4 = array5;Doesn't work because you just overwrote array5. So you'd do this instead:int temp = array5;array5 = array4;array4 = temp;That probably doesn't help. Feel free to ask some questions about sorting, we'll all be glad to help. Actually your initial code was just searching for the 1st and 2nd number.If you want sorting, then the algorithm is entire different.It depends on your specifications.
I mean the easiest way to do this, is first to sort the array and then you will know that the 1st number is the first element of the array, the 2nd number is the second element of the array.Sorting is easy. It is more difficult to find the second number without sorting, which why I was referring to your specifications. Maybe you were asked to find the 1st and 2nd without sorting.