Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Binarysort {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         int n = scan.nextInt();
  7.         int[] a = new int[n];
  8.         out(a);
  9.         sort(a);
  10.         out(a);
  11.     }
  12.     public static void sort(int[] a) {
  13.         Scanner scan = new Scanner(System.in);
  14.         for (int i = 0; i < a.length; i++) {
  15.             int x = scan.nextInt();
  16.             System.out.println(x+"x");
  17.             int l = 0;
  18.             int r = a.length;
  19.             int m = (l + r) / 2;
  20.             boolean z = false;
  21.  
  22.             while (z != true) {
  23.                 if(a[0]==0){
  24.                     a[0]=x;
  25.                     z=true;
  26.                 }else if (a[m] >= x && a[m - 1] <= x) {
  27.                     for (int k = a.length - 2; k > m; k--) {
  28.                         a[k + 1] = a[k];
  29.                     }
  30.                     a[m] = m;
  31.                     z = true;
  32.                 } else if (a[m] > x) {
  33.                     r = m;
  34.                     m = (l + r) / 2;
  35.                 } else if (a[m] < x) {
  36.                     l = m;
  37.                     m = (l + r) / 2;
  38.                 }
  39.             }
  40.         }
  41.     }
  42.  
  43.     public static void out(int[] a) {
  44.         System.out.println();
  45.         for (int val : a) {
  46.             System.out.print(val+ "  ");
  47.         }
  48.     }
  49.  
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement