Quick Sort Pit Filling Tips

Keywords: PHP less

Quick sorting is often used because it is more efficient in several sorting methods with the same O(N*logN), and the idea of quick sorting, dividing and conquering, is really useful, so it has a high chance of appearing in many written interviews.

Quick sorting is still difficult to write directly by silence, so make sure you understand the principle before you memorize it, not by hard backing.

Quick Sort is a partition exchange sort proposed by C.R.A.Hoare in 1962.It employs a divide-and-conquer strategy, commonly referred to as Divide-and-Conque.

The most common implementation method is the "filling method", which is implemented as follows:

  1. Select the column header element as the base element pivot, and remember the location index, which corresponds to a pit.
  2. Set two pointers, left and right, to the leftmost and rightmost elements of the array, respectively.
  3. Next, start with the right pointer and compare the element it points to with the base element. If it is larger than pivot, the right pointer moves to the left; if it is smaller than pivot, the element it points to is placed in the corresponding position of the index.
  4. Assign index the position before the element that was put in the pit (the element that the right pointer pointed to before moving) to make it a new "pit" and move the left pointer one bit to the right.
  5. Next, switch to the left pointer for comparison. If the current element pointed to by the left is less than pivot, the left pointer moves to the right; if the element is greater than pivot, the element is placed in the pit, and the position pointed by the left is assigned to the index, making it a new pit, with the right pointer moving one bit to the left.
  6. Repeat steps 3, 4, and 5 until the left equals right, placing pivot in the position where the left and right coincide, where elements smaller than pivot in the columns are on the left side of the pivot and larger than pivot are on the right side of the pivot element.
  7. Get the pivotIndex position of pivot, and in turn repeat steps 1-6 above for the left and right subcolumns of pivotIndex until the numerator column cannot be detached, which is an incremental column from the beginning.

The code implementation is as follows:

<?php

class QuickSortClass
{
    public function partition(array &$arr, $startIndex, $endIndex)
    {
        // Take the first element as the base value
        $pivot = $arr[$startIndex];
        $left = $startIndex;
        $right = $endIndex;

        // The location of the pit, where the accident is equal to the base pivot
        $dataIndex = $startIndex;
        while ($right >= $left) {
            // The right pointer moves from right to left. If the current value is less than the base value, the current element is placed in the pit. The current element's position becomes a new pit. The left moves one position to the right and switches to the left for comparison. Otherwise, the right moves one position to the left to continue comparing the new element's value with the base value.
            while ($right >= $left) {
                if ($arr[$right] < $pivot) {
                    $arr[$dataIndex] = $arr[$right];
                    $dataIndex = $right;
                    $left++;
                    break;
                }
                $right--;
            }

            // The left pointer moves from left to right. If the current value is greater than the base value, the current element is placed in the pit. The current element becomes a new pit. Right moves one position to the left and switches to right for comparison. Otherwise, left moves one position to the right to continue comparing with the base value.
            while($right >= $left) {
                if ($arr[$left] > $pivot) {
                    $arr[$dataIndex] = $arr[$left];
                    $dataIndex = $left;
                    $right --;
                    break;
                }
                $left ++;
            }

        }

        $arr[$dataIndex] = $pivot;
        return $dataIndex;
    }

    public function quickSort(&$arr, $startIndex, $endIndex)
    {
        // Recursive end when startIndex is greater than or equal to endIndex
        if ($startIndex >= $endIndex) {
            return ;
        }
        $pivotIndex = $this->partition($arr, $startIndex, $endIndex);
        $this->quickSort($arr, $startIndex, $pivotIndex - 1);
        $this->quickSort($arr, $pivotIndex + 1, $endIndex);
    }

}

$quickSortClass = new quickSortClass();
$arr = [72, 6, 57, 88, 60, 42, 83, 73, 48, 85];
$quickSortClass->quickSort($arr, 0, count($arr) - 1);

var_dump($arr);

The tricks of the filling method are as follows

1. $left=strart; $right=end; $pivot=$arr[$left]; the first pit is $arr[$left]

2. $right -- Find a number smaller than $pivot from the back and forward, dig it and fill the previous hole, $arr[$left], $arr[$right] becomes a new hole.

3. $left++ looks forward and backward for a number larger than $pivot and digs it to fill the previous hole, $arr[$right].

4. Repeat 2, 3 steps until $left=$right, filling in $arr[$left] with the base number $pivot.

Posted by francisexpress on Sun, 05 May 2019 19:40:38 -0700