Home Backend Development PHP Tutorial Guide to writing a custom sorting algorithm for PHP arrays

Guide to writing a custom sorting algorithm for PHP arrays

Apr 27, 2024 pm 06:12 PM
php Array sort Bubble Sort

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

PHP 数组自定义排序算法的编写指南

Guidelines for writing PHP array custom sorting algorithm

Introduction
Array sorting is programming A common task that allows us to reorganize elements in an array based on specific criteria. PHP provides a variety of built-in sorting algorithms, but sometimes we need to sort arrays based on custom logic, which requires us to write our own custom sorting algorithm.

Bubble sort
Bubble sort is a simple sorting algorithm that sorts an array by repeatedly comparing adjacent elements and exchanging their positions.

function bubbleSort(array &$arr) {
    $n = count($arr);
    for ($i = 0; $i < $n - 1; $i++) {
        for ($j = 0; $j < $n - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $tmp;
            }
        }
    }
}
Copy after login

Selection sort
Selection sort is a sorting algorithm based on selecting the smallest or largest element and exchanging it with its current position.

function selectionSort(array &$arr) {
    $n = count($arr);
    for ($i = 0; $i < $n - 1; $i++) {
        $min_idx = $i;
        for ($j = $i + 1; $j < $n; $j++) {
            if ($arr[$j] < $arr[$min_idx]) {
                $min_idx = $j;
            }
        }

        $tmp = $arr[$i];
        $arr[$i] = $arr[$min_idx];
        $arr[$min_idx] = $tmp;
    }
}
Copy after login

Insertion sort
Insertion sort is a sorting algorithm based on inserting elements one by one into an ordered part.

function insertionSort(array &$arr) {
    $n = count($arr);
    for ($i = 1; $i < $n; $i++) {
        $key = $arr[$i];
        $j = $i - 1;

        while ($j >= 0 && $arr[$j] > $key) {
            $arr[$j + 1] = $arr[$j];
            $j--;
        }

        $arr[$j + 1] = $key;
    }
}
Copy after login

Practical case
Let us use the bubble sort algorithm to sort the following PHP array in ascending order:

$arr = [5, 2, 8, 3, 1];
Copy after login

Call the bubble sort function:

bubbleSort($arr);
Copy after login

Sorted array:

[1, 2, 3, 5, 8]
Copy after login

The above is the detailed content of Guide to writing a custom sorting algorithm for PHP arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles