Four commonly used sorting algorithms

1. Bubble sort:

For pairwise comparison, there is no need to compare the last element every cycle, because the last element is already the largest or smallest.
The number of the first cycle is n, the maximum number of the second cycle is n-1, and a maximum or minimum value is selected in each round, and the time complexity is O(n^2).

function maopaoSort ($list)
{
    $len = count($list);
    for ($i = 0; $i < $len - 1; $i++) {
        for ($j = 0; $j < $len - $i - 1; $j++) {
            if ($list[$j] > $list[$j + 1]) {
                $tmp = $list[$j];
                $list[$j] = $list[$j + 1];
                $list[$j + 1] = $tmp;
            }
        }
    }
    return $list;
}

2. Select sort:

Choose one as the minimum value, and compare the rest with this one, and then swap the positions.

The number of the first cycle is n, the maximum number of the second cycle is n-1, and the time complexity is O(n^2).

function xuanzeSort ($list)
{
    $len = count($list);
    for ($i = 0; $i < $len - 1; $i++) {
        $pos = $i;
        for ($j = $i + 1; $j < $len; $j++) {
            if ($list[$pos] > $list[$j]) {
                $pos = $j;
            }
        }
        if ($pos != $i) {
            $tmp = $list[$pos];
            $list[$pos] = $list[$i];
            $list[$i] = $tmp;
        }
    }
    return $list;
}

3. Insertion sort:

Assuming that the previous numbers are all in order, the nth number should be inserted into the ordered array.

The number of the first cycle is n, the maximum number of the second cycle is n-1, and the time complexity is O(n^2).

function charuSort ($list){
    $len = count($list);
    for ($i = 1; $i < $len; $i++) {
        $tmp = $list[$i];//Get comparison elements
        for ($j = $i - 1; $j > 0; $j--) {
            if ($list[$j] > $tmp) {
                $list[$j + 1] = $list[$j];
                $list[$j] = $tmp;
            } else {
                break;
            }
        }
    }
    return $list;
}

4. Quick Sort:

Set a benchmark value, put it on the left if it is less than the benchmark value, and put it on the right if it is greater than the benchmark value, and finally recursively continue to arrange the left and right sides, and finally merge the arrays after sorting. The time complexity is O(nlogn)

public function kuaisuSort($list)
{
   $len = count($list);
   
   // Set an intermediate value, put the value smaller than this value on the left, put the value larger than this value on the right, and finally merge
   $middle = $list[0];
   $leftArr = $rightArr = [];
   for ($i = 1; $i < $len; $i++) {
      if ($list[$i] < $middle) {
         $leftArr[] = $list[$i];
      } else {
         $rightArr[] = $list[$i];
      }
   }
   
   // Continue to sort the left and right arrays
   if ($leftArr) {
      $leftArr =kuaisuSort($leftArr);
   }
   // Continue to sort the array on the right
   if ($rightArr) {
      $rightArr = kuaisuSort($rightArr);
   }
   
   return array_merge($leftArr, [$middle], $rightArr);
}

Guess you like

Origin blog.csdn.net/weixin_64940494/article/details/126976768