简体   繁体   English

快速排序脚本有效,但堆排序脚本无效

[英]Quicksort script works, but heapsort one doesn't

I've been using a quicksort function to sort my stringlists, but as an exercise I wanted to try and code an heapsort function too.我一直在使用快速排序 function 对我的字符串列表进行排序,但作为练习,我也想尝试编写堆排序 function 的代码。 Unfortunately it does not work and I can't understand why.不幸的是它不起作用,我不明白为什么。 The utility functions I use work (because I use them in the quicksort script too and trying both on a list the quicksort one works and the other doesn't)我使用的实用函数有效(因为我也在快速排序脚本中使用它们并在列表中尝试两个快速排序有效而另一个无效)


    {------------------------------------------------------------------------------}
    Procedure Heapify(AList : TStringList; N, Root : Integer);
    Var
        Max, L, R : Integer;
    Begin
        Max := Root;
        L := (2 * Root) + 1;
        R := (2 * Root) + 2;
        If (L < N) And (ListSort(AList, Max, L) < 0 {function to compare strings, read as List[L]>List[Max]}) Then Max := L;
        If (R < N) And (ListSort(AList, Max, R) < 0) Then Max := R;
        If Max <> Root Then
        Begin
             ExchangeItems(AList, Root, Max); {Function to swap strings}
             Heapify(AList, N, Max);
        End;
    End;
    {------------------------------------------------------------------------------}
    Procedure HeapSortStringList(AList : TStringList);
    Var
        I : Integer;
    Begin
        For I := (AList.Count / 2) - 1 DownTo 0 Do Heapify(AList, AList.Count, I);
        For I := AList.Count - 1 DownTo 1 Do
        Begin
            ExchangeItems(AList, I, 0);
            Heapify(AList, I, 0);
        End;
    End;
    {------------------------------------------------------------------------------}

How did you compile this code?你是如何编译这段代码的?

Note that compiler gives us message:请注意,编译器给我们消息:

[dcc32 Error] Unit2.pas(175): E2010 Incompatible types: 'Integer' and 'Extended' [dcc32 错误] Unit2.pas(175):E2010 不兼容类型:“整数”和“扩展”

(AList.Count / 2) should be (AList.Count div 2) for integers (AList.Count / 2) (AList.Count div 2)

After this correction code becomes working.此更正代码生效后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM