jpountz commented on a change in pull request #966: LUCENE-9024 Optimize 
IntroSelector to use median of medians
URL: https://github.com/apache/lucene-solr/pull/966#discussion_r338918646
 
 

 ##########
 File path: lucene/core/src/java/org/apache/lucene/util/IntroSelector.java
 ##########
 @@ -33,26 +32,87 @@ public final void select(int from, int to, int k) {
     quickSelect(from, to, k, maxDepth);
   }
 
-  // heap sort
-  // TODO: use median of median instead to have linear worst-case rather than
-  // n*log(n)
-  void slowSelect(int from, int to, int k) {
-    new Sorter() {
+  int slowSelect(int from, int to, int k) {
+    return medianOfMediansSelect(from, to-1, k);
+  }
+
+  int medianOfMediansSelect(int from, int to, int k) {
+    int pivotIndex;
+    do {
+      if (from == to) {
+        return from;
+      }
+      pivotIndex = pivot(from, to);
+      setPivot(pivotIndex);
+      pivotIndex = partition(from, to, k, pivotIndex);
+      setPivot(pivotIndex);
+      if (k == pivotIndex) {
+        return k;
+      } else if (k < pivotIndex) {
+        to = pivotIndex-1;
+      } else {
+        from = pivotIndex+1;
+      }
+    } while (from != to);
+    setPivot(pivotIndex);
+    return pivotIndex;
+  }
 
-      @Override
-      protected void swap(int i, int j) {
-        IntroSelector.this.swap(i, j);
+  private int partition(int left, int right, int n, int pivotIndex) {
 
 Review comment:
   can we rename `n` to `k`, I think it's confusing that the same thing gets 
called different ways?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to