Problem
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:
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.
How I solved the problem
# key word : Bubble Sort(버블정렬)
- 앞의 요소와 그 다음 요소를 비교하여 그 다음 요소보다 앞의 요소가 더 클 경우 swap이 일어남.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; 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 int swapCount = 0; // swap 횟수 for (int i = 0; i < n; i++) { for (int j = 0; j < n - (i + 1); j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; swapCount++; } } } System.out.println("Array is sorted in " + swapCount + " swaps."); System.out.println("First Element: " + a[0]); System.out.println("Last Element: " + a[n-1]); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.01.11] Binary Search Trees (0) | 2019.01.11 |
---|---|
[2019.01.10] Generics (0) | 2019.01.10 |
[2019.01.08] Interfaces (0) | 2019.01.08 |
[2019.01.07] Queues and Stacks (0) | 2019.01.07 |
[2019.01.04] More Exceptions (0) | 2019.01.04 |
댓글