Descending order: descending order is the exact opposite of ascending order. Here is the first screenshot of first sample run: And here is the second screenshot of first sample run: Main Steps involved in above Program All the steps are same as discussed in above one except function.
By the above recursive argument, this second largest array will then reach the last position in the remaining array [1, 2, 3, 4]. For example, if total number of element is 10, then i is initialized to 8, and in second loop, j is initialized with 0.
We repeat this until the array is sorted. It forms an interesting example of how simple computations can be used to perform more complex tasks.
This is because this largest element will always break the desired order. This is undesirable. Therefore, we swap 4 and 2 to get [1, 2, 4, 3, 5]. If first is greater than second then swap, if second is greater than third then swap and so on.
Here, the largest element in the rest of the array which is 4 will be nothing but the second largest element in the array. There are some better algorithms like merge sort in Cetc that take O nlog2n iterations. Conclusion Bubble sort is a fairly simple algorithm.