瀏覽代碼

* Patch from Mattias Gaertner to improve quicksort memory use (Bug 22119)

git-svn-id: trunk@21386 -
michael 13 年之前
父節點
當前提交
032528115f
共有 1 個文件被更改,包括 16 次插入4 次删除
  1. 16 4
      rtl/objpas/classes/lists.inc

+ 16 - 4
rtl/objpas/classes/lists.inc

@@ -318,10 +318,22 @@ begin
        J := J - 1;
        J := J - 1;
      end;
      end;
    until I > J;
    until I > J;
-   if L < J then
-     QuickSort(FList, L, J, Compare);
-   L := I;
- until I >= R;
+   // sort the smaller range recursively
+   // sort the bigger range via the loop
+   // Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
+   if J - L < R - I then
+   begin
+     if L < J then
+       QuickSort(FList, L, J, Compare);
+     L := I;
+   end
+   else
+   begin
+     if I < R then
+       QuickSort(FList, I, R, Compare);
+     R := J;
+   end;
+ until L >= R;
 end;
 end;
 
 
 procedure TFPList.Sort(Compare: TListSortCompare);
 procedure TFPList.Sort(Compare: TListSortCompare);