Advertisement
Guest User

Untitled

a guest
Mar 6th, 2012
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.79 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Oracle designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Oracle in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22.  * or visit www.oracle.com if you need additional information or have any
  23.  * questions.
  24.  */
  25.  
  26. import java.io.PrintStream;
  27. import java.util.*;
  28.  
  29. public class Main
  30. {
  31.     public static void main(String[] args) throws Exception
  32.     {
  33.         long starttime = new Date().getTime();
  34.         int[] scrambled = AntiQuicksort.killQuicksort(100000);
  35.         System.out.println("Scrambling ended in " + (new Date().getTime() - starttime) + "ms with " + AntiQuicksort.pivots.size() + "pivots");
  36.        
  37.         starttime = new Date().getTime();
  38.         Arrays.sort(scrambled);
  39.         System.out.println("Sorting ended in " + (new Date().getTime() - starttime) + "ms");
  40.     }
  41. }
  42.  
  43.  
  44. class AntiQuicksort
  45. {
  46.     static int max;
  47.     static int[] positions;
  48.     static ArrayList<Integer> pivots;
  49.    
  50.     public static int[] killQuicksort(int size) throws Exception
  51.     {
  52.         max = size;
  53.         positions = new int[size];
  54.         int[] sorted = new int[size];
  55.         for (int i = 0; i < size; i++)
  56.         {
  57.             positions[i] = i;
  58.             sorted[i] = Integer.MIN_VALUE;
  59.         }
  60.         pivots = new ArrayList<Integer>();
  61.         sort1(sorted, 0, size);
  62.         int[] result = new int[size];
  63.         for (int i = 0; i < size; i++)
  64.         {
  65.             result[positions[i]] = sorted[i];
  66.             if (result[positions[i]] == Integer.MIN_VALUE)
  67.             {
  68.                 result[positions[i]] = max;
  69.                 max--;
  70.             }
  71.         }
  72.         return result;
  73.     }
  74.    
  75.     /**
  76.      * Sorts the specified sub-array of integers into ascending order.
  77.      */
  78.     private static void sort1(int x[], int off, int len) throws Exception {
  79.         // Insertion sort on smallest arrays
  80.         if (len < 7) {
  81.             for (int i=off; i<len+off; i++)
  82.                 for (int j=i; j>off && x[j-1]>x[j]; j--)
  83.                     swap(x, j, j-1);
  84.             return;
  85.         }
  86.  
  87.         // Choose a partition element, v
  88.         int m = off + (len >> 1);       // Small arrays, middle element
  89.         if (len > 7) {
  90.             int l = off;
  91.             int n = off + len - 1;
  92.             if (len > 40) {        // Big arrays, pseudomedian of 9
  93.                 int s = len/8;
  94.                
  95.             // Hack start
  96.                 if (x[l] != Integer.MIN_VALUE ||
  97.                     x[l + s] != Integer.MIN_VALUE ||
  98.                     x[l + 2 * s] != Integer.MIN_VALUE ||
  99.                     x[m - s] != Integer.MIN_VALUE ||
  100.                     x[m] != Integer.MIN_VALUE ||
  101.                     x[m + s] != Integer.MIN_VALUE ||
  102.                     x[n - 2 * s] != Integer.MIN_VALUE ||
  103.                     x[n - s] != Integer.MIN_VALUE ||
  104.                     x[n] != Integer.MIN_VALUE
  105.                     )
  106.                 {
  107.                     // Do nothing
  108.                 }
  109.                 else
  110.                 {
  111.                     set(x, l, max - 4);
  112.                     set(x, l + s, max - 5);
  113.                     set(x, l + 2 * s, max - 3);
  114.                     set(x, m - s, max - 1);
  115.                     set(x, m, max - 2);
  116.                     set(x, m + s, max);
  117.                     set(x, n - 2 * s, max - 7);
  118.                     set(x, n - s, max - 8);
  119.                     set(x, n, max - 6);
  120.                     max -= 9;
  121.                    
  122.                     pivots.add(x[l]);
  123.                    }
  124.             // Hack end
  125.                
  126.                 l = med3(x, l,     l+s, l+2*s);
  127.                 m = med3(x, m-s,   m,   m+s);
  128.                 n = med3(x, n-2*s, n-s, n);
  129.             }
  130.         // Hack start
  131.             else {
  132.                 if (x[l] != Integer.MIN_VALUE ||
  133.                     x[m] != Integer.MIN_VALUE ||
  134.                     x[n] != Integer.MIN_VALUE)
  135.                 {
  136.                     // Do nothing
  137.                 }
  138.                 else
  139.                 {
  140.                     set(x, l, max - 1);
  141.                     set(x, m, max - 2);
  142.                     set(x, n, max);
  143.                     max -= 3;
  144.                        
  145.                     pivots.add(x[l]);
  146.                     }
  147.             }
  148.         // Hack end
  149.            
  150.             m = med3(x, l, m, n); // Mid-size, med of 3
  151.         }
  152.         int v = x[m];
  153.  
  154.         // Establish Invariant: v* (<v)* (>v)* v*
  155.         int a = off, b = a, c = off + len - 1, d = c;
  156.         while(true) {
  157.             while (b <= c && x[b] <= v) {
  158.                 if (x[b] == v)
  159.                     swap(x, a++, b);
  160.                 b++;
  161.             }
  162.             while (c >= b && x[c] >= v) {
  163.                 if (x[c] == v)
  164.                     swap(x, c, d--);
  165.                 c--;
  166.             }
  167.             if (b > c)
  168.                 break;
  169.             swap(x, b++, c--);
  170.         }
  171.  
  172.         // Swap partition elements back to middle
  173.         int s, n = off + len;
  174.         s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
  175.         s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
  176.  
  177.         // Recursively sort non-partition-elements
  178.         if ((s = b-a) > 1)
  179.             sort1(x, off, s);
  180.         if ((s = d-c) > 1)
  181.             sort1(x, n-s, s);
  182.     }
  183.  
  184.     /**
  185.      * Swaps x[a] with x[b].
  186.      */
  187.     private static void swap(int x[], int a, int b) {
  188.         int t = x[a];
  189.         x[a] = x[b];
  190.         x[b] = t;
  191.         int tpos = positions[a];
  192.         positions[a] = positions[b];
  193.         positions[b] = tpos;
  194.     }
  195.  
  196.     /**
  197.      * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
  198.      */
  199.     private static void vecswap(int x[], int a, int b, int n) {
  200.         for (int i=0; i<n; i++, a++, b++)
  201.             swap(x, a, b);
  202.     }
  203.  
  204.     /**
  205.      * Returns the index of the median of the three indexed integers.
  206.      */
  207.     private static int med3(int x[], int a, int b, int c) {
  208.         return (x[a] < x[b] ?
  209.                 (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
  210.                 (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
  211.     }
  212.    
  213.     private static void set(int x[], int pos, int value) throws Exception
  214.     {
  215.         if (x[pos] == Integer.MIN_VALUE)
  216.             x[pos] = value;
  217.         else
  218.             throw new Exception();
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement