diff --git a/src/stdlib/src/sort.rs b/src/stdlib/src/sort.rs
index 79d1e9489db6430692d23276228e9c25fe33e318..7602285f6b5ebb7eeab49fdea705458f006e2212 100644
--- a/src/stdlib/src/sort.rs
+++ b/src/stdlib/src/sort.rs
@@ -46,10 +46,17 @@ fn introsort_helper(
                 break;
             } else {
                 let (left, right) = partition(base, nel, width, comp);
-                introsort_helper(base, left, width, maxdepth, comp);
-                base = unsafe { base.add((right + 1) * width) };
-                nel -= right + 1;
+                let right_base = unsafe { base.add((right + 1) * width) };
+                let right_nel = nel - (right + 1);
                 maxdepth -= 1;
+                if left < nel - right {
+                    introsort_helper(base, left, width, maxdepth, comp);
+                    base = right_base;
+                    nel = right_nel;
+                } else {
+                    introsort_helper(right_base, right_nel, width, maxdepth, comp);
+                    nel = left;
+                }
             }
         }
     }