Jump to content

Sorting array w/o sort()


Liodel

Recommended Posts

My problem is, how do i sort an array with int values only, without using sort() function.

 

The given are:

- array which contains int values ex. (7, 8, 3, 1, 5)

 

Not given:

- the actual values of the array

- the length of the array

 

I have already written something to know the least value, but putting it all together sorted in a new array

is my problem. This is the code so far:

 

<?php

$a = array(8, 3, 6, 2, 1); // this is just a sample value for the code to test

$a_len = count($a);
$pointer = 0;

for ($i = 0; $i < $a_len; $i++) {
    if (!($a[$pointer] <= $a[$i])) {
    $pointer++;
    $i = 0;
    }
}
$b[] = $a[$pointer];

foreach ($b as $b2) echo $b2;  

?>

 

Thanks for helping.

Link to comment
https://forums.phpfreaks.com/topic/111970-sorting-array-wo-sort/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.