educative.io

Index out of range

for j:=0;j<len(sl);j++{

    for i:=0;i<len(sl);i++{

        if(sl[i]>sl[i+1]){

            temp=sl[i]

            sl[i]=sl[i+1]

            sl[i+1]=temp

        }

    }

}

Why the above code goes beyond range

Remember, that for n elements you just need to make ‘n-1’ comparisons. So you need to make ‘n-1’ iterations only.

Got it. Thanks :+1: