comp.lang.ada
 help / color / mirror / Atom feed
From: Lutz Donnerhacke <lutz@iks-jena.de>
Subject: Re: Quicksort algorithm in ada
Date: Wed, 12 Apr 2006 14:22:37 +0000 (UTC)
Date: 2006-04-12T14:22:37+00:00	[thread overview]
Message-ID: <slrne3q39d.3te.lutz@taranis.iks-jena.de> (raw)
In-Reply-To: 443d040b$1@news.uni-rostock.de

* Thomas Krueger wrote:
> Do you know the trick or a link to a good Ada implementation of quicksort.

The classic algorithm is easy to implement. You are free to make it generic.

with Ada.Text_IO;
use  Ada.Text_IO;

procedure t is
  procedure qsort(f : in out String) is
    procedure swap(i,j : Positive) is
      t : Character := f(i);
    begin
      f(i) := f(j);
      f(j) := t;
    end swap;

    i : Natural := f'First;
    j : Natural := f'Last;
  begin
    if f'Length > 1 then
      while i<j loop
        while i<j loop
	  if f(i) > f(j) then
	    swap(i,j);
	    j := Positive'Pred(j);
	    exit;
	  end if;
	  i := Positive'Succ(i);
        end loop;
        while i<j loop
	  if f(i) > f(j) then
	    swap(i,j);
	    i := Positive'Succ(i);
	    exit;
	  end if;
	  j := Positive'Pred(j);
  	end loop;
      end loop;
      qsort(f(f'First .. Positive'Pred(j)));
      qsort(f(Positive'Succ(i) .. f'Last));
    end if;
  end qsort;
  
  buff : String(1 .. 80);
  last : Natural;
begin
  Get_Line(buff, last);
  qsort(buff(buff'First .. last));
  Put_Line(buff(buff'First .. last));
end t;



  reply	other threads:[~2006-04-12 14:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-12 13:43 Quicksort algorithm in ada Thomas Krueger
2006-04-12 14:22 ` Lutz Donnerhacke [this message]
2006-04-12 15:04 ` Gautier
2006-04-12 17:00 ` Robert A Duff
2006-04-12 18:06 ` Georg Bauhaus
2006-04-12 18:11   ` Georg Bauhaus
2006-04-13  8:24     ` Thomas Krueger
2006-04-13 20:54       ` Matthew Heaney
2006-04-19 16:35 ` Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox