ColdFusion Searching and Sorting Algorithms

Every now and then I find a book related to programming that I put on my mental list of 'must read frequently' books - these are the books that I revisit anywhere from every 6 months to every 2-3 years, simply to stay sharp and to refresh and re-enforce the basics. In order to make this list, a book has to to be well written and be about a topic that makes me think and in some way, shape, or form teach me something every single time I read it. One day I really should post the list here for the benefit of anyone looking for a list of the best books to read to build a solid core understanding of important concepts and techniques for software design, development, and management. Today is not that day. Among this list of books is the Wrox Press "Beginning Algorithms" book (http://www.amazon.com/Beginning-Algorithms-Wrox-Guides/dp/0764596748/ref=sr_1_9?ie=UTF8&s=books&qid=1247243101&sr=1-9), which I recently re-read for the um-teenth time.

I used to (back in 2003-2005) post code on this blog very frequently - primarily APIs, implementations of design patterns, and mini-frameworks and sample applications illustrating programming techniques (AOP, OOP, etc.). It's been a while since I've encountered a subject are, technique, or concept that I felt was worth illustrating in code or hasn't already been illustrated in code by someone else... until I picked up that algorithms book for the first time in quite a while. While I read over it again I realized that nowhere that I know of has anyone provided a comprehensive overview/sample of sorting algorithms using CFML. I find that especially surprising given all of the dialogue and emphasis on taking an Object Oriented Programming approach to CF development within the devolpment community. So, I spent many of my New York City subway rides over the past month implementing sorting algorithms in CFML. The result of wihich I'm finally able to share.

I've created implementations of 6 major sorting algorithms in CFML:
  • Bubble Sort Algorithm
  • Insertion Sort Algorithm
  • Merge Sort Algorithm
  • Quick Sort Algorithm
  • Selection Sort Algorithm
  • Shell Sort Algorithm

I've also implemented the Binary Search algorithm (for searching for an object within a sorted array of objects) and the Binary Insert algorithm (for inserting an object directly into its sorted position in a sorted array of objects). In addition to implementing the sorting algorithms, I created a sample of sorting object without algorithms using Query of Queries. Each algorithm is implemented as a CFC, and should work as-is if you drop them into an application. The only code you have to write in order to implement them, is to write a comparitor function (or several if you want to be able to sort on various properties). The comparitor functions always accept 2 arguments and simply return 0 if the two items to compare are the same, 1 if the first argument is greater, or -1 if the second argument is greater. All of the algorithms accept an array of objects (technically you could also pass arrays of other data types if your comparitor functions expect them), and all of them return a sorted array. All of the algorithms support sorting in ascending and descending directions. Sorting objects that are already in memory is often times more efficient than recreating them, and these algorithms will certainly be more efficient than trying to loop/compare using some other linear 'brute force' type of approach.

I'm not going to describe the algorithms themselves here, nor am I going to get into detail about the efficiency of each compared to the others. The sample application times the sorting, inserting, and searching for you and tells you how long each took. The sample application includes an 'about' page in which I describe how each algorithm conceptually works, and I also give some suggestions about their use including recommendations about which algorithms tend to work best in certain scenarios.

You can download all of the code at http://www.horwith.com/downloads/search-sort.zip. I look forward to hearing how people put them to use as well as any other findings with them.

Comments
When I first read this, I thought to myself... Hmm... doesn't JAVA already have them?

Then I looked up java.util.Collections, and then I realized that there are only binarySearch and sort (that uses MergeSort algorithm) available. They should work with CFML's array (java.util.ArrayList I think?)
# Posted By Henry | 7/10/09 2:18 PM
You are correct Henry - and if you're going to implement just one sorting algorithm, mergesort definitely is the algorithm to implement. That said, the truth is that most CF developers really don't want to write Java code... certainly not for something they can do in CFML. You'd also have to deal with writing the comparison functions in Java and/or type translations from CF to Java. Either way, the important thing to note here is that pretty much every other programming language out there has examples of how to implement sorting and searching algorithms. Though the algorithms I've written work well and are useful in applications, my primary motivation for writing all this was to expose algorithms to CF developers that haven't seen them before, and to provide an example of how you'd implement the algorithms using CFML.
# Posted By Simon Horwith | 7/10/09 3:38 PM
Fantastic. I spent hours looking for something like this last month. I was amazed I couldn't a library for sorts in CF. Hardly any discussion.

Great work. Will any updates be posted here or will you maintain a project at RAIforge?

Thanks!
# Posted By Chris | 7/11/09 8:21 AM
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">wholesale tattoo supplies</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">wholesale tattoo</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">cheap tattoo kits</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">tattoo kits for sale</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">tattoo kits sale</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">technical tattoo supply</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">tatoo kits</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">cheap tattoo supplies</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">tattoo equipment supplies</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">international tattoo supply</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">professional tattoo kits</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">worldwide tattoo supply</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">wholesale tattoo supply</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">tattoo equipment supply</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">cheap tattoo equipment</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">tattoo gun kits</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">international tattoo supplies</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">discount tattoo equipment</a>
<a href="http://www.idealhere.com/wholesale-Tattoo_c68">discount tattoo kits</a>



<a href="http://www.idealhere.com/Tattoo-Machines_c238">custom tattoo machine</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">tattoo machine kits</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">tattoo machine parts</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">custom tattoo machines</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">buy tattoo machine</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">tattoo machine for sale</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">cheap tattoo machines</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">cheap tattoo machine</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">discount tattoo machines</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">homemade tattoo machine</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">discount tattoo machine</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">professional tattoo machines</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">tattoo machines for sale</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">wholesale Tattoo Machine</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">Tattoo Machine wholesale</a>
<a href="http://www.idealhere.com/Tattoo-Machines_c238">Tattoo Machine</a>

<a href="http://www.idealhere.com/wholesale-Needles_c232">cheap tattoo needles</a>
<a href="http://www.idealhere.com/wholesale-Needles_c232">wholesale tattoo needles</a>
<a href="http://www.idealhere.com/wholesale-Needles_c232">discount tattoo needles</a>

<a href="http://www.idealhere.com/Tattoo-Machines_c238">10 Wrap Coils</a>
<a href="http://www.idealhere.com/Tips-and-Nozzles_c239">Tattoo Tubes</a>
<a href="http://www.idealhere.com/Tips-and-Nozzles_c239">Tattoo Tips</a>
<a href="http://www.idealhere.com/Needles_c232">Tattoo Needles</a>
<a href="http://www.idealhere.com/Rotary-Tattoo-Machines_c2...">Rotary Tattoo Machines</a>
<a href="http://www.idealhere.com/wholesale-Tattoo-Machines...">Tattoo Machine Coils</a>
<a href="http://www.idealhere.com/wholesale-Tattoo-Machines...">Tattoo Foot Pedal</a>
<a href="http://www.idealhere.com/wholesale-Power-Supplies-...">Tattoo Power Supply</a>
# Posted By idealhere | 6/25/10 5:11 AM
I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.
<a href="http://simulationassuranceauto.org">simulation assurance auto</a>
# Posted By bloghd86 | 7/2/10 5:35 AM
Your Post is very useful, I am truly happy to post my note on this blog    
<a href="http://theaccidentlawyers.org/injury-lawyer/">injury lawyer</a>. It helped me with
ocean of awareness so I really consider you will do much better in the future
# Posted By injury lawyer | 7/10/10 4:59 AM
This site is hosted by HostMySite and runs off of BlogCFC - thanks, Ray.