


The complete code for implementing bubble sorting and binary search in PHP
This article brings you the complete code for implementing bubble sorting and binary search in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. helped.
<?php /* *冒泡排序 */ function maopao($array){ for($i =0;$i < count($array);$i++){ for($j = $i+1;$j < count($array);$j++){ if($array[$i] > $array[$j]){ $temp = $array[$i]; $array[$i] = $array[$j]; $array[$j] = $temp; } } } return $array; } /* * 二分查找 */ function erfen($array,$search,$low = 0,$hight = 100) { $midPostion = floor(($low + $hight)/2); $midData = $array[$midPostion]; if($midData == $search) { return $midPostion; } if($search < $midData) { $hight = $midPostion; if($hight == 0) { return false; } return erfen($array,$search,$low,$hight); }else{ $low = $midPostion + 1; if($low > $hight){ return false; } return erfen($array,$search,$low,$hight); } } /* * 100+99+98+.......1; */ function leijia($n) { if($n == 1){ return $n; } return $n + leijia($n-1); } $a= array(9,4,6,8,2,4,5,1); $b= maopao($a); $c = array(1,2,3,4,5,6,7,8,9); $k = 5; $d = erfen($c,$k,0,8); $sum = leijia(100); echo $sum;
Related recommendations:
Commonly used sorting and search algorithms in PHP, PHP sorting algorithm
PHP common algorithms [bubble sort, quick sort, insertion sort, rounding sort, binary search, .]
The above is the detailed content of The complete code for implementing bubble sorting and binary search in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

Function pointer technology can improve code efficiency and reusability, specifically as follows: Improved efficiency: Using function pointers can reduce repeated code and optimize the calling process. Improve reusability: Function pointers allow the use of general functions to process different data, improving program reusability.

How to use C# to write a binary search algorithm. The binary search algorithm is an efficient search algorithm. It finds the position of a specific element in an ordered array with a time complexity of O(logN). In C#, we can write the binary search algorithm through the following steps. Step 1: Prepare data First, we need to prepare a sorted array as the target data for the search. Suppose we want to find the position of a specific element in an array. int[]data={1,3,5,7,9,11,13

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.

Go is an increasingly popular programming language that is designed to be easy to write, easy to read, and easy to maintain, while also supporting advanced programming concepts. Time complexity and space complexity are important concepts in algorithm and data structure analysis. They measure the execution efficiency and memory size of a program. In this article, we will focus on analyzing the time complexity and space complexity in the Go language. Time Complexity Time complexity refers to the relationship between the execution time of an algorithm and the size of the problem. Time is usually expressed in Big O notation

How to implement the bubble sort algorithm in C# Bubble sort is a simple but effective sorting algorithm that arranges an array by comparing adjacent elements multiple times and exchanging positions. In this article, we will introduce how to implement the bubble sort algorithm using C# language and provide specific code examples. First, let us understand the basic principles of bubble sort. The algorithm starts from the first element of the array and compares it with the next element. If the current element is larger than the next element, swap their positions; if the current element is smaller than the next element, keep it

PHP array sorting algorithm complexity: Bubble sort: O(n^2) Quick sort: O(nlogn) (average) Merge sort: O(nlogn)

The use of data structures and algorithms is crucial in cloud computing for managing and processing massive amounts of data. Common data structures include arrays, lists, hash tables, trees, and graphs. Commonly used algorithms include sorting algorithms, search algorithms and graph algorithms. Leveraging the power of Java, developers can use Java collections, thread-safe data structures, and Apache Commons Collections to implement these data structures and algorithms.
