Consider the following version of Bubble Sort:
for (int i = 0; i < n; i++) {
// Track number of elements swapped during a single array traversal
int numberOfSwaps = 0;
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]);
numberOfSwaps++;
}
}
// If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break;
}
}
Task
Given an array, , of size distinct elements, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following 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 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, , denoting the number of elements in array .
The second line contains space-separated integers describing the respective values of .
Constraints
- , where .
Output Format
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 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, so we perform the following swaps:
At this point the array is sorted and we print the necessary lines of output shown above.
풀이
중간에 전부 sort 된게 맞으면 break 하는게 더 좋은 로직일 듯 하다
난 numberOfSwaps 하나만 넣었는데 체크용으로 int 하나 더 있는게 좋을 듯
package Day20;
import java.util.Scanner;
public class Solution {
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();
}
// Write Your Code Here
Sorting sorting = new Sorting(a);
int swapCount = sorting.bubbleSorting();
System.out.println("Array is sorted in " + String.valueOf(swapCount) +" swaps.");
System.out.println("First Element: " + sorting.getFirstElement());
System.out.println("Last Element: " + sorting.getLastElement());
}
}
class Sorting {
private int[] array;
Sorting(int[] array) {
this.array = array;
}
Integer bubbleSorting() {
int numberOfSwaps = 0;
for (int i = 0; i < array.length; i++) {
// Track number of elements swapped during a single array traversal
for (int j = 0; j < array.length - 1; j++) {
// Swap adjacent elements if they are in decreasing order
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
numberOfSwaps++;
}
}
// If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break;
}
}
return numberOfSwaps;
}
Integer getFirstElement() {
return array[0];
}
Integer getLastElement() {
return array[array.length - 1];
}
}
'Algorithms > 30 Days of Code' 카테고리의 다른 글
Day 23: BST Level-Order Traversal (0) | 2018.03.08 |
---|---|
Day 21: Generics (0) | 2018.03.08 |
Day 19: Interfaces (0) | 2018.02.27 |
Day 18: Queues and Stacks (0) | 2018.02.27 |
Day 17: More Exceptions (0) | 2018.02.27 |