Bubble Sort of PHP implementation

Foreword

Bubble Sort (Bubble Sort) it is also a simple and intuitive sorting algorithm. It repeatedly visited the number of columns to be sorted, a comparison of two elements, if they put them in the wrong order switching over. The number of visits to the column work is repeated until there is no longer need to swap, that is to say the number of columns already sorted completed. The origin of the name of the algorithm is because the smaller elements will slowly through the exchange of "float" to the top of the columns.

As one of the simplest sorting algorithms, feeling bubble sort I feel like Abandon word appears in the book, like, every time on the first page first, so the most familiar. There is also a bubble sort algorithm optimization is to set up a flag, when the element does not occur in the exchange trip traversal sequence, then prove that the sequence has been ordered. But this improvement to enhance performance and it does not have much effect.

Time complexity and stability

Time complexity: O (n²)

Stability: Stable

Algorithm steps

1, comparing adjacent elements. If the first is greater than the second, the two of them exchanged.

2, for the same work for each pair of adjacent elements, from the beginning to the end of a first of the last pair. Once this is done, the last element is the biggest number.

3. Repeat the above steps for all elements, except the last one.

4 for each of the above steps are repeated fewer elements, until there is no need to compare a pair of numbers.

Moving map presentation

When the fastest

When the input data is already positive sequence (have a positive sequence, and I would also like you bubble sort what is the use ah).

When slowest

When the data is entered in reverse order (write a for loop in reverse order output data is not on the line, why use your bubble sort it, I'm busy right).

PHP code implementation

function BubbleSort($arr) {
    // 获得数组总长度
    $num = count($arr);
    // 正向遍历数组
    for ($i = 1; $i < $num; $i++) {
        // 反向遍历
        for ($j = $num - 1; $j >= $i ; $j--) {
            // 相邻两个数比较
            if ($arr[$j] < $arr[$j-1]) {
                // 暂存较小的数
                $iTemp = $arr[$j-1];
                // 把较大的放前面
                $arr[$j-1] = $arr[$j];
                // 较小的放后面
                $arr[$j] = $iTemp;
            }
        }
    }
    return $arr;
}

 optimization

For questions:

After the order of the data line up, bubble algorithm will continue to the next round of comparisons until $ num-1 times, the comparison does not make sense back.

Program:

Set flag $ flag, if the exchange $ flag is set to true occurrence; if there is no exchange is set to false. So that when comparing the end of the round if $ flag is still false, namely: this round of exchange did not occur, indicating the order of data already lined up, there is no need to continue.

function BubbleSort($arr) {
    // 获得数组总长度
    $num = count($arr);
    // 正向遍历数组
    for ($i = 1; $i < $num; $i++) {
        $flag = false;
        // 反向遍历
        for ($j = $num - 1; $j >= $i ; $j--) {
            // 相邻两个数比较
            if ($arr[$j] < $arr[$j-1]) {
                // 暂存较小的数
                $iTemp = $arr[$j-1];
                // 把较大的放前面
                $arr[$j-1] = $arr[$j];
                // 较小的放后面
                $arr[$j] = $iTemp;
                $flag = true;
            }
        }
        if(!$flag) break;
    }
    return $arr;
}

 

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

Guess you like

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