Home > Backend Development > PHP Tutorial > How to implement in-place sorting of an array in PHP so that odd numbers are in front of even numbers (code)

How to implement in-place sorting of an array in PHP so that odd numbers are in front of even numbers (code)

不言
Release: 2023-04-04 08:10:02
Original
2021 people have browsed it

The content of this article is about how to implement in-place sorting of arrays in PHP so that odd numbers are in front of even numbers (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Input an integer array, implement a function to adjust the order of the numbers in the array, so that all odd numbers are located in the first half of the array, and all even numbers are located in the second half of the array, and ensure that odd numbers, odd numbers, and even numbers are The relative position between even numbers remains unchanged.

1. Traverse the array, determine whether the elements are odd or even, push into the new array, and exchange space for time

2. The idea of ​​​​insertion sorting is spatially in-place sorting

2.1 Traverse from front to back to determine if the current number is an odd number

2.2 Start from the current number and traverse from back to front. If it is an even number, move one digit forward

2.3 Current odd insertion position

for i=1;i<arr.length;i++
    target=arr[i]
    if arr[i]%2==1
        j=i-1
        while j>=0&&arr[j]%2==0
            arr[j+1]=arr[j]
            j--
        arr[j+1]=target
Copy after login
<?php
$arr=array(1,2,3,4,5,6,7,8,9,10);
function reOrderArray($arr){
        $length=count($arr);
        //从前往后遍历
        for($i=1;$i<$length;$i++){
                //判断当前元素是奇数
                $target=$arr[$i];
                if($target%2==1){
                        //从后往前遍历,如果有偶数就往后移动一位
                        $j=$i-1;
                        while($j>=0 && $arr[$j]%2==0){
                                $arr[$j+1]=$arr[$j];
                                $j--;
                        }   
                        //把奇数插入位置
                        $arr[$j+1]=$target;
                }   
        }   
        return $arr;
}

$arr2=reOrderArray($arr);
var_dump($arr2);
Copy after login
array(10) {
  [0]=>
  int(1)
  [1]=>
  int(3)
  [2]=>
  int(5)
  [3]=>
  int(7)
  [4]=>
  int(9)
  [5]=>
  int(2)
  [6]=>
  int(4)
  [7]=>
  int(6)
  [8]=>
  int(8)
  [9]=>
  int(10)
}
Copy after login

The above is the detailed content of How to implement in-place sorting of an array in PHP so that odd numbers are in front of even numbers (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template