Sort given array using bubble sort.
Example 1:
Input:
N=6
arr[] = {3, 2, 9 , 1, 15, 10}
Output: {1, 2, 3, 9, 10, 15}
Example 2:
Input:
N=4
arr[] = {15. -2, 1, 18}
Output: {-2, 1, 15, 18}
1. BubbleSort function:-This is the implementation of the Bubble Sort algorithm.
The bubblesort function takes an integer array arr[] and its size n as parameters.
It uses two nested loops to compare adjacent elements in the array and swap them if they are in the wrong
order.
The outer loop (i) iterates from 0 to n-2, and the inner loop (j) iterates from 0 to n-i-2.
If the element at arr[j] is greater than the element at arr[j+1], they are swapped.
2. Main function:-
In the main function:
It takes an integer n as input, which represents the number of elements in the array.
It declares an integer array arr[] of size n to store the input elements.
It reads n integers from the standard input (usually the keyboard) and stores them in the array arr[].
It then calls the bubblesort function to sort the array in ascending order.
Finally, it prints the sorted array elements separated by spaces.
3. Bubble Sort Execution:-
The bubblesort function is called to sort the array arr[] in ascending order using the Bubble Sort algorithm.
Bubble Sort involves comparing adjacent elements and swapping them if necessary, repeatedly until the entire array is sorted.
4. Output:-
Finally, After sorting is completed, the main function prints the sorted array elements separated by spaces.