Recent

Author Topic: RadixSort loses array elements.  (Read 1721 times)

Lupp

  • New Member
  • *
  • Posts: 31
RadixSort loses array elements.
« on: February 06, 2017, 10:20:03 pm »
(Meanwhile I also posted a link to this thread in the 'General' subforum: http://forum.lazarus.freepascal.org/index.php/topic,35734.0.html.)

Greetings again after a while. 

I tried to use the unit URadixSort and the function RadixSort defined there to create a dll. Since I failed in a strange way I made a simple test with a console program on Win 10. Follow the code and the result:
Code: Pascal  [Select][+][-]
  1. program TradixsortTerminal;
  2.  
  3. uses
  4.   Crt, Sysutils, Classes, URadixSort;
  5.  
  6. var arr: array[0..7] of Integer; j: Integer;
  7.  
  8. begin
  9. arr[0]:=999; arr[1]:=111; arr[6]:=666; arr[7]:=777;
  10. arr[2]:=4;     arr[3]:=2;     arr[4]:=5;     arr[5]:=3;
  11.  
  12. RadixSort(arr);
  13.  
  14. ClrScr;
  15. for j := 0 to 7 Do
  16.   WriteLn(arr[j]);
  17.  
  18. Repeat until ReadKey = #27;
  19.  
  20. end.
  21.  
  22. {
  23. The output to the console was :
  24. 2
  25. 3
  26. 4
  27. 5
  28. 666
  29. 777
  30. 777
  31. 777
  32. }

The first and the second element (indices 0, 1) were lost. The highest number was repeated on the unused high places twice.  A translation of indices to [2..7] with the respective assignments and the loop adapted produced exactly the same result.
In a different case not using radixsort passing dynamic arrays to procedures/functions worked as expected.
What did I wrong? Or is there a bug? 

(Lazarus 1.6.2 with FPC 3.0.0 on Win 10)
« Last Edit: February 07, 2017, 11:11:26 am by Lupp »

 

TinyPortal © 2005-2018