1
00:00:02,160 --> 00:00:04,890
Let's try to implement this in another way.

2
00:00:04,890 --> 00:00:09,450
For this, I'll add a new function, getMin2 and there,

3
00:00:09,450 --> 00:00:16,740
I also want to get my numbers and I also will run this check here and throw an error if it's an invalid

4
00:00:16,770 --> 00:00:21,770
input but now let's see if we can solve this differently.

5
00:00:21,770 --> 00:00:26,480
I think our solution here is not that bad but still let's see if we could find a different solution.

6
00:00:26,510 --> 00:00:28,230
Now what could be a different solution?

7
00:00:29,150 --> 00:00:36,080
Well an alternative could be that we try to sort this array we're getting and if we sort it and we

8
00:00:36,080 --> 00:00:43,370
know that we have the smallest item at the beginning, then we could just return the first item in the

9
00:00:43,370 --> 00:00:43,730
array,

10
00:00:43,730 --> 00:00:45,350
we just have to sort it.

11
00:00:45,830 --> 00:00:52,620
So we could return sorted numbers and there, simply the first item

12
00:00:52,630 --> 00:00:58,030
if we sort it in an ascending order, so if we have the smallest item first and then the biggest item at

13
00:00:58,030 --> 00:00:59,110
the end.

14
00:00:59,200 --> 00:01:03,010
Now we just need a sorted numbers variable here

15
00:01:03,040 --> 00:01:06,490
and we now need to do the actual sorting.

16
00:01:06,520 --> 00:01:13,810
Now how can we sort an array? Now of course we could do it quickly by calling the sort function which

17
00:01:13,810 --> 00:01:15,620
is built into Javascript

18
00:01:15,760 --> 00:01:21,680
but actually let's write our own sorting algorithm which is also something we can do.

19
00:01:21,730 --> 00:01:25,680
Now how can we sort our numbers here?

20
00:01:25,720 --> 00:01:33,070
Well we can, again, write a for loop here where we now actually start at index 0 and we keep on going

21
00:01:33,070 --> 00:01:42,120
as long as I is smaller than numbers length and we then increment it and now inside of that loop, we have

22
00:01:42,120 --> 00:01:48,390
another loop because I want to go through all items in my array but then for every item, I want to go

23
00:01:48,390 --> 00:01:55,290
through all other items in this array and check if there is a smaller item than the one I'm currently

24
00:01:55,290 --> 00:01:55,830
looking at

25
00:01:55,830 --> 00:02:02,700
and this will allow me to sort this. So we can add another loop here, where we maybe use J instead of

26
00:02:02,730 --> 00:02:03,210
I

27
00:02:03,390 --> 00:02:04,560
because we can't reuse

28
00:02:04,570 --> 00:02:12,880
I here, we're already using that and I set this equal to i+1, so that I start at the next item

29
00:02:13,030 --> 00:02:24,220
after the item we're looking at in this outer loop here and then we check if J is smaller than numbers

30
00:02:24,340 --> 00:02:30,970
length because this hasn't changed, we still don't want to exceed our array boundaries and we increment

31
00:02:31,000 --> 00:02:33,270
J.

32
00:02:33,470 --> 00:02:41,000
Now we just need to do something before we enter this inner loop, we save an outer element here and I

33
00:02:41,000 --> 00:02:49,360
set this to numbers for the index i, so the element I'm currently looking at in the outer loop and

34
00:02:49,360 --> 00:02:59,360
and in here, we have an inner element in this inner loop where we simply take numbers for J so that we have the

35
00:02:59,360 --> 00:03:05,340
concrete elements we're comparing - the one in the outer loop and the one in the inner loop and then we

36
00:03:05,340 --> 00:03:13,560
need an if check in here where we check if the outer element is greater than the inner element and if it

37
00:03:13,680 --> 00:03:20,370
is greater, then we know okay in the array we're going through, so in this array for example, in the first iteration

38
00:03:20,370 --> 00:03:25,650
where this would be the outer and this would be the inner element, we know the outer element is bigger

39
00:03:25,650 --> 00:03:32,910
than the inner element, we need to swap positions, we need to replace three with one and one with three,

40
00:03:32,910 --> 00:03:37,090
we need to swap these two so that we order this in an ascending way here.

41
00:03:37,500 --> 00:03:48,180
So we need to do a swap here by setting numbers for i, so the index of the outer element equal to

