1
00:00:02,530 --> 00:00:05,120
So let's derive an algorithm.

2
00:00:05,170 --> 00:00:07,980
Now we have a certain input, in this case

3
00:00:07,990 --> 00:00:12,720
we have an array of numbers - 3, 1, 2

4
00:00:12,730 --> 00:00:14,440
and what's important here

5
00:00:14,440 --> 00:00:18,590
and that of course is simply just something which is the case for this example,

6
00:00:18,610 --> 00:00:27,220
this here is an unordered, an unsorted array, so it's not sorted in ascending or descending order,

7
00:00:27,280 --> 00:00:28,960
ut's randomly sorted.

8
00:00:28,960 --> 00:00:33,170
You see we have the smallest item here in the middle,

9
00:00:33,400 --> 00:00:35,240
so it could be anywhere

10
00:00:35,350 --> 00:00:40,150
and now our goal is to get the minimum number here.

11
00:00:40,150 --> 00:00:45,100
So in this case we would want to get one as a result because that of course is the smallest number in

12
00:00:45,100 --> 00:00:47,110
this given array.

13
00:00:47,110 --> 00:00:50,380
Now the question is, how do we get there?

14
00:00:50,380 --> 00:00:55,030
Well there are actually different approaches we could follow,

15
00:00:55,030 --> 00:01:03,850
one approach is that we simply write an algorithm where we loop with a for loop maybe through all the

16
00:01:04,120 --> 00:01:12,400
items and in every loop iteration here, we basically check if the item we're currently looking at,

17
00:01:12,400 --> 00:01:14,780
so we go through all the items from left to right,

18
00:01:14,800 --> 00:01:19,350
if the item we're currently looking at is the smallest item.

19
00:01:19,420 --> 00:01:26,890
So for that, before we actually enter the loop for the first time, we save a let's say curMin variable

20
00:01:28,250 --> 00:01:31,310
and set this simply to the first item in the array,

21
00:01:31,370 --> 00:01:33,400
so in this case to three

22
00:01:33,560 --> 00:01:37,070
and then in every loop iteration, we check if the next item in the array,

23
00:01:37,070 --> 00:01:40,950
so 1 if that is smaller than the current min,

24
00:01:40,970 --> 00:01:51,250
so if I is smaller than the current min and if I is smaller than the current min, then we set current

25
00:01:51,280 --> 00:01:52,650
min to I,

26
00:01:52,680 --> 00:01:54,600
so to the item we're currently looking at

27
00:01:54,770 --> 00:01:59,270
and in the next iteration, we'll compare the next item with the new current minimum

28
00:01:59,270 --> 00:02:06,650
and if the next item is not smaller than the current minimum, we skip this part here in the if check,

29
00:02:06,920 --> 00:02:09,050
so instead we then just continue.

30
00:02:09,050 --> 00:02:13,040
And if we do all of that, once we're out of that loop here,

31
00:02:13,340 --> 00:02:23,210
so once we're off that loop, we will have our current minimum and then we just return this current minimum.

32
00:02:23,210 --> 00:02:26,300
That would be a simple algorithm we could derive

33
00:02:26,300 --> 00:02:32,420
here, we go through all the items and we compare them with each other so to say and return our derived

34
00:02:32,420 --> 00:02:34,090
minimum at the end.

35
00:02:34,100 --> 00:02:40,660
Now this here, what I laid out here is basically my algorithm written in some pseudocode,

36
00:02:40,670 --> 00:02:46,400
so this is not actual code of course but it is good enough to explain my ideas.

37
00:02:46,400 --> 00:02:53,150
Now we will implement it in Javascript and the implementation of an algorithm in a concrete programming

38
00:02:53,150 --> 00:02:55,550
language is then called the program,

39
00:02:55,550 --> 00:03:01,730
so now we're going to write a program for this problem which basically implements this algorithm to

40
00:03:01,730 --> 00:03:05,510
hopefully give us a solution to find the minimum.

41
00:03:05,600 --> 00:03:12,890
Now for that, I'm back in a very simple new project with an empty app.js file and an index.html file

42
00:03:12,890 --> 00:03:19,310
where I only import this file and then my config files here and now here in app.js, I want to basically

43
00:03:19,310 --> 00:03:20,480
implement this algorithm.

44
00:03:20,600 --> 00:03:28,330
So actually I will rename this to alg-1-minimum and I will import this here

