PHP to achieve quick sort of

Foreword

Quick sort is a sorting algorithm developed by the Tony Hoare's. Under average conditions, to sort n items to Ο (nlogn) compare times. In the worst situation you need to Ο (n2) comparisons, but this situation is not uncommon. In fact, quick sort it is usually significantly higher than other Ο (nlogn) algorithm is faster because its inner loop (inner loop) can be implemented very efficiently out on most of the architecture.

Quick sort using a divide and conquer (Divide and conquer) strategy to put a serial (list) is divided into two sub-serial (sub-lists).

Quick Sort is a typical apply a divide and conquer thoughts on sorting algorithms. Essentially, quick sort should be bubbling recursive divide and conquer on the basis of the sort.

Quick sort of name from is simple and crude, as heard the name you will know the meaning of its existence, that is, fast, and efficient! It is one of the big data processing fastest sorting algorithm of. Although the Worst Case time complexity reaches O (n²), but the people is good, better than the average time complexity is O (n logn) sorting algorithms fared better in most cases, but this is why, I do not know either. Fortunately, my obsessive-compulsive disorder and committed, the investigation more than N data finally found a satisfactory answer on "algorithm art and Informatics Contest":

The worst-case operating quicksort is O (n²), for example, fast row order number sequence. However, it is desirable amortized time is O (nlogn), and O (nlogn) notation implies a small constant factor, equal complexity than stable O (nlogn) merge sort is much smaller. So, the vast majority of the order of weak random number sequence, the quick sort is always better than merge sort.

Algorithm steps

1, pick one element from the series, called "reference" (Pivot);

2, reordering columns, all of the elements placed in front of the reference small than the reference value, all the elements placed behind the reference value larger than the reference (the number may be either the same side). After the partition exit, on the basis of the number of columns in the middle position. This is called a partition (Partition) operation;

3, recursively (recursive This) than the reference value the number of columns and the sub-element is greater than the reference value of the child element of the sorted columns;

The bottom case of recursion, the size is the number of columns is zero or one, that is, always has been sorted well. While it has been recursion, but this algorithm always quit, because in each iteration (iteration), which will at least one element placed in its final position to go.

The basic idea :( partition)

  • Remove a key number as the number of columns in the start value;

  • This small number than the total number on its left side, it is greater than or equal to the number of all on its right side;

  • Left and right two columns decimal repeat step until only one number each interval.

Auxiliary understand : digging fill in the number of

Initially i = 0; j = 9; key = 72

1, since the number has been saved a [0] in the key may be understood as a pit dug in the array a [0], the data can be filled into the other to this.

2, j from a small start looking forward than the key number. When j = 8, meet the requirements, a [0] = a [8]; i ++; the a [8] into a pit dug refill a [0] in.

3, such a pit a [0] was to get up, but the formation of a new pit a [8], how to do this? Simple, find a digital to fill a [8] the pit.

4, the start looking rearwardly from a number i is greater than the key, when i = 3, meet the requirements, a [8] = a [3]; j-; to a [3] into a pit dug reloading in.

1, when i = 3; j = 7; key = 72

2, and then repeat the above steps, after the start looking forward, and then front looking back.

3, looking forward from the beginning j, when j = 5, in line with conditions, a [5] to fill a pit dug, a [3] = a [5]; i ++;

4, from the start looking rearwardly i, when i = 5, since i == j exit.

5, at this time, i = j = 5, and A [5] is just the last pit dug, thus the key fill a [5].

As can be seen a [5] it is less than the foregoing figures, a [5] is greater than the number after it. Thus above steps are repeated for a [0 ... 4] and a [6 ... 9] This two sub-interval it.

Moving map presentation

PHP code implementation

function QuickSort($arr){
    $num = count($arr);
    $l = $r = 0;
    $left = $right = array();
    // 从索引的第二个开始遍历数组
    for ($i = 1;$i < $num; $i++) {
        // 如果值小于索引1
        if ($arr[$i] < $arr[0]) {
            // 装入左索引数组(小于索引1的数据)
            $left[] = $arr[$i];
            $l++;
        } else {
            // 否则装入右索引中(大于索引1的数据)
            $right[] = $arr[$i];
            $r++; //
        }       
    }
    // 如果左索引有值 则对左索引排序
    if($l > 1) {
        $left = QuickSort($left);
    }
    // 排序后的数组
    $new_arr = $left;
    // 将当前数组第一个放到最后
    $new_arr[] = $arr[0];
    // 如果又索引有值 则对右索引排序
    if ($r > 1) {
        $right = QuickSort($right);
    }
    // 根据右索引的长度再次增加数据
    for($i = 0;$i < $r; $i++) {
        $new_arr[] = $right[$i];
    }
    return $new_arr;
}

 

Published 109 original articles · won praise 101 · views 360 000 +

Guess you like

Origin blog.csdn.net/Alen_xiaoxin/article/details/105203527