42
00:03:48,180 --> 00:03:54,630
the inner element, so we swap the outer element with the inner element and of course that also means

43
00:03:54,630 --> 00:04:01,780
that we have to set number j, so the inner elements position equal to the outer element.

44
00:04:01,800 --> 00:04:04,340
This simply swaps the two elements in the array,

45
00:04:04,410 --> 00:04:12,210
so this array for the first iteration would now swap three with one for example, so that after the swap,

46
00:04:12,450 --> 00:04:20,880
we would have 1, 3 and then 2, this would now be our current array for the first iteration.

47
00:04:21,000 --> 00:04:28,110
Now we also need to update our outer and inner element references because they're now wrong and now

48
00:04:28,110 --> 00:04:38,400
we set outer element equal to numbers for i and inner element equal to numbers for j

49
00:04:38,400 --> 00:04:43,860
because since we swapped the elements here in their position in the array, we now need to update these

50
00:04:43,860 --> 00:04:45,380
variables again to be correct

51
00:04:45,510 --> 00:04:53,640
so that after the swap, we as I just said had 1, 3, 2 but before these two lines, outer element would

52
00:04:53,640 --> 00:04:56,970
of course still be equal to three,

53
00:04:56,970 --> 00:05:01,140
so to our first element and inner element would still be equal to 1,

54
00:05:01,140 --> 00:05:07,710
well now we need to update this after the swap so that thereafter, outer element is 1 and inner

55
00:05:07,710 --> 00:05:11,490
element is three, that's what we're doing here.

56
00:05:12,700 --> 00:05:18,130
And by the way if this is confusing at any point, you can simply add a console log statement here and

57
00:05:18,130 --> 00:05:25,650
console log numbers i, numbers j

58
00:05:25,820 --> 00:05:30,630
and then outer and inner element to always keep track of what's where.

59
00:05:30,680 --> 00:05:39,170
This however will be the logic we need to update the numbers we're getting and to make sure that we

60
00:05:39,170 --> 00:05:42,560
actually have a sorted list.

61
00:05:42,560 --> 00:05:48,560
So what I'll actually do now is I'll get rid of the sorted numbers array because I just used the numbers

62
00:05:48,560 --> 00:05:53,550
array we got as an input here to sort it and hence here I will just return numbers

63
00:05:53,570 --> 00:05:55,890
and then the first element here.

64
00:05:55,910 --> 00:06:01,760
So now we have a getMin algorithm which is of course longer if we compare it to the first one where we

65
00:06:01,760 --> 00:06:07,700
simply sort our list and then return the first element which has to be the smallest element since we

66
00:06:07,700 --> 00:06:11,470
sorted the list in an ascending way with this logic here.

67
00:06:11,510 --> 00:06:13,620
So let's see if that works.

68
00:06:13,790 --> 00:06:17,160
If I try running this with getMin2,

69
00:06:17,180 --> 00:06:18,890
so with this new algorithm,

70
00:06:18,890 --> 00:06:24,310
let's see if we still get one. If I reload here, I get one.

71
00:06:24,310 --> 00:06:25,960
Now let's try this differently,

72
00:06:25,960 --> 00:06:31,150
let's say my algorithm and my array here is this one

73
00:06:34,800 --> 00:06:36,040
and let's see if that works,

74
00:06:36,060 --> 00:06:39,160
we should get -10 now,

75
00:06:39,230 --> 00:06:40,070
that looks good to me,

76
00:06:40,070 --> 00:06:41,750
this is -10.

77
00:06:41,780 --> 00:06:49,530
So what if I had just one element in there? That also works and gives me that one element

78
00:06:49,560 --> 00:06:55,950
and what if I have an array that looks something like this? Well, still works.

79
00:06:56,020 --> 00:07:05,680
So it seems like this also does the trick now and it gives us the smallest number. Now which solution

80
00:07:05,680 --> 00:07:09,340
is better then? This one or this one?

81
00:07:09,580 --> 00:07:14,110
Of course we have two different approaches here and that's quite common in programming,

82
00:07:14,110 --> 00:07:18,070
often you can find more than one solution for a given problem

83
00:07:18,070 --> 00:07:22,010
and now we need to compare solutions to find out which one is better

84
00:07:22,060 --> 00:07:24,190
but how should we compare these two solutions?
