• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help modifying a method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I created a method that accepts an integer array as a parameter and returns the element with the smallest value as the return value; in essence, all that was required was a loop that iterated until the view became empty. Then, I changed my method to include two additional parameters: the method is supposed to find the minimum value in the array within the given index range instead of the entire array since int startIndex and endIndex are supposed to limit the view on the array. The function findMin(arr, 2, 5) checks indices 2, 3, 4, and 5 and returns 2 as the smallest number in the array's range. I also found a method to swap two array indices, such as swap(array, 3, 7) to replace index 3 with index 7.



Right now, I'm stuck on modifying the findMin method so that it returns the index of the min element rather than the actual min element. And maybe rename it to findMinIndex.

Can someone help me?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you are confusing yourself with the names of the parameters for that method. You called them minIndex and maxIndex, but they aren't the indices of the minimum or maximum. If you called them startIndex and endIndex, maybe you would find the method easier to understand.
Do you want the two indices to work on the start‑inclusive end‑exclusive convention? If you were at a more advanced stage, you would probably verify that the two indices are within the bounds of the array before starting the method.
Yes, it would be a good idea to call the method findMinIndex() or similar.
 
Steven Baka
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I think you are confusing yourself with the names of the parameters for that method. You called them minIndex and maxIndex, but they aren't the indices of the minimum or maximum. If you called them startIndex and endIndex, maybe you would find the method easier to understand.
Do you want the two indices to work on the start‑inclusive end‑exclusive convention? If you were at a more advanced stage, you would probably verify that the two indices are within the bounds of the array before starting the method.
Yes, it would be a good idea to call the method findMinIndex() or similar.





With this code I received 5, we now have the index for arr[4].

Do you by any chance know how to write the code for the selection sort algorithm?

This is how it was explained to me:

If we look at the selection sort algorithm, we start with a full array, search the minimum, swap it to the front, then reduce the view on the array by one. There are two variables needed—one for the start index and the other for the end index—and a loop that iterates over the array's elements repeatedly until the view is empty (start == end index). During the loop, we find the minimum, swap it to the front, and then decrease the view by one (startIndex++).




 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds right for selection sort. But remember the end index will be the same as the size of the array.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you managed your selection sort?
Note that your screenshot (we don't usually like screenshots) uses 1‑based indices and Java® arrays always use 0‑based indices. Also, I recommend you write a swap() method, like this:-Remember how important the documentation comments are.To swap in an array of Objects, use the header from this incomplete method:-
 
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic