#I am doing the mooc(part 7/14, doing sorting algorithms) and i need help on index of smallest from

1 messages · Page 1 of 1 (latest)

tranquil inlet
#

I am doing the mooc(part 7/14, doing sorting algorithms) and i am doing the sorting exercise. i am stuck on part 3. the part says that i have to make a method to find the index of the smallest number in an array, but with one of the parameters is a startingIndex, which the method is only going to find the smallest index of the array without the values of the indexes before the starting index. i cant figure out how to do it. i have tried making a new array, only adding values to it(from the array) when the index is bigger or equal to the starting Index and then returning the indexOfSmallest of that new array. I have also tried making an indexOf method and then finding the smallest of the numbers(after the starting index) in the array and then getting the index of that number, but that didnt work. My file is in the message. i have deleted my past attempts from the file(i still have them somewhere else tho). could someone help me?(sry if any part of this message didnt make sense)

high steppeBOT
#

<@&987246399047479336> please have a look, thanks.

high steppeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

junior jolt
#

also your indexOfSmallest works?

#

hm according to the code it shouldnt always work

#

if you got indexOfSmallest correct then you can simply use 90% of it in the indexOfSmallestFrom

tranquil inlet
#

i am going to have lunch and then i am going to see if indexOfSmallest 100% works.

#

i checked that indexOfSmallest and i cant find a case that it doesnt work, so here is my previous attemts at doing indexOfSmallestFrom

public static int indexOfSmallestFrom(int[] array, int startIndex) {
        // first attempt
        int[] newArray= new int[array.length-startIndex];
        for(int i = 0;i< array.length;i++){
            if(i>=startIndex){
                newArray[i-startIndex]=array[i];
                System.out.println(newArray[i-startIndex]);
            }
        }
        return indexOfSmallest(newArray);
        // second attempt
        int smallest = 0;
        for(int i = 0; i< array.length; i++ ){
            if(i<=startIndex){
                 smallest = array[i];
            }
        }

        return indexOf(array, smallest);
    }
    // method for second attempt
    public static int indexOf(int[] array, int value) {
        for(int i =0; i<array.length; i++){
            if(array[i]==value){
                return i;
            }
        }
        return -1;
    }
junior jolt
#
    public static int indexOfSmallest(int[] array){
        int index = array[0];
        for(int i = 0; i< array.length; i++ ){
            if(array[i]==smallest(array)){
                index=i;
            }
        }
        return index;
    }

there is already something wrong with this

#

imagine this array:

6, 12, 60, 64

what will it return

#

also the if check is just wrong

#

you always go run the smallest method for each time you are checking

#

the result of smallest(array) will never change

#

ok so lets continue how I would solve this problem after finishing the first part

#

you got this:

    public static int smallest(int[] array){
        int smallest= array[0];
        for(int small : array){
            if(smallest>small){
                smallest=small;
            }
        }
        return smallest;
    }
#

and then the question is to return the index instead of the actual number

#

so I would just add a variable to this which always stores the index of the smallest number

#

but therefore you need to use a fori loop instead

tranquil inlet
#

idk what fori loops are, they werent in the previous parts of the mooc

junior jolt
#

? just the normal for loop

#
for(int i = 0; i < array.length; i++) {

}
#

example for loop

tranquil inlet
#

oh ok

junior jolt
#

and these store the current index

#

which will be needed in order to store the index of the current smallest

tranquil inlet
junior jolt
tranquil inlet
#

oh ok i get it

junior jolt
#

so you want to return the index of the smallest

#

so you would need a variable which stores the index of the smallest

#

and that variable should change when you found a new smaller value in the array

#

and at the end return the index

tranquil inlet
#

so something like this?

public static int indexOfSmallest(int[] array){
        int smallest= array[0];
        int index = 0;
        for(int i = 0; i< array.length; i++ ){
            if(smallest>array[i]){
                smallest=array[i];
                index=i;
            }
        }
        return index;
    }
junior jolt
#

yeah

#

I would maybe change the if condition to this array[i] < smallest, makes it better readable imo

tranquil inlet
junior jolt
#

xD

#

kinda simple though

#

x < y x smaller y

tranquil inlet
#

yeah ik, but like if someone made me look at a < or > operator, i would not be able to tell them which is smaller/bigger almost instantly. i would need to look at it carefully first

junior jolt
#

I kinda learned it like this
the area of the left is smaller
so the it shows that the left is smaller than the right pepekek

tranquil inlet
#

ok thank you

junior jolt
#

left is bigger than the right

#

ok so lets start with part 3

#

its basically part 2

#

but instead of checking the whole array you should start at a given point

tranquil inlet
# junior jolt ok so lets start with part 3

i tried something while you where explaining

public static int indexOfSmallestFrom(int[] array, int startIndex) {
        int smallest= array[0];
        int index = 0;
        for(int i = 0; i< array.length; i++ ){
            if(smallest>array[i]){
                if(i>=startIndex){
                    smallest=array[i];
                    index=i;
                }
            }
        }
        return index;
    }
junior jolt
#
public static int indexOfSmallest(int[] array){
        int smallest= array[0];
        int index = 0;
        for(int i = 0; i< array.length; i++ ){
            if(smallest>array[i]){
                smallest=array[i];
                index=i;
            }
        }
        return index;
    }

so if we are taking this from part 2, we iterate through the whole array from index 0 -> array.length
but now in part 3 we should only check numbers from index startIndex -> array.length

#

so what would you need to change

#

its just one small change

junior jolt
#

because now you are iterating again through the whole array 0 -> array.length but
the first array entries from 0 -> startIndex - 1 are not needed

tranquil inlet
#

ok i tried once again

int smallest= array[startIndex];
        int index = startIndex;
        for(int i = startIndex; i< array.length; i++ ){
            if(smallest>array[i]){
                smallest=array[i];
                index=i;
            }
        }
        return index;
junior jolt
#

yeah exactly

#

though

#

yeah nvm

#

all good

tranquil inlet
#

Ok thank you so much SquidXTV!!!! now my program works( and I know how to recognize which is smaller and bigger in > and < quickly!)