<?php/** * bubblesort0 ($arr): Bubbling sort * bubblesort1 ($arr): Bubbling sort-optimization * bubblesort2 ( $arr): Bubbling sort--another implementation method * selectsort ($arr): Select Sort * insertsort ($arr): Insert Sort * shellsort ( $arr): Hill sort */class Sort{ /** * Bubble Sort: Refers to 22 of adjacent data directly to the comparison exchange sort * @param $arr array to sort */ public static function bubblesort0 ($arr) { for ($i =0; $i <count ($arr)-1; $i + +) { for ($j =1; $j <count ($arr); $j + +) { if ($arr [$j -1]< $arr [$j]) { //sort from big to small $temp = $arr [$j -1]; $arr [ $j -1]= $arr [$j]; $arr [$j]= $temp; } } } return $ arr; } //This algorithm is optimized for the above algorithm //avoids the comparison of the already ordered data public static function bubblesort1 ($arr) { $flag =true; for ($i =0; $i <count ($arr)-1 && $flag, $i + +) { $ flag=false; for ($j =1; $j <count ($arr); $j + +) { if ($arr [$j -1]< $arr [$j]) { //sort from big to small $temp = $arr [$j -1]; $arr [$j -1]= $arr [$j]; $arr [$j]= $temp; $flag =true; } } } } //this algorithm, one of the preceding data is compared to all subsequent data in order to be exchanged more in size. Not exactly bubble sort public static function bubblesort2 ($arr) { for ($i =0; $i <count ($arr)-1; $i + +) { for ($j = $i +1; $j <count ($arr); $j + +) { if ($arr [$i]< $arr [$j]) { $temp = $arr [$i]; $arr [$i]= $arr [$j]; $arr [$j]= $temp; } } } return $arr; } /** * Simple sorting: The first one is taken out (the first of each cycle), assuming the smallest of the data on which the subscript is located, Then, in turn, the following data are compared to find the lowest index of the data element, and finally the exchange. * @param $arr */ Public static function selectsort ($arr) { for ($i = 0; $i <count ($arr)-1; $i + +) { $min = $i;// Subscript for that represents the smallest value in the array ($j = $i +1; $j <count ($ ARR), $j + +) { if ( $arr [$min]> $arr [$j]) { //from small to large $min = $j; } } if ($min != $i) { $temp = $arr [$min]; $arr [$min]=$ arr[$i]; $arr [$ i]= $temp; } } } /** * Direct Insertion Sort: it is in an ordered collection of data, by comparing it with the elements in the collection, and then inserting the data. * @param $arr &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*/&NBSp; public static function insertsort ($arr) { for ($i =1; $i <count ($arr); $i + +) { $inserted = $arr [$i]; //First assume that the array has only one element and that it is already ordered by default (because there is only one), then take the second element as the inserted element //$j = $i-1: The previous data element to insert element subscript //$j >=0 && $inserted < $arr [ $J]: Compare the element that is currently being inserted with the element preceding it, to insert an element that is smaller than the element in front of it, and then move the element in front of it to make a position for the element to be inserted for ($j = $i-1; $j >=0 && $inserted < $arr [$j]; $j-) { $arr [$j +1]=$ arr[$j]; } $arr [$j +1]= $inserted; //indicates that the position to insert the element is labeled $j+1, because the for loop finally executes $j--once, so subtract 1 plus } return $arr; } /** * Hill sort: An array to be sorted, Divided into several sub-sequences, and then sorted separately, so that the its first part ordered, so loop, and finally get an ordered array. * It is an improvement to the direct insertion sort. * @param $arr * @return mixed */ public static function shellsort ($arr) { $count =count ($arr); for ($inc =floor ($count/3), $inc >0; $inc =floor ($inc/3)) { for ($i = $inc; $i < $count; $i + +) { $instered= $arr [$i]; for ($j = $i-$inc, $j >=0 && $instered < $arr [$j]; $j-= $inc) { $arr [$j + $inc]= $arr [$j]; } $arr [$j + $inc]= $instered; } } return $arr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1611587
Data structure--Sort (PHP code implementation)