From 1fd382a5a3d932315e40335abdd85785e5bbf13d Mon Sep 17 00:00:00 2001
From: inversepixel <takenine@nvidia.com>
Date: Tue, 9 Apr 2024 17:59:25 +0200
Subject: [PATCH] Changed from std::sort to std::nth_element, since it is
 substantially faster.

---
 cpp/FLIP.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/cpp/FLIP.h b/cpp/FLIP.h
index 884fbc9..e05a8ff 100644
--- a/cpp/FLIP.h
+++ b/cpp/FLIP.h
@@ -2224,9 +2224,10 @@ namespace FLIP
                     Ymax = std::max(luminance, Ymax);
                 }
             }
-            std::sort(luminances.begin(), luminances.end());
-            size_t medianLocation = luminances.size() / 2 - 1;
-            float Ymedian = (luminances[medianLocation] + luminances[medianLocation + 1]) * 0.5f;
+
+            size_t medianLocation = luminances.size() / 2;
+            std::nth_element(luminances.begin(), luminances.begin() + medianLocation, luminances.end());
+            float Ymedian = luminances[medianLocation];
 
             startExposure = log2(xMax / Ymax);
             stopExposure = log2(xMax / Ymedian);