Nozim N
Nozim N

Reputation: 13

How come v [index] < v [index - 1] returns true if index == 0?

Link to the function image Wrote a function to find an index of a pivot element(starting/lowest) in sorted & rotated array. I worked through the problem and was checking for

edge cases and it even works for cases where index is zero. Could anyone

explain why/how v [index] < v [index - 1] returns true where index is

zero

Here is function progression:

[1 2 3 4 5 6 7 8 9 ]
Checking: 5, this is low & high:0, 8
Checking: 2, this is low & high:0, 3
Checking: 1, this is low & high:0, 0
Index of the pivot: 0

[7 8 9 1 2 3 4 5 6]

... Index of the pivot: 3

Upvotes: 0

Views: 550

Answers (1)

NewMe
NewMe

Reputation: 162

If index is 0 and you are trying to get value if index -1 it means that you are trying to access memory which does not belong to your array/vector. It is undefined behaviour. Don't do that

Upvotes: 2

Related Questions