public class Main {
public static void main(String[] args) {
int [] input = new int []
{1 ,6 ,5 ,2 ,7 ,10 ,86 ,21 ,89 ,10 ,87 ,10 ,81 ,3 ,88 ,10 ,123 ,56 ,21 ,
31 ,465 ,31 ,634 ,321 ,564 ,6987 ,21 ,1 ,1 ,2 ,3 ,5 ,46 ,6874 ,63541 ,321 ,321 ,
54 ,32 ,654 ,87 ,365 ,321 ,574 ,687 ,321 ,21 ,653 ,987 ,31 ,321 };
int K = 10 ;
int [] result = new int [K];
for (int i = 0 ;i<K;i++){
result[i] = input[i];
int index = i;
while (index != 0 && result[index ] < result[getParent(index )]){
result = swap(result, index , getParent(index ));
index = getParent(index );
}
}
for (int i = K;i<input.length;i++){
if (input[i] < result[0 ])
continue ;
int index = 0 ;
result[0 ] = input[i];
while (getLeftSon(result, index )!= -1 ){
int swap = 0 ;
if (getRightSon(result, index )!= -1 ){
if (result[index ] > result[getLeftSon(result, index )] && result[index ] > result[getRightSon(result, index )]){
if (result[getLeftSon(result, index )] < result[getRightSon(result, index )])
swap = 1 ;
else
swap = 2 ;
}
else if (result[index ] > result[getLeftSon(result, index )])
swap = 1 ;
else if (result[index ] > result[getRightSon(result, index )])
swap = 2 ;
}
else {
if (result[index ] > result[getLeftSon(result, index )])
swap = 1 ;
}
switch (swap) {
case 1 :
result = swap(result, index , getLeftSon(result, index ));
index = getLeftSon(result, index );
break ;
case 2 :
result = swap(result, index , getRightSon(result, index ));
index = getRightSon(result, index );
break ;
default :
break ;
}
if (swap == 0 ){
break ;
}
}
}
for (int i : result) {
System.out.println(i);
}
}
public static int getParent(int x){
return (int )((x - 1 )/2 );
}
public static int getLeftSon(int [] arr,int x){
if (2 *x + 1 < arr.length )
return 2 *x + 1 ;
return -1 ;
}
public static int getRightSon(int [] arr,int x){
if (2 *x+2 < arr.length)
return 2 *x + 2 ;
return -1 ;
}
public static int [] swap(int [] arr, int x, int y) {
int temp;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
return arr;
}
}