DEV Community

Discussion on: Daily Challenge #308 - Wave Sort

Collapse
 
peter279k profile image
peter279k

Here is the simple solution in PHP:

function wave_sort(&$array) {
  sort($array);
  for ($index=0; $index<count($array)-1; $index+=2) {
    $tmp = $array[$index];
    $array[$index] = $array[$index+1];
    $array[$index+1] = $tmp;
  }
}
Enter fullscreen mode Exit fullscreen mode