Sorting an array? lazarus / 62-General ---------------------------------------------------------------------- Jay · 2016-11-28 09:28 Doe FP have a unit which contains procedures for sorting arrays? I couldn't find anything in the docs. I know it's easy enough to write your own from scratch but as it's such a common operation I thought there would be a handy routine for it. Thanks. ---------------------------------------------------------------------- Thaddy · 2016-11-28 09:36 Look at rtl-generics. It has Delphi 2009+ compatible array sorts on any type of array. ---------------------------------------------------------------------- Jay · 2016-11-28 10:51 Thanks. I haven't got my head around OOP yet so I was looking for something procedural, but useful for future reference. ---------------------------------------------------------------------- Thaddy · 2016-11-28 12:16 In fact it is procedural, because the TArray.Sort() methods are class methods. But you can also download 17628 from the embarcadero developers website. That is fully procedural but 32 bit only. It compiles in {$mode delphi}. ---------------------------------------------------------------------- sam707 · 2016-12-14 15:59 you can get inspiration here (dont worry the link below is in english lang) http://jean-pierre.moreau.pagesperso-orange.fr/p_sort.html in some cases TRUE QuickSort algo (my favorite) is more efficient than the inplace mergesort implementation in the fcl of Free Pascal that some people call quicksort, but is NOT ---------------------------------------------------------------------- sam707 · 2016-12-14 16:09 Quote from: Jay on November 28, 2016, 10:28:17 amI know it's easy enough to write your own from scratch huh? computer scientists (1980-90) had great times over years and years, starting from scratch to get fast and safe sort algorithms. So I definitively disagree with "easy" LOL ---------------------------------------------------------------------- skalogryz · 2016-12-14 16:17 Yeah, I posted this old-school approach before. Well, let me do it again! Below is AnySort function that can sort any array of any data as long as comparison routine is provided. SortArrayInteger() is an example of how to sort an array of integers using AnySort. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit anysort; {$ifdef fpc}{$mode delphi}{$H+}{$endif} interface type  TCompareFunc = function (const elem1, elem2): Integer; procedure AnySort(var Arr; Count: Integer; Stride: Integer; CompareFunc: TCompareFunc);procedure SortArrayInteger(var arr: array of Integer; count: Integer); implementation type  TByteArray = array [Word] of byte;  PByteArray = ^TByteArray; procedure AnyQuickSort(var Arr; idxL, idxH: Integer;  Stride: Integer; CompareFunc: TCompareFunc; var SwapBuf);var  ls,hs : Integer;  li,hi : Integer;  mi    : Integer;  ms    : Integer;  pb    : PByteArray;begin  pb:=@Arr;  li:=idxL;  hi:=idxH;  mi:=(li+hi) div 2;  ls:=li*Stride;  hs:=hi*Stride;  ms:=mi*Stride;  repeat    while CompareFunc( pb[ls], pb[ms] ) < 0 do begin      inc(ls, Stride);      inc(li);    end;    while CompareFunc( pb[ms], pb[hs] ) < 0 do begin      dec(hs, Stride);      dec(hi);    end;    if ls <= hs then begin      Move(pb[ls], SwapBuf, Stride);      Move(pb[hs], pb[ls], Stride);      Move(SwapBuf, pb[hs], Stride);      inc(ls, Stride); inc(li);      dec(hs, Stride); dec(hi);    end;  until ls>hs;  if hi>idxL then AnyQuickSort(Arr, idxL, hi, Stride, CompareFunc, SwapBuf);  if li Java itself may be considered more strongly typed than Pascal Yet in Java, arrays of the same type are the same type no matter, which file they are declared in.java is NOT a language its a mix soup subdialect of c++ with one and only one paradigm = object.. plus it is even NOT a compiler producing machine native code but PCode wich java claimed to be new concept but HAHAH new since Fortran 77 (1977 SO! new my ass)I've use a little and for a very short time Fortran 77 in 1990 and I neither knew nor realized that it used a virtual machine on that time. Quote from: sam707 on October 02, 2015, 12:15:33 ammy opinion is simple = bad workers always complain they have bad tools .. they are never guilty but the tools are ---------------------------------------------------------------------- avk · 2019-12-29 09:55 @jollytall, you seem to be right. Would you be kind enough to update the wiki? But IMO, those who use this sorting, definitely should not complain that the compiler generates not quite optimal code. ---------------------------------------------------------------------- jollytall · 2019-12-30 08:38 Quote from: avk on December 29, 2019, 10:55:39 am@jollytall, you seem to be right. Would you be kind enough to update the wiki? I would be happy, but three issues: 1. I hope the community can validate that my patch is really fixing the issue in all cases 2. I hope skalogryz will also read it, and build it in his own code, maybe more elegantly 3. I do not know, how to update the wiki (but probably can find it somewhere). ---------------------------------------------------------------------- jollytall · 2021-10-15 08:00 It took me some time, but finally I went to the wiki page and in the Discussion section commented the bug. I did not want to touch the main page, but probably it also should be done. I also uploaded the source with some more descriptive name changes (and in my own style) to Github: https://github.com/zsoltszakaly/quicksortforpascal ---------------------------------------------------------------------- jollytall · 2021-11-11 07:13 JFYI: I also updated the source code on the Wiki main page: https://wiki.freepascal.org/Array_sort ---------------------------------------------------------------------- marcov · 2021-11-11 09:16 Thank you!