1 00:00:01,680 --> 00:00:03,300 Hey, guys, what's up? 2 00:00:03,840 --> 00:00:06,780 So in the last video, we talked about Bubble. 3 00:00:08,020 --> 00:00:14,530 And I told you that the Bulls takes and square a number of steps to sort our Eddie. 4 00:00:15,120 --> 00:00:22,890 OK, so, for example, if my input is, let's say three, four, seven, one, 10. 5 00:00:23,430 --> 00:00:28,450 So if my input is this, how many number of steps it will take, it will take and square a number of 6 00:00:28,540 --> 00:00:29,020 steps. 7 00:00:30,640 --> 00:00:42,430 OK, so if my ETA is one, three, four, seven and a 10, so if my input is this, then also it will 8 00:00:42,430 --> 00:00:44,500 take into a number of steps. 9 00:00:45,340 --> 00:00:49,060 OK, so this area this area is unsorted, Eddie. 10 00:00:50,630 --> 00:00:56,190 This ad is unsorted today, then it is taking any number of steps. 11 00:00:57,540 --> 00:01:08,190 This area is already sorted, this area is already sorted, then also it is taking and a number of steps 12 00:01:08,700 --> 00:01:11,210 to sort this area, which is already certain. 13 00:01:11,940 --> 00:01:19,950 OK, so if I give this as input, maybe it will take and number of steps if I give this as in, but 14 00:01:20,220 --> 00:01:23,470 then also bolster this digging into a number of steps. 15 00:01:24,180 --> 00:01:27,030 So our aim is to optimise. 16 00:01:27,950 --> 00:01:30,860 Our aim is to optimize bubbles out. 17 00:01:32,920 --> 00:01:42,460 So that if the input is unsorted, it can take any number of steps, but if the error is already sorted, 18 00:01:42,670 --> 00:01:44,560 it will take and steps. 19 00:01:48,580 --> 00:01:50,620 OK, so we want to achieve this. 20 00:01:53,360 --> 00:02:01,040 So what we want we want to optimize our budget so that if the given it is already sorted, then it should 21 00:02:01,040 --> 00:02:02,480 take any steps. 22 00:02:04,200 --> 00:02:06,460 So how we can optimize our budget. 23 00:02:07,790 --> 00:02:09,380 So let's take an example. 24 00:02:09,500 --> 00:02:17,690 Let us try to understand with the help of an example, so suppose the given area is two, four, seven, 25 00:02:18,080 --> 00:02:20,230 eight, nine and 12. 26 00:02:20,960 --> 00:02:23,610 So the given area is already sorted. 27 00:02:24,590 --> 00:02:29,910 OK, this given that is already sorted, let us implement bubbles out on it. 28 00:02:30,020 --> 00:02:30,470 So. 29 00:02:31,750 --> 00:02:34,300 Compared no slapping. 30 00:02:35,960 --> 00:02:39,920 No sweat compared, no sweat. 31 00:02:42,080 --> 00:02:43,850 Compare notes, web. 32 00:02:46,700 --> 00:02:48,140 Compare notes app. 33 00:02:50,450 --> 00:02:52,240 Compare Nosef. 34 00:02:53,900 --> 00:03:00,680 That means if the given area is already sorted, then no slapping will take place. 35 00:03:00,710 --> 00:03:02,980 OK, so I will not be there. 36 00:03:03,530 --> 00:03:08,260 Slapping will not be there. 37 00:03:10,050 --> 00:03:12,360 OK, so we can use this property. 38 00:03:13,400 --> 00:03:20,630 OK, we will use it, we will use it that if the given array is already sorted, if the given that is 39 00:03:20,630 --> 00:03:24,370 only distorted, then something will not be there. 40 00:03:24,800 --> 00:03:34,460 We will use this so that our sorted so that if the input is already sorted, then bubble start, take 41 00:03:34,640 --> 00:03:35,810 and steps. 42 00:03:38,340 --> 00:03:42,030 OK, so how we will do it, so let's see. 43 00:03:44,950 --> 00:03:47,680 So let's nameless, faceless, optimized bubble. 44 00:03:50,700 --> 00:03:58,380 Optimized bubble bubble are large c.p. 45 00:04:01,010 --> 00:04:06,370 So what I will do, so this was our old heart, so we will cooperate. 46 00:04:08,160 --> 00:04:09,900 And Bastad here. 47 00:04:11,710 --> 00:04:13,420 So what optimization we have to do? 48 00:04:15,420 --> 00:04:18,880 So this swab thing, we have to optimize this, so what I will do? 49 00:04:19,800 --> 00:04:25,200 Let us take a variable and flag. 50 00:04:26,310 --> 00:04:29,800 So I'm taking a variable and flag, which is initially zero. 51 00:04:30,610 --> 00:04:34,450 OK, so I am taking our flag variable, which is initially zero. 52 00:04:34,830 --> 00:04:36,690 Now I will enter this loop. 53 00:04:37,760 --> 00:04:46,430 OK, so in this loop, so if there is swiping what I will do, I will change the value of flag to one. 54 00:04:47,670 --> 00:04:55,380 OK, so that means if the intruder is unsorted, this condition will be true at least once, this condition 55 00:04:55,380 --> 00:04:58,500 will be true and and these two men should be executed. 56 00:04:59,370 --> 00:05:03,500 So when this for Loop and I will check the value of flag. 57 00:05:03,810 --> 00:05:13,290 So if flag is still zero, if flag is still zero, that means the is already started. 58 00:05:13,590 --> 00:05:15,350 So what you can do is break. 59 00:05:18,510 --> 00:05:22,090 So these three lines, initially my flag also zero. 60 00:05:22,470 --> 00:05:26,390 So if that is sorted, this condition will always be false. 61 00:05:27,410 --> 00:05:36,650 OK, if that is sorted, this condition will always be false, hence these two lines will never be executed 62 00:05:37,580 --> 00:05:43,860 and this fall will terminate, then I'm checking if the value of flag is still zero. 63 00:05:44,390 --> 00:05:46,430 That means you can right here. 64 00:05:47,060 --> 00:05:54,600 That means my error is that it is already sorted. 65 00:05:56,060 --> 00:05:58,460 So what you should do is come out of the loop. 66 00:05:59,510 --> 00:06:01,910 OK, so come out. 67 00:06:02,660 --> 00:06:05,590 Come out of the loop. 68 00:06:06,230 --> 00:06:11,610 OK, so what this extraordinary do so this public statement will break this outlook. 69 00:06:12,080 --> 00:06:15,260 OK, so this was closing and this was happening. 70 00:06:15,530 --> 00:06:18,550 So this big statement will break this loop. 71 00:06:18,770 --> 00:06:26,120 I will come out of this loop and this is where my function and OK, so this is all that we have to do. 72 00:06:27,100 --> 00:06:29,350 Now, this is called optimized mobile art. 73 00:06:30,780 --> 00:06:34,380 OK, it will take any steps, vital taken steps. 74 00:06:36,170 --> 00:06:44,650 So my flag is zero, so let's say that elements are one, two and three, so I will check JMJ Listen, 75 00:06:44,900 --> 00:06:48,530 compare no slapping compar, no slapping. 76 00:06:48,860 --> 00:06:50,630 So I will reach this lame flag. 77 00:06:50,630 --> 00:06:51,710 Flaggers still zero. 78 00:06:52,000 --> 00:06:54,740 So flaggers still zero break. 79 00:06:54,890 --> 00:06:56,000 Come out of this loop. 80 00:06:56,600 --> 00:06:57,750 Come out of this loop. 81 00:06:58,040 --> 00:06:59,990 So how many number of steps it took. 82 00:07:01,280 --> 00:07:07,550 As many number of times as this loop is running, so this loop is running and times. 83 00:07:09,470 --> 00:07:14,790 OK, it is actually raining and minus two times or minus one times, but it doesn't matter. 84 00:07:15,700 --> 00:07:19,660 OK, this constant one, two, three, there doesn't matter. 85 00:07:19,670 --> 00:07:21,530 We always report our answer in terms of. 86 00:07:21,530 --> 00:07:24,820 And so the slope is ending and times. 87 00:07:25,070 --> 00:07:31,530 So this optimized budget will take and steps if that is already sorted out. 88 00:07:31,890 --> 00:07:35,510 OK, so the main logic was just to use a variable flag. 89 00:07:36,140 --> 00:07:39,050 If there's a spring that means that is unsorted. 90 00:07:40,140 --> 00:07:45,690 That means there is unsorted and the value of flag will change, the value of flag will become one. 91 00:07:46,840 --> 00:07:48,620 And this condition will become false. 92 00:07:49,150 --> 00:07:50,980 So Blake will not be executed. 93 00:07:51,500 --> 00:07:53,830 OK, so this was the whole logic. 94 00:07:55,680 --> 00:08:02,400 OK, so if you have any doubt regarding the optimized verbal assault or regarding the Bolstad, you 95 00:08:02,400 --> 00:08:04,740 can ask me, OK, feel free to ask. 96 00:08:05,040 --> 00:08:06,420 So this is all for this video. 97 00:08:06,810 --> 00:08:07,350 Thank you.