Objective
Today, we’re discussing a simple sorting algorithm called Bubble Sort. Check out the Tutorial tab for learning materials and an instructional video!
Consider the following version of Bubble Sort:
1 | for (int i = 0; i < n; i++) { |
Task
Given an array, a, of size n distinct elements, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following 3 lines:
- Array is sorted in numSwaps swaps.
where numSwaps is the number of swaps that took place. - First Element: firstElement
where firstElement is the first element in the sorted array. - Last Element: lastElement
where lastElement is the last element in the sorted array.
Hint: To complete this challenge, you will need to add a variable that keeps a running tally of all swaps that occur during execution.
Input Format
The first line contains an integer, n, denoting the number of elements in array a. The second line contains n space-separated integers describing the respective values of a0, a1, …, an-1.
Constraints
- 2 <= n <= 600
- 1 <= ai <= 2 X 106, where 0 <= i < n
Output Format
Print the following three lines of output:
- Array is sorted in numSwaps swaps.
where numSwaps is the number of swaps that took place. - First Element: firstElement
where firstElement is the first element in the sorted array. - Last Element: lastElement
where lastElements is the last element in the sorted array.
Sample Input 0
1 | 3 |
Sample Output 0
1 | Array is sorted in 0 swaps. |
Explanation 0
The array is already sorted, so 0 swaps take place and we print the necessary 3 lines of output shown above.
Sample Input 1
1 | 3 |
Sample Output 1
1 | Array is sorted in 3 swaps. |
Explanation 1
The array a = [3, 2, 1] is not sorted, so we perform the following swaps:
- [3, 2, 1] -> [2, 3, 1]
- [2, 3, 1] -> [2, 1, 3]
- [2, 1, 3] -> [1, 2, 3]
At this point the array is sorted and we print the necessary 3 lines of output shown above.
Solution
1 | function main() { |