aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_QUICKSORT_HPP aoqi@0: #define SHARE_VM_UTILITIES_QUICKSORT_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "runtime/globals.hpp" aoqi@0: #include "utilities/debug.hpp" aoqi@0: aoqi@0: class QuickSort : AllStatic { aoqi@0: aoqi@0: private: aoqi@0: template aoqi@0: static void swap(T* array, int x, int y) { aoqi@0: T tmp = array[x]; aoqi@0: array[x] = array[y]; aoqi@0: array[y] = tmp; aoqi@0: } aoqi@0: aoqi@0: // As pivot we use the median of the first, last and middle elements. aoqi@0: // We swap in these three values at the right place in the array. This aoqi@0: // means that this method not only returns the index of the pivot aoqi@0: // element. It also alters the array so that: aoqi@0: // array[first] <= array[middle] <= array[last] aoqi@0: // A side effect of this is that arrays of length <= 3 are sorted. aoqi@0: template aoqi@0: static int find_pivot(T* array, int length, C comparator) { aoqi@0: assert(length > 1, "length of array must be > 0"); aoqi@0: aoqi@0: int middle_index = length / 2; aoqi@0: int last_index = length - 1; aoqi@0: aoqi@0: if (comparator(array[0], array[middle_index]) == 1) { aoqi@0: swap(array, 0, middle_index); aoqi@0: } aoqi@0: if (comparator(array[0], array[last_index]) == 1) { aoqi@0: swap(array, 0, last_index); aoqi@0: } aoqi@0: if (comparator(array[middle_index], array[last_index]) == 1) { aoqi@0: swap(array, middle_index, last_index); aoqi@0: } aoqi@0: // Now the value in the middle of the array is the median aoqi@0: // of the fist, last and middle values. Use this as pivot. aoqi@0: return middle_index; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: static int partition(T* array, int pivot, int length, C comparator) { aoqi@0: int left_index = -1; aoqi@0: int right_index = length; aoqi@0: T pivot_val = array[pivot]; aoqi@0: aoqi@0: while (true) { aoqi@0: do { aoqi@0: left_index++; aoqi@0: } while (comparator(array[left_index], pivot_val) == -1); aoqi@0: do { aoqi@0: right_index--; aoqi@0: } while (comparator(array[right_index], pivot_val) == 1); aoqi@0: aoqi@0: if (left_index < right_index) { aoqi@0: if (!idempotent || comparator(array[left_index], array[right_index]) != 0) { aoqi@0: swap(array, left_index, right_index); aoqi@0: } aoqi@0: } else { aoqi@0: return right_index; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ShouldNotReachHere(); aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: template aoqi@0: static void inner_sort(T* array, int length, C comparator) { aoqi@0: if (length < 2) { aoqi@0: return; aoqi@0: } aoqi@0: int pivot = find_pivot(array, length, comparator); aoqi@0: if (length < 4) { aoqi@0: // arrays up to length 3 will be sorted after finding the pivot aoqi@0: return; aoqi@0: } aoqi@0: int split = partition(array, pivot, length, comparator); aoqi@0: int first_part_length = split + 1; aoqi@0: inner_sort(array, first_part_length, comparator); aoqi@0: inner_sort(&array[first_part_length], length - first_part_length, comparator); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: // The idempotent parameter prevents the sort from aoqi@0: // reordering a previous valid sort by not swapping aoqi@0: // fields that compare as equal. This requires extra aoqi@0: // calls to the comparator, so the performance aoqi@0: // impact depends on the comparator. aoqi@0: template aoqi@0: static void sort(T* array, int length, C comparator, bool idempotent) { aoqi@0: // Switch "idempotent" from function paramter to template parameter aoqi@0: if (idempotent) { aoqi@0: inner_sort(array, length, comparator); aoqi@0: } else { aoqi@0: inner_sort(array, length, comparator); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // for unit testing aoqi@0: #ifndef PRODUCT aoqi@0: static void print_array(const char* prefix, int* array, int length); aoqi@0: static bool compare_arrays(int* actual, int* expected, int length); aoqi@0: template static bool sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent = false); aoqi@0: static void test_quick_sort(); aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: #endif //SHARE_VM_UTILITIES_QUICKSORT_HPP