gon2

NotQuick.java

Feb 28th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. /*
  2. Note: This is a sabotaged version of Sedgewick and Wayne's Quick.java.
  3. http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Quick.java
  4. This version does not shuffle the input array, allowing degenerate behaviour.
  5.  
  6. Methods not relevant to the associated assignment have been removed.
  7. */
  8.  
  9. import edu.princeton.cs.algs4.StdOut;
  10.  
  11. public class NotQuick {
  12.  
  13.    // This class should not be instantiated.
  14.    private NotQuick() {
  15.    }
  16.  
  17.    /**
  18.     * Rearranges the array in ascending order, using the natural order.
  19.     *
  20.     * @param a the array to be sorted
  21.     */
  22.    public static void sort(Comparable[] a) {
  23.        sort(a, 0, a.length - 1);
  24.        assert isSorted(a);
  25.    }
  26.  
  27.    // quicksort the subarray from a[lo] to a[hi]
  28.    private static void sort(Comparable[] a, int lo, int hi) {
  29.        if (hi <= lo) return;
  30.        int j = partition(a, lo, hi);
  31.        sort(a, lo, j - 1);
  32.        sort(a, j + 1, hi);
  33.        assert isSorted(a, lo, hi);
  34.    }
  35.  
  36.    // partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi]
  37.    // and return the index j.
  38.    private static int partition(Comparable[] a, int lo, int hi) {
  39.        int i = lo;
  40.        int j = hi + 1;
  41.        Comparable v = a[lo];
  42.        while (true) {
  43.  
  44.            // find item on lo to swap
  45.            while (less(a[++i], v))
  46.                if (i == hi) break;
  47.  
  48.            // find item on hi to swap
  49.            while (less(v, a[--j]))
  50.                if (j == lo) break;      // redundant since a[lo] acts as sentinel
  51.  
  52.            // check if pointers cross
  53.            if (i >= j) break;
  54.  
  55.            exch(a, i, j);
  56.        }
  57.  
  58.        // put partitioning item v at a[j]
  59.        exch(a, lo, j);
  60.        
  61.        // minn kóði:
  62.        
  63.        // jöfnuð prentun
  64.        String bil = "";
  65.        if (lo < 10) bil = " ";
  66.        String bil2 = "";
  67.        if (hi < 10) bil2 = " ";
  68.        StdOut.print("Vendisstak " + v + " raðað frá " + bil + lo + " til " + bil2 + hi + ": ");
  69.        for (Comparable c : a) StdOut.print(c + " ");
  70.        StdOut.println();
  71.  
  72.        // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
  73.        return j;
  74.    }
  75.  
  76.    /***************************************************************************
  77.     *  Helper sorting functions.
  78.     ***************************************************************************/
  79.  
  80.    // is v < w ?
  81.    private static boolean less(Comparable v, Comparable w) {
  82.        return v.compareTo(w) < 0;
  83.    }
  84.  
  85.    // exchange a[i] and a[j]
  86.    private static void exch(Object[] a, int i, int j) {
  87.        Object swap = a[i];
  88.        a[i] = a[j];
  89.        a[j] = swap;
  90.    }
  91.  
  92.  
  93.    /***************************************************************************
  94.     *  Check if array is sorted - useful for debugging.
  95.     ***************************************************************************/
  96.    private static boolean isSorted(Comparable[] a) {
  97.        return isSorted(a, 0, a.length - 1);
  98.    }
  99.  
  100.    private static boolean isSorted(Comparable[] a, int lo, int hi) {
  101.        for (int i = lo + 1; i <= hi; i++)
  102.            if (less(a[i], a[i - 1])) return false;
  103.        return true;
  104.    }
  105.  
  106.    // print array to standard output
  107.    private static void show(Comparable[] comparables) {
  108.        for (Comparable c : comparables) {
  109.            StdOut.print(c + " ");
  110.        }
  111.        StdOut.println();
  112.    }
  113.    
  114.    public static void main(String[] args) {
  115.        String s = "QUICKBROWNFOXJUMPS";
  116.        //String s = "KRATELEPUIMQCXOS";
  117.        Character[] c = new Character[s.length()];
  118.        for (int i = 0; i < s.length(); i++) {
  119.            c[i] = s.charAt(i);
  120.        }
  121.  
  122.        StdOut.print("Fyrir röðun: ");
  123.        show(c);
  124.        NotQuick.sort(c);
  125.        assert isSorted(c);
  126.        StdOut.print("Eftir röðun: ");
  127.        show(c);
  128.  
  129.    }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment