Now that you understand the basics of sorting, it’s time we move on to our first sorting algorithm — the Bubble sort. Let’s get going.
In this video, you learnt how the Bubble sort algorithm works. You started with the first two numbers in each iteration and did a comparison between them. When the numbers were not in order, you swapped them. The same steps were repeated for the second and third number, and so on. At the end of the first iteration, the largest number was pushed to the end of the array; at the end of the second iteration, the second largest number was pushed to the second last position, and so on. In the next video, you will learn how to write the pseudocode for Bubble sort algorithm.
Now that you have understood how to write the pseudocode for Bubble sort algorithm, let's move on to the next segment where you will see the Java implementation of the algorithm.
An interesting fact to note is that swapping requires three statements similar to that of copying. For example, in swapping:
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
while to copy:
array[j]=array[j+1];
This makes copying an element 3 times faster than swapping the same element.