Sorting: Bubble Sort
Consider the following version of Bubble Sort:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
// Swap adjacent elements if they are in decreasing order
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
}
}
}
Task
Given an -element array, , of distinct elements, sort array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three lines:
Array is sorted in numSwaps swaps.
, where is the number of swaps that took place.First Element: firstElement
, where is the first element in the sorted array.Last Element: lastElement
, where is the last element in the sorted array.
Hint: To complete this challenge, you must add a variable that keeps a running tally of all swaps that occur during execution.
Input Format
The first line contains an integer, , denoting the number of elements in array .
The second line contains space-separated integers describing the respective values of .
Constraints
- ,
Output Format
You must print the following three lines of output:
Array is sorted in numSwaps swaps.
, where is the number of swaps that took place.First Element: firstElement
, where is the first element in the sorted array.Last Element: lastElement
, where is the last element in the sorted array.
Sample Input 0
3
1 2 3
Sample Output 0
Array is sorted in 0 swaps.
First Element: 1
Last Element: 3
Explanation 0
The array is already sorted, so swaps take place and we print the necessary three lines of output shown above.
Sample Input 1
3
3 2 1
Sample Output 1
Array is sorted in 3 swaps.
First Element: 1
Last Element: 3
Explanation 1
The array is not sorted, and its initial values are: . The following swaps take place:
At this point the array is sorted and we print the necessary three lines of output shown above.
풀이
기본적으로 버블정렬에 대해 알고 있다면 쉽게 풀 수 있을 것이다.
시간복잡도가 O(n^2) 이다보니 실제 잘 사용하는 정렬은 아니나, 쓰기 매우 간편하다는 장점이 있다.
package algorithms;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int count = 0;
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a.length - 1; j++){
if(a[j] > a[j+1]){
int tmp = a[j+1];
a[j+1] = a[j];
a[j] = tmp;
count++;
}
}
}
System.out.println("Array is sorted in " + count + " swaps.");
System.out.println("First Element: " + a[0]);
System.out.println("Last Element: " + a[a.length-1]);
}
}