Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84443 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28776 invoked from network); 8 Mar 2015 18:05:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2015 18:05:50 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.174 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.174 mail-wi0-f174.google.com Received: from [209.85.212.174] ([209.85.212.174:37473] helo=mail-wi0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7D/27-17427-C7F8CF45 for ; Sun, 08 Mar 2015 13:05:50 -0500 Received: by widem10 with SMTP id em10so134885wid.2 for ; Sun, 08 Mar 2015 11:05:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=YoB7jt5XYpvht64gSDtCmf3jVpBxHRGPG9Rsyoma9OM=; b=xZRtABVK4svD+vM0i54W0dOk6ACw3Ljkrg78pQScLroCeGlygVpsspmNW71H7PdleO gWRLaMPIy6dSlYLtkHisblSORBhK/z29zwAdiiAcCn7z2s1IrMmOBOsdFFH0emTUY8r/ 4Jf8PrZ7CjySTMK2yIIGlYH88wUY0L1X887WQWRoCIaFXjyoNBY2AfqVbyqWZ6/1JcZr ajz+Qsnnq3Lw868ARC1XUpPRb8iz7QbSD9vGHpL+6xyst6slnhvrhGRfvS4rRVmG9++1 nzds9k6vNqP4AN4UT2DbogAOH2F+DH7Mkcjqdiu3itD+PhCZsBzfpg1lTkdkZyFKJ2U5 thng== X-Received: by 10.180.89.210 with SMTP id bq18mr5499037wib.28.1425837945609; Sun, 08 Mar 2015 11:05:45 -0700 (PDT) Received: from [192.168.0.5] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id hn8sm4148868wib.18.2015.03.08.11.05.44 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Mar 2015 11:05:44 -0700 (PDT) Message-ID: <54FC8F5F.3020107@gmail.com> Date: Sun, 08 Mar 2015 18:05:19 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: =?UTF-8?B?R3LDqWdvcnkgUGxhbmNoYXQ=?= , internals@lists.php.net References: <54FC29B4.4000105@luni.fr> <54FC5A71.7000905@gmail.com> <54FC6EAA.3050100@luni.fr> In-Reply-To: <54FC6EAA.3050100@luni.fr> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Feature request and RFC From: rowan.collins@gmail.com (Rowan Collins) On 08/03/2015 15:45, Grégory Planchat wrote: > class BarSortable implements Sorter > { > public function sort(Sortable $collection) > { > $previousKey = null; > $previousElement = null; > > foreach ($collection as $key => $element) { > if ($previousKey === null) { > $previousKey = $key; > $previousElement = $element; > continue; > } > if ($previousElement < element) { > $collection->swap($previousKey, $key); > } > > $previousKey = $key; > $previousElement = $element; > } > } > } Unfortunately, this won't actually sort the array - it only makes one check of each value, so [4, 1, 2, 3, 5] would come out as [4, 2, 3, 1, 5], when it should be [5, 4, 3, 2, 1] (your < sign means you're sorting largest value first). It's *almost* like a bubble sort, though (you keep swapping things until you know that there's no more swaps to be made), which is the simplest but also least efficient way of sorting a list. I think insertion sort, which is simple and efficient for small lists, could be done with iteration plus a single callback of "insertElementAt". For larger lists, though, you're going to want a quicksort, and I can't quite picture what callbacks that would need to run on the collection as it went; it ultimately requires some way of partitioning the list into multiple pieces. Regards, -- Rowan Collins [IMSoP]