Linear Search Invariant

preview_player
Показать описание
Discusses the invariant of a linear search algorithm.

Рекомендации по теме
Комментарии
Автор

5 years later, *raises hand*

The invariant is the element you are looping through.

hyayyuddhbi
Автор

The array is invariant, the algorithm does not mutate anything and simply loops through it so every time you call it it will produce the same output. You never move the hammer to a different place. (5 years after watching the video and I am here [trying to] answering all your questions. I hope you don't get annoyed with the notification).

MatheusLustosa
Автор

Well... In my opinion, as a developer for 25+ years, the invariant of a linear search would be not to alter the values and to exit from the loop avoiding a infinite loop or seach outside "valid" memory (C/C++/ASM). And by the way, I'm here for the entertainment. Jamie is smart and a great teacher as well as entertainer! :-)

jeffmerlin
Автор

My answer would be is that i < array.length because if this is not true, then they function will constantly provide a -1. Thus, the function will actually never compare the target value with the values that are within the array.

ED-ihlr
Автор

The invariant is the array itself (including the length of the array, obviously) and the specific value we are looking for.

uzairhasan
Автор

One invariant would be the array passed in because the function needs an initialized array, the loop needs to know the length of the array, and the index we return needs to be the an index within the array that matches the target value. Another invariant would be the type of the array and the target value because, in this case, we are looking for a int in our int array.

luisandrade
Автор

just remembered of you today Jamie Sir that why are u not uploading any interesting stuff from so long, and suddenly u came up with a video haha you will live 100 years :D

yashdodeja
Автор

invariants for the loop are
1. i starts at zero. 
2. targetValue is constant
3. the array, indexOfElment and value are constant

Mohsin
Автор

Looks like the parts of the loop that don't change are:
- the length of the array (once the loop starts, it always checks against the same array length).
- the searched integer
- the type of test
- the return statement

sylverg
Автор

In my opinion invariant means that the element which had gone through the operations of a program and by this property gain by the element remains unchanged till the program terminate. At the end of the termination invariant evolves a stable property

akashkhunt
Автор

the values in the array, the length of the array, the value we're searching for and the sentinel value

maximilianhelgesen
Автор

My best guess based on the invariant video that you put of (A constant non changing variable). I would say the two arguments thrown into the method used by the loop are your invariant.

shlude
Автор

I would say that invariant are the array passed in params since we read values but doesnt change them and the element that is compared to the current element of the array. However the index used to loop is a variant because it's value change at each iteration of the loop.

bladbimer
Автор

I think that the invariant is that it always goes through each element of the array and that it always returns a number, this number either being -1 or any integer within the bounds of 0 and the array length.

derekdj
Автор

The invariants are that a populated array and a target value are passed in to indexOf, and those values do not change while in indexOf.

joewidi
Автор

The method and the print are the invariants. I would be tempted to say the array and the target value as these are required for the method to work, but they are also variables, which is the opposite of invariant. I am however quite new to this.

gregilett
Автор

Following your definition of invariant, where you must clean up after yourself after you used a method.
The invariant would be to reset the iteration variable value to zero, otherwise the next time you use the method you may not loop through the array at all or loop only through a subset of the array.

LBJ
Автор

The array elements, array length and the value we are searching the index of.

Edit*
Admittedly I am rarely thinking of my algorithms in terms of invariants. Despite not being a beginner, I still learnt something good from this. Cheers for a great channel!

You reminded me of when I sat in a programming class learning C++ basics. Our teacher insisted on talking about apples and boxes to explain variables and datatypes. I couldn't grasp what the hell that guy was talking about. Afterwards I asked a friend what datatypes are good for. I don't remember how he phrased it, but it took him less than ten seconds to describe it in clear language.

Simple proof that teaching is more than just knowing about the subject. You are evidently a great teacher considering how many positive comments you are receiving on your videos. I am learning a lot too.

I don't know if you cover it in any of your videos, but Rubber Ducking is probably the most important term I've learnt about in programming. If you can't express in words what your code is doing, then you probably don't have any real clue about what you have typed on the screen and what it is supposed to do.

DennyLindberg
Автор

I also have no idea, just watched the previous videos, but I would say i (int) and meInts (array)are the invariants, because these are hard-coded, and don't change

bradem
Автор

I was reading my Algorithms book and had to stop to watch this a couple times. i definitely understand it better now. thanks for the video! hope you can post algorithms videos as quickly as we cover the material. it'll definitely cut study time dramatically!

allenbui