Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class a {
  4.  
  5.     static Scanner in = new Scanner(System.in);
  6.     public static int n = in.nextInt();
  7.     public static int a[] = new int[n + 2];
  8.     public static int b[] = new int[n + 2];
  9.    
  10.     public static void main(String[] args){
  11.         for (int i = 1; i < n + 1; i++)
  12.             a[i] = in.nextInt();
  13.  
  14.         for (int i = n; i > 0; i--)
  15.             b[i+1]=heap(i-1);
  16.        
  17.         for (int i = 2; i < n + 2; i++)
  18.             System.out.print(b[i]+" ");
  19.     }
  20.  
  21.     public static int heap(int q) {
  22.  
  23.         for (int i = n; i > q; i--)
  24.             f(a, i);
  25.        
  26.         int temp=a[1];
  27.         a[1]=0;
  28.         return temp;
  29.     }
  30.  
  31.     public static void f(int a[], int i) {
  32.         while (i / 2 > 0) {
  33.             if (a[i] > a[i / 2]) {
  34.                 int temp = a[i];
  35.                 a[i] = a[i / 2];
  36.                 a[i / 2] = temp;
  37.             } else
  38.                 i /= 2;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement