Home > Sort algorithms > Comb Sort algorithm – PHP implementation

Comb Sort algorithm – PHP implementation


  1. Compare each pair of elements found on positions n and n+gap and, if they are in reversed order, swap them.
  2. Change the step by division with 1.3
  3. If step is 1 do bubble sort

In bubble sort, when any two elements are compared, they always have a gap (distance from each other) of 1. The basic idea of comb sort is that the gap can be much more than 1 (Shell sort is also based on this idea, but it is a modification of insertion sort rather than bubble sort).

  • Worst case: O(n2)
  • Average case: O(n logn)
  • Best case: O(n)

PHP implementation:

     /**Function for sorting an array with comb sort algorithm.
     * @param array $array
     * @return array
    public static function combSort(array $array) {
        while($gap>1 || $swapped) {
            if ($gap>1) {
            for ($i=0;$i+$gap<$length;$i++) {
                if ($array[$i]>$array[$i+$gap]) {
        return $array;


  1. http://en.wikipedia.org/wiki/Comb_sort
Categories: Sort algorithms Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: