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