-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArranging_Stones.java
46 lines (41 loc) · 1.14 KB
/
Arranging_Stones.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
37
38
39
40
41
42
43
44
45
46
package com.company;
import java.util.Scanner;
public class Arranging_Stones {
static int getMax(int[] arr,int start,int end){
int max = start;
for (int i = 0; i <= end; i++) {
if(arr[max] < arr[i]){
max = i;
}
}
return max;
}
static void sort(int[] arr){
for (int i = 0; i < arr.length; i++){
int last = arr.length - i - 1;
int max = getMax(arr,0,last);
swap(arr,max,last);
}
}
static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0){
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
sort(arr);
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}