PHP four basic sorting algorithms

Ready to work

<?php
//待排序数组
$arr = [7, 1, 3, 2, 6, 5, 4, 0];

One, bubble sort

function bubbleSort($arr)
{
    
    
	$len = count($arr);
	if ($len <= 1) {
    
    
		return $arr;
	}
  	for ($i = 1; $i < $len; ++$i) {
    
    
		for ($j = 0; $j < $len - $i; ++$j) {
    
    
	  		if ($arr[$j] > $arr[$j + 1]) {
    
    
	  			$temp = $arr[$j + 1];
	  			$arr[$j + 1] = $arr[$j];
	  			$arr[$j] = $temp;
	  		}
		}
  	}
  	return $arr;
}
  1. Compare items n and n+1, if n item> n+1 item, swap positions.
  2. In fact, it is to find the maximum value one by one.
  3. The first level of for loop controls the number of times to find the maximum value, the length is -1 times.
  4. The second level of the for loop controls the boundary. $i represents several maximum values ​​that have been sorted at the end.

Second, select sort

function selectSort($arr)
{
    
    
	$len = count($arr);
	for ($i = 0; $i < $len; ++$i) {
    
    
		$k = $i;
		for ($j = $i + 1; $j < $len; ++$j) {
    
    
			if ($arr[$j] < $arr[$k]) {
    
    
				$k = $j;
			}
		}
		if ($k !== $i) {
    
    
			$temp = $arr[$k];
			$arr[$k] = $arr[$i];
			$arr[$i] = $temp;
		}
	}
	return $arr;
}
  1. Start from 0, find the minimum value backward, and then swap the positions of the two. Then start from 1 to find the minimum value, and so on.
  2. The first level of the for loop controls the number of times to find the minimum value, the length of the time, and the difference between bubbling and self-realization.
  3. The second level of the for loop controls the starting position, and it has been sorted before $i.

Three, insertion sort

function insertSort($arr)
{
    
    
	$len = count($arr);
	if ($len <= 1) {
    
    
		return $arr;
	}
	for ($i = 1; $i < $len; ++$i) {
    
    
		$temp = $arr[$i];
		for ($j = $i - 1; $j > -1; --$j) {
    
    
			if ($temp < $arr[$j]) {
    
    
				$arr[$j + 1] = $arr[$j];
				$arr[$j] = $temp;
			} else {
    
    
				break;
			}
		}
	}
	return $arr;
}
  1. The current item is compared and exchanged forward until it is not greater than that item, and the loop is out of the loop.
  2. The first level of for loop control represents how many items need to be compared.
  3. The second level of for loop controls the left boundary.
  4. It's a bit like bubbling, but it only focuses on the current value and stops immediately after finding its position.

Four, quick sort

function quickSort($arr)
{
    
    
	$len = count($arr);
	if ($len <= 1) {
    
    
		return $arr;
	}
	$left = $right = [];
	for ($i = 1; $i < $len; ++$i) {
    
    
		if ($arr[$i] < $arr[0]) {
    
    
			$left[] = $arr[$i];
		} else {
    
    
			$right[] = $arr[$i];
		}
	}
	$left = quickSort($left);
	$right = quickSort($right);
	return array_merge($left, [$arr[0]], $right);
}
  1. Take the first item of the array as the standard, sort the other items into the left and right arrays, and then operate recursively.
  2. Recursion recursion recursion recursion.

Guess you like

Origin blog.csdn.net/z772532526/article/details/83544832