45
00:03:28,340 --> 00:03:34,770
then of course, so that's alg-1-minimum.js which I import here and now in alg-1-minimum.js, I want to

46
00:03:34,860 --> 00:03:37,660
write this logic I just described.

47
00:03:37,700 --> 00:03:42,350
So let's add a function, maybe call it getMin because that is what we'll do

48
00:03:42,500 --> 00:03:45,270
and in there we'll get our array of numbers,

49
00:03:45,320 --> 00:03:50,870
so I will name the parameter numbers and I want to return the minimum number.

50
00:03:51,020 --> 00:03:56,480
So for this, let's create a new variable, current minimum

51
00:04:00,820 --> 00:04:07,120
and we'll initialize it in a second and at the end, we'll return the current minimum and then we could

52
00:04:07,120 --> 00:04:18,200
simply have our test array here or our test numbers and that would be 3, 1, 2, that's our array and

53
00:04:18,200 --> 00:04:25,190
I want to get the minimum by calling getMin and passing in the test numbers and then if we console

54
00:04:25,250 --> 00:04:31,690
log min, that should be one of course for this provided array.

55
00:04:31,730 --> 00:04:36,170
So with that, let's go to our concrete implementation of this algorithm.

56
00:04:36,200 --> 00:04:43,460
Now as I sketched out before, I want to initialize current minimum with the first number I have in my

57
00:04:43,610 --> 00:04:44,780
array I get here,

58
00:04:44,840 --> 00:04:50,030
so that's simply my assumption because I have no better assumption initially, that the first number in

59
00:04:50,030 --> 00:04:55,280
the array, so the first number I get to know is my current minimum because it's the only number I know

60
00:04:55,280 --> 00:04:56,620
from the incoming array,

61
00:04:56,630 --> 00:05:00,340
so for the moment it is the smallest number I know from there.

62
00:05:00,350 --> 00:05:04,360
Now of course theoretically, numbers could be an empty array,

63
00:05:04,640 --> 00:05:13,390
so we could check if numbers length, if that is equal to zero, so if it is an empty array and if that

64
00:05:13,390 --> 00:05:18,440
is the case, we could throw an error if we don't want to support this,

65
00:05:18,520 --> 00:05:25,730
so we could throw a new error and say should not be an empty array,

66
00:05:25,810 --> 00:05:26,530
something like this,

67
00:05:26,530 --> 00:05:28,720
that's something we could do. For that 

68
00:05:28,740 --> 00:05:37,270
we can also check if not numbers length which does not just catch the length equals zero case but also

69
00:05:37,270 --> 00:05:38,980
the case that we have no length at all,

70
00:05:38,980 --> 00:05:41,380
so that we didn't even fit in an array

71
00:05:41,770 --> 00:05:48,130
and we can also check one other scenario, we check that numbers length is equal to 1 which means we have

72
00:05:48,130 --> 00:05:49,330
exactly one item,

73
00:05:49,570 --> 00:05:52,330
well if that's the case we don't need to check anything,

74
00:05:52,330 --> 00:05:55,360
then we can just return numbers

75
00:05:55,360 --> 00:05:56,720
and then the first item here

76
00:05:56,740 --> 00:06:02,560
because if it's an array with only one item in there, of course that only one item is the smallest item,

77
00:06:02,560 --> 00:06:03,460
right.

78
00:06:03,460 --> 00:06:06,590
So these are two special cases we can check upfront

79
00:06:06,610 --> 00:06:10,510
and now we can actually implement our concrete algorithm,

80
00:06:10,660 --> 00:06:13,180
so the heart of this task here.

81
00:06:13,330 --> 00:06:19,450
So we initialize current minimum with the first number we find in the array and then we need a for loop,

82
00:06:19,840 --> 00:06:27,670
a for loop where we go through all the items in the array, so where we have a variable, I and we actually

83
00:06:27,670 --> 00:06:34,910
start at a value of one, not at zero because I want to start at the second item with index one in the

84
00:06:34,920 --> 00:06:43,150
array because the first item is assigned as my current minimum here and then here, I keep on going as

85
00:06:43,150 --> 00:06:52,580
long as I is smaller than numbers length and I increment I by 1.

86
00:06:52,600 --> 00:06:59,730
So in the end here, I go through my numbers array as long as well I'm within the boundaries of it

87
00:06:59,730 --> 00:07:06,070
and since the index starts at zero and I want to start at the second element, I start with one and then

