1
00:00:00,510 --> 00:00:03,030
OK, so how do we do this searching problem?

2
00:00:03,060 --> 00:00:09,990
Well, we're going to go over the most basic, rudimentary style of search, which is called linear

3
00:00:10,050 --> 00:00:12,990
search or sequential search and all.

4
00:00:12,990 --> 00:00:19,050
It is literally just moving one by one from left to right through an array of vector and looking for

5
00:00:19,050 --> 00:00:20,900
the thing that we're interested in.

6
00:00:20,910 --> 00:00:22,040
So this is pretty straightforward.

7
00:00:22,050 --> 00:00:24,820
You probably were able to figure this out.

8
00:00:24,840 --> 00:00:26,910
This one was quite a bit easier than the other ones.

9
00:00:28,140 --> 00:00:32,460
And you know, that's the most straightforward way to find something is just linear search.

10
00:00:32,700 --> 00:00:34,950
So let's go ahead and go in here.

11
00:00:34,950 --> 00:00:39,690
I've already written most of a program because this is stuff we've already done.

12
00:00:39,700 --> 00:00:42,330
So what I do is I just make a vector here.

13
00:00:42,900 --> 00:00:48,150
I have an integer that I read into for those five numbers that go into the vector.

14
00:00:49,470 --> 00:00:53,160
Then I have number one and number two, which is the two numbers that get taken from the user to make

15
00:00:53,160 --> 00:00:53,730
the sum.

16
00:00:54,770 --> 00:01:01,460
So in this loop right here, it's just what we've been doing over and over so far, which is getting

17
00:01:01,460 --> 00:01:04,050
five numbers and reading them into the vector here.

18
00:01:05,060 --> 00:01:09,430
Then we say into two integers to be summed and we read in two more integers.

19
00:01:09,440 --> 00:01:12,290
And then what I check here is, I say if.

20
00:01:13,290 --> 00:01:22,050
Linear search and I send this vector and the sum of number one, plus them to both as arguments to this

21
00:01:22,050 --> 00:01:27,510
and linear search function, and that function is up here, which I have not written yet, but you notice

22
00:01:27,510 --> 00:01:35,490
that it takes a vector of integers by reference here, passing it by reference and then we have an integer

23
00:01:35,490 --> 00:01:36,690
in this pass by value.

24
00:01:38,460 --> 00:01:46,500
So it says if this function returns true, then this whole statement here will be replaced by true and

25
00:01:46,500 --> 00:01:52,500
it will evaluate to true, and it will run the code inside here, which says that no one less known

26
00:01:52,500 --> 00:01:55,830
to equals the sum is in the list.

27
00:01:56,010 --> 00:02:00,120
Else we're going to say the same thing to say is not in the list.

28
00:02:01,200 --> 00:02:01,610
All right.

29
00:02:01,650 --> 00:02:07,650
So pretty straightforward, I'm just going to go ahead and implement this function is a very straightforward

30
00:02:07,650 --> 00:02:08,140
function.

31
00:02:08,160 --> 00:02:09,280
It's just going to be simple.

32
00:02:09,280 --> 00:02:12,270
I live where I do the same style.

33
00:02:12,300 --> 00:02:17,730
Live or die with zero is less than the.

34
00:02:17,970 --> 00:02:22,140
I'm going to say V because we're passing the vector as the variable V.

35
00:02:22,500 --> 00:02:29,910
Some say we that size I + + and then I'm just going to have some f check in here.

36
00:02:29,910 --> 00:02:32,490
And I'm just going to say if v ie

37
00:02:35,020 --> 00:02:39,810
is equal to N, then I'm just going to return true.

38
00:02:43,590 --> 00:02:47,730
And I'm going to live through this whole vector, and so if I ever see.

39
00:02:49,690 --> 00:02:56,020
That the current position in the vector is equal to the number that was passed to this function.

40
00:02:56,320 --> 00:03:00,100
Then I'm going to immediately return from the function and I'm going to return truth to notice that

41
00:03:00,100 --> 00:03:02,110
this is a Boolean type function.

42
00:03:02,140 --> 00:03:03,700
This is the return type it's bore.

43
00:03:04,960 --> 00:03:08,440
So I'm going immediately, just break out of the whole function and return true.

44
00:03:09,690 --> 00:03:19,080
If I'm able to successfully make it through the whole loop, so up to, you know, we got size minus

45
00:03:19,080 --> 00:03:20,970
one basically and I stopped the loop.

46
00:03:21,300 --> 00:03:27,000
That means that I never found an inside of the vector.

47
00:03:27,510 --> 00:03:34,110
And so in that case, I'm going to basically be down here after this last bracket for the loop.

48
00:03:34,110 --> 00:03:39,990
And so I can just return false at this point because that means that I did not find the item I was looking

49
00:03:39,990 --> 00:03:40,380
for.

50
00:03:41,400 --> 00:03:44,250
And so this will return either true or false.

51
00:03:45,000 --> 00:03:51,360
And that will set us up nicely right here to check to see whether the number was in the list or not.

52
00:03:51,390 --> 00:03:52,950
So let's go ahead and run this.

53
00:03:52,950 --> 00:03:56,400
So I'm just going to for a plus linear search.

54
00:03:57,210 --> 00:04:03,540
So I'll say, let's start out and I'll run this and let's just

55
00:04:06,660 --> 00:04:07,890
put some numbers in here.

56
00:04:07,890 --> 00:04:15,930
So we have four seven eight one three, and I'm just going to say, how about we do five and three?

57
00:04:16,740 --> 00:04:18,890
So five plus three is eight is in the list, right?

58
00:04:18,900 --> 00:04:19,740
Because eight is here.

59
00:04:19,740 --> 00:04:28,110
So let's go ahead and try this again, and I'll put the same one I to save for seven eight one three.

60
00:04:28,710 --> 00:04:32,310
And let's put something like, you know.

61
00:04:33,470 --> 00:04:36,710
Nine and three.

62
00:04:37,550 --> 00:04:39,950
So, Nancy is one that is not in the list.

63
00:04:40,870 --> 00:04:48,060
All right, so just have those two examples there, it's often best to do a lot more input and testing

64
00:04:48,060 --> 00:04:53,340
with your programs just to be sure that everything is working correctly, but I'm just going to leave

65
00:04:53,340 --> 00:04:54,150
it at this.

66
00:04:54,150 --> 00:04:56,820
So if you are able to figure that out, great.

67
00:04:56,850 --> 00:05:04,860
If not, you know, it's you're probably just overthinking it because really, you can just use that

68
00:05:04,860 --> 00:05:10,410
linear surge to loop through with just a basic foil loop and you don't have to do anything else besides

69
00:05:10,410 --> 00:05:19,110
that, although there is a more efficient way to search for items in a list, and that's what we're

70
00:05:19,110 --> 00:05:27,240
going to be going over in the next lecture, and it actually requires us to first sort list.

71
00:05:27,270 --> 00:05:30,750
So with that, I will see you in the next lecture.
