By the above recursive argument, this second largest array will then reach the last position in the remaining array [1, 2, 3, 4]. If first element is greater than the second, then swapped will be performed otherwise not.
For example, if we have total of 10 elements in the arraythen we have to perform swapping 10 times as shown in the program given here. C program to test if a number is a power of 2 C program to sort array using bubble sort algorithm Sorting data either in ascending or descending order is the important computing application that can be achieved by various ways.
So, at the end of the first pass, the largest element will always reach its correct position. Sorting forms a great case study for those who want to learn Data Structures and Algorithms. In function, we have used same technique as discussed above, that is comparing each consecutive elements from starting to ending.
Basically, we need to find a way to sort this array so that the final array obtained is [1, 2, 3, 4, 5]. View all posts by Aman Goel Related Posts. Therefore, it will take a lot of iterations for the algorithm to complete.
Now again, 4, 3 is incorrect so we do another swap and get [1, 2, 3, 4, 5].