Home >Backend Development >PHP Tutorial >Learn how to implement insertion sort in PHP with examples

Learn how to implement insertion sort in PHP with examples

WBOY
WBOYOriginal
2016-07-25 08:51:20800browse
  1. /**

  2. * Insertion sort
  3. * @param Array $a unordered set
  4. * @return Array ordered set
  5. */
  6. function insertSort($a) {
  7. $temp;
  8. $i;
  9. $j;
  10. $size_a = count($ a);
  11. #Start from the second element
  12. for ($i = 1; $i < $size_a; $i++) {
  13. if ($a[$i] < $a[$i-1]) {
  14. $j = $i; # Save the position of the current element
  15. $temp = $a[$i]; # The value of the current element
  16. # Compare the element on the left, if it finds one smaller than itself, move the element to the right , otherwise insert the element to the current position
  17. while($j>0 && $temp<$a[$j-1]) {
  18. $a[$j] = $a[$j-1];
  19. $j-- ;
  20. }

  21. # Insert element

  22. $a[$j] = $temp;
  23. }
  24. }
  25. return $a;
  26. }
  27. /**
  28. * Get random number
  29. * @param Integer $size quantity
  30. * @return Integer
  31. */
  32. function randomNumber ($size = 10) {
  33. $rand = array();
  34. srand(time(NULL));
  35. for ($i = 0; $i < $size; $i++) {
  36. array_push($rand, mt_rand (0,1000));
  37. }
  38. return $rand;
  39. }
  40. $a = randomNumber();
  41. echo sprintf("Unsorted list %sn", implode(" ", $a));
  42. echo sprintf( "Sorted list %sn", implode(" ", insertSort($a)));

Copy code

php insertion sort implementation code

Insertion sort: Insert a piece of data into the sorted sorted data to obtain a new sorted data with the number plus one.

Algorithm description: ⒈ Starting from the first element, the element can be considered to have been sorted ⒉ Take out the next element and scan from back to front in the sorted element sequence ⒊ If the element (sorted) is larger than the new element, move the element to the next position ⒋ Repeat step 3 until you find the position where the sorted element is less than or equal to the new element ⒌ Insert the new element into the next position ⒍ Repeat step 2

Example:

  1. $arr =array(123,0,5,-1,4,15);

  2. function insertSort(&$arr){
  3. //default first A number with a subscript of 0 is an arranged number
  4. for($i=1;$i//Determine the number for insertion and comparison
  5. $insertVal=$arr[$ i];
  6. //Confirm and compare with the previously compared number
  7. $insertIndex=$i-1;

  8. //Indicates that the position is not found

  9. while($insertIndex>=0 && $insertVal< $arr[$insertIndex]){
  10. //Move the number back
  11. $arr[$insertIndex+1]=$arr[$insertIndex];
  12. $insertIndex--;
  13. }
  14. //Insert (find the position for $insertval )
  15. $arr[$insertIndex+1] = $insertVal;
  16. }
  17. }
  18. insertSort($arr);
  19. print_r($arr);
  20. ?>

Copy code


Statement:
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