forked from naklecha/hackerrank-algo-implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Larrys_Array.java
36 lines (32 loc) · 951 Bytes
/
Larrys_Array.java
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
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i=0; i<t; i++){
int n = scanner.nextInt();
int[] a = new int[n];
for (int j=0; j<n; j++){
a[j]=scanner.nextInt();
}
bubleSort(a,n);
System.out.println(a[n-2]<a[n-1]?"YES":"NO");
}
}
public static void bubleSort(int[] array, int n){
for (int i=0; i<n-2; i++){
for (int j=n-2-1; j>=i; j--){
while (array[j]>array[j+1]||array[j]>array[j+2]) {
rotate(array,j);
}
}
}
}
public static void rotate(int[] arr, int a){
int temp = arr[a];
arr[a] = arr[a+1];
arr[a+1] = arr[a+2];
arr[a+2] = temp;
}
}