Bubble Sort is a simple sorting algorithm where each element is compared with its adjacent element and swapped if they are in the wrong order. This process is repeated until the list is sorted.
Array before sorting: [5, 3, 8, 4, 2]
Time Complexity:
Space Complexity: O(1) - only requires a constant amount of additional space.
The following is the pseudo code for Bubble Sort:
Begin
for each i from 1 to n
for each j from 1 to n-i
if arr[j] > arr[j+1]
swap(arr[j], arr[j+1])
End
for(int i = 0; i < n-1; i++) { for(int j = 0; j < n-i-1; j++) { if(arr[j] > arr[j+1]) { // Swap arr[j] and arr[j+1] int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } }