1

Trying to sort PHP array in ascending order but loosing grip

<?php

$array = [4,5,63,2,1,66,43];
$count = count($array);
$counter = 0;
for ($i=0; $i < count($array); $i++) {
    if($counter > 100)
        break;
    if($array[$i] < $array[$i + 1]) {
        continue;
    } else {
        $now_i = $array[$i];
        unset($array[$i]);
        $array[] = $now_i;
        $array = array_values($array);
        $i = 0;
    }
    print_r($array);
    $counter++;
}

// End up with this array

Array
(
    [0] => 4
    [1] => 1
    [2] => 2
    [3] => 5
    [4] => 43
    [5] => 63
    [6] => 66
)

I will appreciate if some champ would fix this as i spend 4 hours thinking whats going wrong.

Thanks

Raheel
  • 8,716
  • 9
  • 60
  • 102
  • `sort()` and done?! – Rizier123 May 31 '16 at 22:13
  • Yeah, while doing production work i will obviously do this. Since i lack in algorithms so this is just for my increasing my skill – Raheel May 31 '16 at 22:14
  • which algo are you trying to use? this looks vaguely like an attempt at bubble, but I'm rusty – Jeff Puckett May 31 '16 at 22:14
  • 4
    See: http://stackoverflow.com/q/17364127/3933332 – Rizier123 May 31 '16 at 22:15
  • Haha at first i had intention to do bubble but later on i changed my mind to attempt it this way – Raheel May 31 '16 at 22:15
  • 1
    The Golden Rule for Questions: **"Imagine You’re Trying To Answer The Question"** – Pedro Lobito May 31 '16 at 22:27
  • 1
    For one, $i + 1 doesn't exist when the iteration is on the last element in your array. So this comparison is bad: `if($array[$i] < $array[$i + 1])` – devlin carnate May 31 '16 at 22:28
  • you can see from the source that it's as sorted as it's going to get by the 13th iteration, so the `$counter > 100` is failing its purpose. I don't think `$i` is the right index for this algorithm. you need to start over since 4 < 5, it gets stuck there at the first position forever – Jeff Puckett May 31 '16 at 22:30
  • @JeffPuckettII - this could be resolved by re-inspecting array[0] at the end of the iterations. I'm not saying this is the best choice for sorting, but it would fix the problem where 4 gets 'stuck'. – devlin carnate May 31 '16 at 22:37
  • @devlincarnate I think "re-inspecting array[0]" is what I was getting at by "need to start over" – Jeff Puckett May 31 '16 at 22:38

3 Answers3

1

you're setting $i = 0; but then the loop increments it! you should set it to -1 instead.

<?php

$array = [4,5,63,2,1,66,43];
$count = count($array);
$counter = 0;
for ($i=0; $i < count($array); $i++) {
    if($counter > 100)
        break;
    // this ends the program when complete
    if (!isset($array[$i + 1])) exit;
    if($array[$i] < $array[$i + 1]) {
        continue;
    } else {
        $now_i = $array[$i];
        unset($array[$i]);
        $array[] = $now_i;
        $array = array_values($array);
        // this is the fix
        $i = -1;
    }
    print_r($array);
    $counter++;
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
0
$array = [4,5,63,2,1,66,43];
sort($array);
echo "<pre>".print_r($array,1)."</pre>";
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
[4] => 43
[5] => 63
[6] => 66
)
Jeff
  • 9
  • 5
-1

plese try this below code

$array = [4,5,63,2,1,66,43];
sort($array);
$len = count($array);

for($x=0;$x<$len;$x++)
{
    echo $array[$x];
    echo '<br>';
}

output 1 2 4 5 43 63 66

Luke
  • 77
  • 8