Sign in Join
Wiki copytemporary

Radix sort/fi

English · Français · Suomi

Radix sort on kokonaisluku lajittelu algoritmi.

Ominaisuudet

unit URadixSort.pas

pascal

unit URadixSort;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, 
  GQueue ; //  packages/fcl-stl/src/gqueue.pp

type
  // data type
  TItemRadixSort=integer;

// sorting function
procedure RadixSort( var a: array of TItemRadixSort );

implementation

procedure RadixSort( var a: array of TItemRadixSort );

const
  BASE = 16;

type TQueueIRS = specialize TQueue< TItemRadixSort >;

var
  jono : array[ 0 .. BASE - 1 ] of TQueueIRS;
  max : TItemRadixSort;
  i ,k : integer;

  procedure pick;
  var
    i, j: integer;
  begin
    i := 0;
    j := 0;
    while i < high( a ) do
      begin
         while not jono[ j ].IsEmpty do
           begin
             a[ i ] := jono[ j ].Front;
             jono[ j ].Pop;
             inc( i );
           end;
         inc( j );
      end;
  end;

begin
  max := high( a );
  for i := 0 to BASE - 1 do
    jono[ i ] := TQueueIRS.Create;
  for i := low( a ) to high( a ) do
    begin
      if a[ i ] > max then max := a[ i ];
      jono[ abs( a[ i ] mod BASE ) ].Push( a[ i ] );
    end;
  pick;
  k := BASE;
  while  max > k do
    begin
      for i := low( a ) to high( a ) do
        jono[ abs( a[ i ] div k mod BASE ) ].Push( a[ i ] );
      pick;
      k := k * BASE;
    end;
  for i := 0 to BASE - 1 do
    jono[ i ].Free;

end;

end.

Käyttö esimerkki

pascal

uses
  URadixSort

  ...

var

  a: array[0..100] of integer; 

begin

  ...

  RadixSort( a );

From the Free Pascal wiki, page "Radix sort/fi", as it stood on 2019-07-08 after a change by Djzepi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2019

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
DjzepiCreated page

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.