There isn't a "rule of thumb" or algorithm that will tell you what the optimal fingering for a passage is. Fingering choice is mostly a matter of experience and practice.
Many techniques will keep your fingers in a 4 or 5 fret "zone" on the fingerboard, moving to a different zone as needed for the notes.
The most efficient way to learn good fingering technique is to find a competent teacher, who can show you things you didn't know you need to know, and offer corrections and examples as you progress on the instrument.
Not everyone will have access to a good teacher though.
As mentioned in the comments, learning your scales and scale patterns will show you the basic fingerings as well as teaching you where the different locations for the notes you want are.
Fingering choice will change depending on the passage you are playing, the position you want to play it in, and the leading and following chords or melodic passages.
Some ways you can gain experience for making these choices is to work on the basics of playing the instrument, then applying those basics to the song you want to learn.
Starting with chords, you can find chord charts that have the fingers listed for the chord shapes. You should look up multiple versions, as there are different fingerings for the same chords and you should learn multiple fingerings. Song melodies will often be created around the chord notes, and in some cases you'll find the the melody can be played simply by holding the chord and playing individual notes, or moving a single finger out of the chord. Practicing arpeggio runs up and down the neck will also show you different positions and fingerings for melodies. You can also find charts that show the arpeggio runs and where the shifts occur.
There are many methods for playing scales on fretted instruments. The ukulele is more limited in its range, but the basic techniques apply. In general, when starting with scales I use two main finger patterns: one finger per fret, skipping fingers to play a whole tone/step. The fingering would look like:
where 1 is the index finger and 4 is the pinky.
The other pattern I use is to separate the 1st and 2nd finger a whole step (two frets):
Some methods will use a violin/mandolin type of fingering where the each finger covers two frets:
I think the first two are simpler, especially for self teaching, and more closely matches the finger choices for the basic chord shapes.
If you do a search for "Ukulele C Major scale positions" you will find charts and tutorials that will get you started. You can use the one finger per fret pattern across the scale, moving up frets to each position. Here a couple links I found:
Once you have learned to play the scale, chords, and arpeggios, you will find that you can apply the same fingerings to the melodies you look up in tabs. The position you shift to and the various note location options will become easier to find as you play more.