PHP implementation Insert Sort, PHP implementation sort _php Tutorial

Source: Internet
Author: User
Tags sprintf

PHP implementation Insert Sort, PHP implementation sort


<?php/** * Insert Sort * @param array $a unordered collection * @return Array ordered set */function insertsort ($a) {  $temp;  $i;  $j;  $size _a = count ($a);  # Start with the second element  for ($i = 1; $i < $size _a; $i + +) {          if ($a [$i] < $a [$i-1]) {           $j = $i; # Saves the position of the current element      $temp = $a [$i]; # The value of the current element         # compares the left element, if found smaller than itself, move the element to the right, otherwise insert the element into the current position while      ($j >0 && $temp < $a [$j-1]) {        $a [$j] = $a [$j-1];        $j--;      }             # Insert Element      $a [$j] = $temp;    }  }  return $a;} /** * Get random number * @param integer $size number * @return integer */function randomnumber ($size = ten) {  $rand = array ();  Srand (Time (NULL));  for ($i = 0; $i < $size; $i + +) {    Array_push ($rand, Mt_rand (0,1000));     }  return $rand;} $a = Randomnumber (); Echo sprintf ("unsorted list%s\n", Implode ("", $a)); Echo sprintf ("Sorted list%s\n", Implode ("", INS Ertsort ($a)));

The above is the whole content of this article, I hope you can enjoy.

http://www.bkjia.com/PHPjc/975525.html www.bkjia.com true http://www.bkjia.com/PHPjc/975525.html techarticle PHP Implementation Insert Sort, PHP implementation sort php/** * Insert sort * @param array $a unordered collection * @return Array ordered set */function insertsort ($a) {$temp; $i; $j; $size _a = Count ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.