Array ( [0] => [1] => questions [2] => HackwithInfy-Previous-Questions [3] => Bubble-Sort )
Given a list of integers and a integer (size of array). Sort the given array using Bubble Sort Algorithm.
First line contains the size of array (n) followed by n integers separated by spaces.
Print the sorted array separated by spaces.
5 5 4 3 2 1
1 2 3 4 5
6 96 54 100 32 45 64
32 45 54 64 96 100
7 9 5 4 7 5 2 4
2 4 4 5 5 7 9
Login to see Discussion
Step-i) Starting from the first element of the array, compare the current element with the next element.
Step-ii) If the current element is greater than the next element, swap them else move to the next element.
Step-iii) Repeat the above steps.
Time Complexity: O(n^2)
Space Complexity: O(1)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution