src/share/vm/utilities/quickSort.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 3335
3c648b9ad052
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_QUICKSORT_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_QUICKSORT_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/globals.hpp"
aoqi@0 30 #include "utilities/debug.hpp"
aoqi@0 31
aoqi@0 32 class QuickSort : AllStatic {
aoqi@0 33
aoqi@0 34 private:
aoqi@0 35 template<class T>
aoqi@0 36 static void swap(T* array, int x, int y) {
aoqi@0 37 T tmp = array[x];
aoqi@0 38 array[x] = array[y];
aoqi@0 39 array[y] = tmp;
aoqi@0 40 }
aoqi@0 41
aoqi@0 42 // As pivot we use the median of the first, last and middle elements.
aoqi@0 43 // We swap in these three values at the right place in the array. This
aoqi@0 44 // means that this method not only returns the index of the pivot
aoqi@0 45 // element. It also alters the array so that:
aoqi@0 46 // array[first] <= array[middle] <= array[last]
aoqi@0 47 // A side effect of this is that arrays of length <= 3 are sorted.
aoqi@0 48 template<class T, class C>
aoqi@0 49 static int find_pivot(T* array, int length, C comparator) {
aoqi@0 50 assert(length > 1, "length of array must be > 0");
aoqi@0 51
aoqi@0 52 int middle_index = length / 2;
aoqi@0 53 int last_index = length - 1;
aoqi@0 54
aoqi@0 55 if (comparator(array[0], array[middle_index]) == 1) {
aoqi@0 56 swap(array, 0, middle_index);
aoqi@0 57 }
aoqi@0 58 if (comparator(array[0], array[last_index]) == 1) {
aoqi@0 59 swap(array, 0, last_index);
aoqi@0 60 }
aoqi@0 61 if (comparator(array[middle_index], array[last_index]) == 1) {
aoqi@0 62 swap(array, middle_index, last_index);
aoqi@0 63 }
aoqi@0 64 // Now the value in the middle of the array is the median
aoqi@0 65 // of the fist, last and middle values. Use this as pivot.
aoqi@0 66 return middle_index;
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 template<class T, class C, bool idempotent>
aoqi@0 70 static int partition(T* array, int pivot, int length, C comparator) {
aoqi@0 71 int left_index = -1;
aoqi@0 72 int right_index = length;
aoqi@0 73 T pivot_val = array[pivot];
aoqi@0 74
aoqi@0 75 while (true) {
aoqi@0 76 do {
aoqi@0 77 left_index++;
aoqi@0 78 } while (comparator(array[left_index], pivot_val) == -1);
aoqi@0 79 do {
aoqi@0 80 right_index--;
aoqi@0 81 } while (comparator(array[right_index], pivot_val) == 1);
aoqi@0 82
aoqi@0 83 if (left_index < right_index) {
aoqi@0 84 if (!idempotent || comparator(array[left_index], array[right_index]) != 0) {
aoqi@0 85 swap(array, left_index, right_index);
aoqi@0 86 }
aoqi@0 87 } else {
aoqi@0 88 return right_index;
aoqi@0 89 }
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 ShouldNotReachHere();
aoqi@0 93 return 0;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 template<class T, class C, bool idempotent>
aoqi@0 97 static void inner_sort(T* array, int length, C comparator) {
aoqi@0 98 if (length < 2) {
aoqi@0 99 return;
aoqi@0 100 }
aoqi@0 101 int pivot = find_pivot(array, length, comparator);
aoqi@0 102 if (length < 4) {
aoqi@0 103 // arrays up to length 3 will be sorted after finding the pivot
aoqi@0 104 return;
aoqi@0 105 }
aoqi@0 106 int split = partition<T, C, idempotent>(array, pivot, length, comparator);
aoqi@0 107 int first_part_length = split + 1;
aoqi@0 108 inner_sort<T, C, idempotent>(array, first_part_length, comparator);
aoqi@0 109 inner_sort<T, C, idempotent>(&array[first_part_length], length - first_part_length, comparator);
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 public:
aoqi@0 113 // The idempotent parameter prevents the sort from
aoqi@0 114 // reordering a previous valid sort by not swapping
aoqi@0 115 // fields that compare as equal. This requires extra
aoqi@0 116 // calls to the comparator, so the performance
aoqi@0 117 // impact depends on the comparator.
aoqi@0 118 template<class T, class C>
aoqi@0 119 static void sort(T* array, int length, C comparator, bool idempotent) {
aoqi@0 120 // Switch "idempotent" from function paramter to template parameter
aoqi@0 121 if (idempotent) {
aoqi@0 122 inner_sort<T, C, true>(array, length, comparator);
aoqi@0 123 } else {
aoqi@0 124 inner_sort<T, C, false>(array, length, comparator);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 // for unit testing
aoqi@0 129 #ifndef PRODUCT
aoqi@0 130 static void print_array(const char* prefix, int* array, int length);
aoqi@0 131 static bool compare_arrays(int* actual, int* expected, int length);
aoqi@0 132 template <class C> static bool sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent = false);
aoqi@0 133 static void test_quick_sort();
aoqi@0 134 #endif
aoqi@0 135 };
aoqi@0 136
aoqi@0 137
aoqi@0 138 #endif //SHARE_VM_UTILITIES_QUICKSORT_HPP

mercurial