News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sameer Borate's Blog:
Finding if an array is ordered
January 08, 2009 @ 11:15:35

On the CodeDiesel.com blog today Sameer has posted a quick code snippet that lets you see if a numeric array is already ordered correctly (useful for something like unit testing).

I recently wrote a quick code to find if a numeric array is ordered i.e sorted in a ascending or descending order. I needed it to check a sort algorithm I had written. The problem is that the following code has a worst-case running time of O(N).

He includes the forty-five line function that takes in the array and the sort order it should match. The code then loops around through them checking the value against an incrementing local variable. Also included is an example of its use in a unit test, passing it into an assertEquals for a PHPunit test to check the given array.

2 comments voice your opinion now!
order check array unittest example


blog comments powered by Disqus

Similar Posts

Kevin Schroeder's Blog: ZendCon 2010 Podcast - Unit Testing in Zend Framework 1.8

Pádraic Brady's Blog: Complex Views with the Zend Framework - Part 2: View Helper Pattern

Reddit.com: Any ideas on what "MVC agnostic" PHP code is?

Cal Loveless' Blog: On Continuous Deployment

Shay Ben Moshe's Blog: PHP's native array vs SplFixedArray performance


Community Events





Don't see your event here?
Let us know!


testing series introduction community code list configure interview install language wordpress release framework refactor podcast laravel developer unittest threedevsandamaybe opinion

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework