00:01
This question asks us to use python to write a function to basically sort an array using bubble sort and return the sorted array.
00:08
So in python, we have a depth, and then let's say we want bubble sort, and we pass in an array.
00:19
So what we're going to do here is we're going to get the length of the array.
00:23
N equals length of the array.
00:27
This is going to help us write our four loops.
00:29
So in bubble sort, the basic concept in bubble sort is we have two four loops.
00:33
To traverse through all array elements and then compare them to each array element that are already in place, right, the last i elements, right? so we're going to go through all of them for i in range, n -1, that's a length.
00:52
And then we've got to repeat for every i element that is already in place, every j element to compare to, for j -in -range 0 to, and minus i minus uh one and what this does is it goes from every single um i element through every single j element so every single i element will go through the entire array and every single j element will go from every array that's are sorry uh from every element that's already sorted onto the end of the array so or actually in our case it'll go from zero to the first element that's not sorted so it'll go that way.
01:38
So we're sorting from right to left instead of left to right.
01:41
So now that we have that, we're going to traverse the array from 0 to n minus 1 minus i and then swap it if it's greater than the next element.
01:48
So if array j is greater than array j plus 1.
01:57
So if the one before is greater than the one above, we're going to swap them.
02:02
So array j and array j plus 1 are going to be equal to array j plus 1 and array j.
02:15
So what we've done is we've put j plus 1 where j is and then j where j plus 1 is.
02:22
So now that we have this, it's going to return, well essentially it doesn't have to return anything.
02:30
Array will be accessible from outside...