88
00:07:06,100 --> 00:07:11,470
I keep on going as long as I is smaller than my length because if it would be equal, it would exceed

89
00:07:11,710 --> 00:07:15,400
the array because as I mentioned, the index starts at zero.

90
00:07:16,330 --> 00:07:23,640
So now for example if we have this array which has three items, we would set current minimum equal to three,

91
00:07:23,680 --> 00:07:29,770
so the first element, then we would go into the loop where we start at index 1,

92
00:07:29,770 --> 00:07:34,150
so at this item here which also has a value of 1 coincidently here,

93
00:07:34,150 --> 00:07:42,670
so we start at this item and we execute whatever will be in here and then we'll increment i to 2 because

94
00:07:42,670 --> 00:07:43,390
we start at 1

95
00:07:43,390 --> 00:07:46,090
so we look at this item and execute this logic

96
00:07:46,090 --> 00:07:53,160
and now I is 2, the length is three so we execute this but now we thereafter increment i to three,

97
00:07:53,200 --> 00:07:55,150
now the length of our array is three,

98
00:07:55,150 --> 00:07:57,510
so this condition is no longer true,

99
00:07:57,520 --> 00:07:59,890
so we won't make it into here a third time

100
00:07:59,890 --> 00:08:01,360
which is exactly what we want,

101
00:08:01,360 --> 00:08:03,340
we don't want to make it there a third time,

102
00:08:03,370 --> 00:08:05,950
so we then return our current minimum.

103
00:08:05,950 --> 00:08:08,880
Now we just need to update the current minimum in here

104
00:08:09,100 --> 00:08:13,740
and for that we need to check if numbers for the current index,

105
00:08:13,740 --> 00:08:23,920
so for I, is smaller than the current minimum we saved because if that new number we're looking at for the index

106
00:08:23,930 --> 00:08:31,130
i, if that is smaller, then of course I want to update current minimum and set it to numbers I, otherwise

107
00:08:31,130 --> 00:08:36,920
if it's not smaller, I will just do nothing and continue to the next loop iteration and then by the end,

108
00:08:36,950 --> 00:08:43,020
we should have a current minimum and that should already be all here.

109
00:08:43,130 --> 00:08:49,340
Now technically, we can now also remove this check up here because if we take a closer look at our algorithm,

110
00:08:49,700 --> 00:08:56,530
we see that we set the current minimum to our first number here and if we only have one element in the

111
00:08:56,530 --> 00:09:01,640
array, we won't make it into this for loop anyways, so this check is a bit redundant and we can just

112
00:09:01,640 --> 00:09:04,820
keep the first check in case we don't get an array.

113
00:09:05,090 --> 00:09:07,330
So that could be our final algorithm

114
00:09:07,580 --> 00:09:09,300
and now let's simply try it.

115
00:09:09,370 --> 00:09:11,340
I'm importing it in index.html

116
00:09:11,360 --> 00:09:19,160
so let's simply load this page in the browser and reload it and I get a value of 1 here for every reload

117
00:09:19,190 --> 00:09:21,150
which is exactly what I want.

118
00:09:21,170 --> 00:09:23,990
So it seems to work but let's verify it,

119
00:09:24,050 --> 00:09:26,200
let's also try some other values.

120
00:09:26,240 --> 00:09:32,230
Let's say I only have one element, three. If I now save this, I get three.

121
00:09:32,450 --> 00:09:38,260
Now if I feed in an empty array, again an error, should not be an empty array,

122
00:09:38,310 --> 00:09:39,660
so that's also good.

123
00:09:39,660 --> 00:09:42,930
Now what if I have 1, 5, 1?

124
00:09:42,980 --> 00:09:46,110
So if I have a smallest value twice?

125
00:09:46,110 --> 00:09:49,120
Well I still get one as a result here.

126
00:09:49,200 --> 00:09:56,570
Now what if I feed in 10, -5, -5,

127
00:09:56,620 --> 00:10:05,700
100, 99, -2? I get -5 which is the smallest value of course.

128
00:10:05,720 --> 00:10:07,820
So that seems to work,

129
00:10:07,820 --> 00:10:12,350
looks like we found the algorithm that does exactly what we want here.

130
00:10:12,350 --> 00:10:14,840
Now, is this the best solution though?

131
00:10:14,840 --> 00:10:17,140
Are there other ways of solving this

132
00:10:17,180 --> 00:10:20,180
and how could we then compare these different solutions?
