Advertisement
Guest User

SHell

a guest
Oct 22nd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. public class Shell {
  2.  
  3.     public static void shell(int[] a) {
  4.         int increment = a.length / 2;
  5.         while (increment > 0) {
  6.             for (int i = increment; i < a.length; i++) {
  7.                 int j = i;
  8.                 int temp = a[i];
  9.                 while (j >= increment && a[j - increment] > temp) {
  10.                     a[j] = a[j - increment];
  11.                     j = j - increment;
  12.                 }
  13.                 a[j] = temp;
  14.             }
  15.             if (increment == 2) {
  16.                 increment = 1;
  17.             } else {
  18.                 increment *= (5.0 / 11);
  19.             }
  20.         }
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.         int[] x = new int[]{3, 2, 1, 21, 32, 12, 10, 29, 8};
  25.  
  26.         System.out.println("Sebelum:");
  27.         for (Integer i : x) {
  28.             System.out.print(i.intValue() + " ");
  29.         }
  30.         System.out.println("");
  31.         shell(x);
  32.         System.out.println("Sesudah:");
  33.         for (Integer i : x) {
  34.             System.out.print(i.intValue() + " ");
  35.         }
  36.         System.out.println("");
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement