1
00:00:00,700 --> 00:00:03,160
Hi everyone, let's do this question.

2
00:00:03,160 --> 00:00:05,200
You are given an array of integers.

3
00:00:05,200 --> 00:00:12,070
Write a function that will take this array as input and return the sorted array using bubble sort.

4
00:00:12,070 --> 00:00:14,260
So this question is a standard question.

5
00:00:14,260 --> 00:00:15,430
It's about bubble sort.

6
00:00:15,460 --> 00:00:22,630
Now bubble sort is an algorithm where we will keep comparing values a particular value and the next

7
00:00:22,630 --> 00:00:22,900
value.

8
00:00:22,900 --> 00:00:27,640
For example, let's say that the given array is this one.

9
00:00:28,030 --> 00:00:30,130
Let's say it's uh this one okay.

10
00:00:30,130 --> 00:00:34,420
So what we'll do is we'll keep comparing a particular element and the next one.

11
00:00:34,420 --> 00:00:39,100
Now if the order is not correct, that is if it's not in ascending order we will swap.

12
00:00:39,100 --> 00:00:40,330
Otherwise we will move forward.

13
00:00:40,330 --> 00:00:43,180
So at this point the pointer would be over here.

14
00:00:43,180 --> 00:00:44,740
And we'll check this value.

15
00:00:44,740 --> 00:00:45,670
And the next value.

16
00:00:45,670 --> 00:00:46,840
Now there is no problem.

17
00:00:46,840 --> 00:00:47,890
It's in the correct order.

18
00:00:47,890 --> 00:00:50,440
So we will move this pointer to this one.

19
00:00:50,440 --> 00:00:52,780
And we'll check this and the next value.

20
00:00:52,780 --> 00:00:55,180
So over here we can see that the order is not correct.

21
00:00:55,180 --> 00:00:56,260
So we will swap them.

22
00:00:56,260 --> 00:00:59,710
So this will become 213 and then five.

23
00:00:59,710 --> 00:01:01,210
And we will move the pointer.

24
00:01:01,210 --> 00:01:03,940
So over here the pointer was at index one.

25
00:01:03,940 --> 00:01:04,120
Right.

26
00:01:04,120 --> 00:01:05,830
So we will move the pointer to index two.

27
00:01:05,830 --> 00:01:07,120
And we'll check these two.

28
00:01:07,120 --> 00:01:08,230
Now it's in order.

29
00:01:08,230 --> 00:01:09,490
So we don't do anything.

30
00:01:09,490 --> 00:01:12,190
Now we keep doing this is this is called one pass.

31
00:01:12,190 --> 00:01:13,540
So we have done one pass.

32
00:01:13,540 --> 00:01:19,180
And we'll do uh many passes until the given array of integers is sorted.

33
00:01:19,180 --> 00:01:20,260
So that's the question.

34
00:01:20,260 --> 00:01:24,850
Now let's go ahead and look at a few test cases to understand this in a better manner.

35
00:01:24,850 --> 00:01:27,580
Now let's say the input is 1324.

36
00:01:27,610 --> 00:01:29,740
Then the output has to be 123, four.

37
00:01:29,770 --> 00:01:30,670
It's just sorted.

38
00:01:30,670 --> 00:01:34,630
Now what if the input is a single integer?

39
00:01:34,630 --> 00:01:39,430
If the array has just one integer, we just give that particular integer as output.

40
00:01:39,430 --> 00:01:39,820
Right?

41
00:01:39,820 --> 00:01:46,750
And if the input integer is already sorted, then again we just give the that particular array as output.

42
00:01:46,750 --> 00:01:50,920
Alright, now let's go ahead and see how we can implement this.

43
00:01:50,920 --> 00:01:53,140
Let's discuss the logic of the bubble sort.

44
00:01:53,140 --> 00:01:55,570
And after that we will go ahead and code it together.
