Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 2.68 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  private static void sort(int start, int end, long[] array) {
  2.            long temp;
  3.            int length = end - start;
  4.            if (length < 7) {
  5.                for (int i = start + 1; i < end; i++) {
  6.                    for (int j = i; j > start && array[j - 1] > array[j]; j--) {
  7.                        temp = array[j];
  8.                        array[j] = array[j - 1];
  9.                        array[j - 1] = temp;
  10.                    }
  11.                }
  12.                return;
  13.            }
  14.            int middle = (start + end) / 2;
  15.            if (length > 7) {
  16.                int bottom = start;
  17.                int top = end - 1;
  18.                if (length > 40) {
  19.                    length /= 8;
  20.                    bottom = med3(array, bottom, bottom + length, bottom
  21.                            + (2 * length));
  22.                    middle = med3(array, middle - length, middle, middle + length);
  23.                    top = med3(array, top - (2 * length), top - length, top);
  24.                }
  25.                middle = med3(array, bottom, middle, top);
  26.            }
  27.            long partionValue = array[middle];
  28.            int a, b, c, d;
  29.            a = b = start;
  30.            c = d = end - 1;
  31.            while (true) {
  32.                while (b <= c && array[b] <= partionValue) {
  33.                    if (array[b] == partionValue) {
  34.                        temp = array[a];
  35.                        array[a++] = array[b];
  36.                        array[b] = temp;
  37.                    }
  38.                    b++;
  39.                }
  40.                while (c >= b && array[c] >= partionValue) {
  41.                    if (array[c] == partionValue) {
  42.                        temp = array[c];
  43.                        array[c] = array[d];
  44.                        array[d--] = temp;
  45.                    }
  46.                    c--;
  47.                }
  48.                if (b > c) {
  49.                    break;
  50.                }
  51.                temp = array[b];
  52.                array[b++] = array[c];
  53.                array[c--] = temp;
  54.            }
  55.            length = a - start < b - a ? a - start : b - a;
  56.            int l = start;
  57.            int h = b - length;
  58.            while (length-- > 0) {
  59.                temp = array[l];
  60.                array[l++] = array[h];
  61.                array[h++] = temp;
  62.            }
  63.            length = d - c < end - 1 - d ? d - c : end - 1 - d;
  64.            l = b;
  65.            h = end - length;
  66.            while (length-- > 0) {
  67.                temp = array[l];
  68.                array[l++] = array[h];
  69.                array[h++] = temp;
  70.            }
  71.            if ((length = b - a) > 0) {
  72.                sort(start, start + length, array);
  73.            }
  74.            if ((length = d - c) > 0) {
  75.                sort(end - length, end, array);
  76.            }
  77.        }