Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Find the first and the second minimum index of each row in 2d array

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm trying to find the index of first minimum value and the index second minimum value of each row in 2D array.
For example, the input: int arr[][] = {{1, 2, 3, 4, 5, 6},{1, 2, 3, 4, 5, 6}};  the output must be:The smallest element index of each row is [0, 0] and second Smallest element index of each row is [1, 1].
Please help me adapt the following code:

Thanks in advance!
 
Marshal
Posts: 65782
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't a 2D array; Java┬« only supports 1D arrays. What that is is an array of arrays, which is better. I think you should start by finding the smallest elements in a 1D array, because you can scale it up to more dimensions later.
Please show us the algorithm you are using, as well as the definition of next‑smallest element. Every array with ≥ 2 elements has a next‑smallest element, so you can't say there is no next‑smallest element. Maybe you mean smallest not equal to the actual smallest element; that is something different. Or can you impose a restriction that your arrays are free from repeated values?
I don't understand the code you have been given; I think you will end up deleting it. I tried that code and it didn't work. So forget it. I suggest you start by finding the smallest element in an array, and print,

The smallest element was −43752083 at 97.

Once you have the smallest element, work out how to find the next‑smallest element. It is easy to do with two passes through the array, ut it should be possible by iterating the array only once.
 
nanouha abab
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I need to find only the second index of the minimum value of each array of 2 arrays. I try to code with one array and it works. What I must do to find the index of min of each row of  int[] []a ={ {5, 4, 0, 2, 10, 30, 3},{5, 4, 0, 2, 10, 30, 3}};
 
Sheriff
Posts: 6266
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe what you need to do is put another loop outside of the existing loop that changes the index to the internal arrays.
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and what happens when you use more irregular arrays? Try this several times:-That will show you a 100‑element array, “randomly” filled. That will give a better test of your method.
Your technique looks good. But move it out of the main() method. Try getting it to return an int[] comprising the two indices:-See how that works. Once you have that sort of array, you can copy the values into the arrays you want.
 
Saloon Keeper
Posts: 6224
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nanouha abab wrote:Actually, I need to find only the second index of the minimum value of each array of 2 arrays. I try to code with one array and it works. What I must do to find the index of min of each row of  int[] []a ={ {5, 4, 0, 2, 10, 30, 3},{5, 4, 0, 2, 10, 30, 3}};

You are not properly updating 'secondSmallesIndex'.
 
Bartender
Posts: 3509
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative way, and a bit more flexible in that you can also obtain the index of the nth smallest element, is to take a copy of your array, sort it, take element n, and do a search for it in the original array. The Arrays class can help you here.
 
His brain is the size of a cherry pit! About the size of this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!