-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-7400: [Java] Avoid the worst case for quick sort
This issue is in response of a discussion in: #5540 (comment). The quick sort algorithm can degenerate to an O(n^2) algorithm, if the pivot is selected poorly. This is an important problem, as the worst case can happen, if the input vector is alrady sorted, which is frequently encountered in practice. After some investigation, we solve the problem with a simple but effective approach: take 3 samples and choose the median (with at most 3 comparisons) as the pivot. This sorts the vector which is already sorted in O(nlogn) time. Closes #6039 from liyafan82/fly_1213_sort and squashes the following commits: 0943b06 <liyafan82> Make tests more readable 7cdf0a6 <liyafan82> Fix the bug of choosing pivot and add more tests e6ab2bf <liyafan82> Apply insertion sort when the range is small 1167176 <liyafan82> Avoids the worst case for quick sort Authored-by: liyafan82 <fan_li_ya@foxmail.com> Signed-off-by: Micah Kornfield <emkornfield@gmail.com>
- Loading branch information
1 parent
389d38b
commit 38504e3
Showing
7 changed files
with
513 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
java/algorithm/src/main/java/org/apache/arrow/algorithm/sort/InsertionSorter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.arrow.algorithm.sort; | ||
|
||
import org.apache.arrow.vector.BaseFixedWidthVector; | ||
import org.apache.arrow.vector.IntVector; | ||
import org.apache.arrow.vector.ValueVector; | ||
|
||
/** | ||
* Insertion sorter. | ||
*/ | ||
class InsertionSorter { | ||
|
||
/** | ||
* Sorts the range of a vector by insertion sort. | ||
* | ||
* @param vector the vector to be sorted. | ||
* @param startIdx the start index of the range (inclusive). | ||
* @param endIdx the end index of the range (inclusive). | ||
* @param buffer an extra buffer with capacity 1 to hold the current key. | ||
* @param comparator the criteria for vector element comparison. | ||
* @param <V> the vector type. | ||
*/ | ||
static <V extends BaseFixedWidthVector> void insertionSort( | ||
V vector, int startIdx, int endIdx, VectorValueComparator<V> comparator, V buffer) { | ||
comparator.attachVectors(vector, buffer); | ||
for (int i = startIdx; i <= endIdx; i++) { | ||
buffer.copyFrom(i, 0, vector); | ||
int j = i - 1; | ||
while (j >= startIdx && comparator.compare(j, 0) > 0) { | ||
vector.copyFrom(j, j + 1, vector); | ||
j = j - 1; | ||
} | ||
vector.copyFrom(0, j + 1, buffer); | ||
} | ||
} | ||
|
||
/** | ||
* Sorts the range of vector indices by insertion sort. | ||
* | ||
* @param indices the vector indices. | ||
* @param startIdx the start index of the range (inclusive). | ||
* @param endIdx the end index of the range (inclusive). | ||
* @param comparator the criteria for vector element comparison. | ||
* @param <V> the vector type. | ||
*/ | ||
static <V extends ValueVector> void insertionSort( | ||
IntVector indices, int startIdx, int endIdx, VectorValueComparator<V> comparator) { | ||
for (int i = startIdx; i <= endIdx; i++) { | ||
int key = indices.get(i); | ||
int j = i - 1; | ||
while (j >= startIdx && comparator.compare(indices.get(j), key) > 0) { | ||
indices.set(j + 1, indices.get(j)); | ||
j = j - 1; | ||
} | ||
indices.set(j + 1, key); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.