From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9dfc10efb95ba130 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: BrianG Newsgroups: comp.lang.ada Subject: Re: Inefficient algorithms Date: Tue, 14 Sep 2010 21:11:37 -0400 Organization: A noiseless patient Spider Message-ID: References: <1f6f73cd-f5ff-4408-a1d7-c9ca7dfa70ee@m35g2000prn.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 15 Sep 2010 01:11:46 +0000 (UTC) Injection-Info: mx01.eternal-september.org; posting-host="60RAy+MV5MGUhMnUUJoOPg"; logging-data="10227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CrgqIvRWOQmDA+dyOTnQz" User-Agent: Thunderbird 2.0.0.24 (X11/20100623) In-Reply-To: <1f6f73cd-f5ff-4408-a1d7-c9ca7dfa70ee@m35g2000prn.googlegroups.com> Cancel-Lock: sha1:/J/F++BygSJrhz8ZShhVKAOaQOA= Xref: g2news1.google.com comp.lang.ada:14084 Date: 2010-09-14T21:11:37-04:00 List-Id: 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 :-)