forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_Sorting.java
More file actions
40 lines (36 loc) · 1.31 KB
/
20_Sorting.java
File metadata and controls
40 lines (36 loc) · 1.31 KB
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
38
39
40
/**
* Given an array, a, of size distinct elements, sort the array in ascending order using the Bubble Sort algorithm above.
* Once sorted, print the following 3 lines:
* 1. Array is sorted in numSwaps swaps where numSwaps is the number of swaps that took place.
* 2. First Element: firstElement where firstElement is the first element in the sorted array.
* 3. Last Element: lastElement where lastElement is the last element in the sorted array.
*/
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 i=0; i < n; i++){
a[i] = in.nextInt();
}
int swaps = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n - 1; j++){
if(a[j] > a[j + 1]){
int t = a[j + 1];
a[j + 1] = a[j];
a[j] = t;
swaps++;
}
}
}
System.out.println("Array is sorted in " + swaps + " swaps.");
System.out.println("First Element: " + a[0]);
System.out.println("Last Element: " + a[n - 1]);
}
}