comp.lang.ada
 help / color / mirror / Atom feed
From: BrianG <briang000@gmail.com>
Subject: Re: Inefficient algorithms
Date: Tue, 14 Sep 2010 21:11:37 -0400
Date: 2010-09-14T21:11:37-04:00	[thread overview]
Message-ID: <i6p6ge$9vj$1@news.eternal-september.org> (raw)
In-Reply-To: <1f6f73cd-f5ff-4408-a1d7-c9ca7dfa70ee@m35g2000prn.googlegroups.com>

Rick wrote:
> I am working on a class in algorithmic efficiency and, to make a
> point, need an algorithm that runs O(2^n) - a factoring algorithm
> perhaps.  All I can find is 'C' code I can't decipher.  Can someone
> please help with an Ada example.
> 
> Thanks
Um, after reading all the responses to date, I don't uderstand what's so 
hard about coming up with an _IN_efficient example.  I'd think any first 
year programming class ought to provide at least one good example.

How about:

------------------------------------------------------------------------------- 

-- Bad_Sort.Ada - Example of O(n^2) code
------------------------------------------------------------------------------- 

    with Ada.Text_IO, Ada.Integer_Text_IO;
    use  Ada.Text_IO, Ada.Integer_Text_IO;
procedure Bad_Sort is
    type My_Array is array (Natural range <>) of Integer;
    function Sort (This : My_Array) return My_Array is
       Input  : My_Array := This;
       Result : My_Array (This'first..This'last);
       Min, Index : Integer;
    begin
       for I in Result'range loop
          Min := Integer'last;
          Index := Input'first-1;
          for J in Input'range loop
             if Input(J) < Min then
                Min := Input(J);
                Index := J;
             end if;
          end loop;
          Result(I) := Min;
          if Index >= Input'first then
             Input(Index) := Integer'last;
          end if;
       end loop;
       return Result;
    end Sort;
    --
    Test : My_Array := (1, 3, 2, 5, 7, 4, 9, 8, 6, 0);
    Answ : My_Array := Sort(Test);
begin
    Put("Input: ");
    for I in Test'range loop
       Put(Test(I));
    end loop;
    New_Line;
    Put("Output:");
    for I in Answ'range loop
       Put(Answ(I));
    end loop;
    New_Line;
end Bad_Sort;

(of course, this is an O(n^2) implementation; maybe that's not what you 
meant :-)



  parent reply	other threads:[~2010-09-15  1:11 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-11  4:24 Inefficient algorithms Rick
2010-09-11  6:51 ` J-P. Rosen
2010-09-13  3:45   ` Robert A Duff
2010-09-11  6:54 ` Niklas Holsti
2010-09-11  7:07   ` Niklas Holsti
2010-09-11  9:07     ` Rick
2010-09-11 15:05       ` Niklas Holsti
2010-09-17  5:26         ` Rick
2010-09-11  9:20     ` Ludovic Brenta
2010-09-11  9:23       ` Ludovic Brenta
2010-09-11 11:20       ` Niklas Holsti
2010-09-11 18:29         ` Peter C. Chapin
2010-09-11 14:28 ` stefan-lucks
2010-09-12  1:04   ` Wilson
2010-09-12  1:53   ` Rick
2010-09-12  8:35     ` Georg Bauhaus
2010-09-12 11:56     ` stefan-lucks
2010-09-15  1:11 ` BrianG [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-09-15  8:51 Rick
2010-09-15 21:45 ` John B. Matthews
2010-09-16 12:05   ` Chad  R. Meiners
2010-09-16 20:19     ` John B. Matthews
2010-09-17  5:24   ` Rick
replies disabled

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