0

I checked all the questions which have already been asked. I did not find the suitable one. The question is to sort and rsort an array but use sort and rsort unfunction is not allowed. It supposed to be done with for loop.

 <?php
       $numberstring = $_GET['numberstring'];
       $array = explode(',',$numberstring);

       echo "Order in the beginning: $numberstring\n";

       // My code starts from here for the missing part


        for ($i=0;$i<count($array);$i++)
        {
          if ($get_largest<$array[$i])

             {$get_largest=$array[$i];
              $get_largest=range(0,count($array));//I 'm not sure of this line

         }


           if ($get_smallest>$array[$i])

           { 
               $get_smallest=$array[$i];
               $get_smallest=range(0,count($array));

           }
         }

          $largest_smallest=explode(',',$get_largest);
          $smallest_largest=explode(' ,',$get_smallest);
          // my code finished


         echo "Largest to smallest: $largest_smallest\n";
         echo "Smallest to largest: $smallest_largest\n";
         ?>
lau
  • 19
  • 4
  • Is this for a class or something? Maybe check out a bubble sort function. Uses a while loop to sort smallest to largest and keep going through as long as a shift is detected. – nwolybug Aug 27 '14 at 06:02
  • It's a self study course. Complete the following PHP script so that it prints the numbers, given in a form, in a specific order. The script should organize the numbers from largest to the smallest and from smallest to largest and print both of these number strings on screen. The points are sent to the script as a character string, where points are separated with comma (e.g. 4,5,2). Points are divided into an array with the explode-function. Using the sort-function is not allowed. Do the organizing with a for-statement. Incomplete program. – lau Aug 27 '14 at 06:04
  • and example output Example output Order in the beginning: 4,7,-2,0,6 Largest to smallest: 7,6,4,0,-2 Smallest to largest: -2,0,4,6,7 – lau Aug 27 '14 at 06:05
  • @nwolybug with your bubble sort keyword, I found selection sort. Maybe that can solve it. – lau Aug 27 '14 at 06:09
  • The bubble sort is a very inefficient sorting method because you have to go through the array multiple times to get the final result. I only mentioned it because it was something we had to study in class and create a model for it. This was probably 20 years ago... The idea is that the array is provided. The bubble starts at the first two indexes of the array and compare which one is bigger and then swaps if you are arranging bigger to smaller or reverse. If a shift is detected, a variable is set to show that you need to go through again. Repeat until no shift is detected. – nwolybug Aug 29 '14 at 06:00
  • I like C4ud3x's idea better as it will take only a single loop to get your answer. Much more efficient. I'll write an answer for mine so you can see the difference. – nwolybug Aug 29 '14 at 06:02
  • There's a terrific answer with a bunch of sorting algorithm implementations here: http://stackoverflow.com/a/17364468/476 – deceze Aug 29 '14 at 07:42

2 Answers2

0

The easiest way I can imagine would be using the min() and max() function.

Here's the example:

<?
function array_sort($input,$reverse){
    unset($new_array);
    $new_array = array(); // This will be our output where sorted values coming in //

    if($reverse == false){  // RSort or not //
        for($i=0;$i<count($input);$i){ // loop as many times as many values are stored in the array //
            $get_smallest = min($input); // get the smallest value of the input array //
            $key = array_search($get_smallest, $input); // get the index of the smallest array to unset it later //
            $new_array[] = $get_smallest; // store the smallest value in a new array //
            unset($input[$key]); // unset (delete) the extracted (smallest) value so the min()-function grabs the next one in the next loop //
        }
    }
    else{  // RSort or not //
        for($i=0;$i<count($input);$i){
            $get_biggest = max($input);
            $key = array_search($get_biggest, $input);
            $new_array[] = $get_biggest;
            unset($input[$key]);
        }
    }
    return $new_array;
}

$unsorted_array = array( 'ab','aa','ac','bf','be'); // Our test array

$output1 = array_sort($unsorted_array,false);
$output2 = array_sort($unsorted_array,true);

var_dump($output1);
echo '<br><br>';
var_dump($output2);
?>

Output: 1

array(5) {   
            [0]=> string(2) "aa"  
            [1]=> string(2) "ab"  
            [2]=> string(2) "ac"  
            [3]=> string(2) "be"  
            [4]=> string(2) "bf"  
         }

Output: 2

array(5) {   
            [0]=> string(2) "bf"  
            [1]=> string(2) "be"  
            [2]=> string(2) "ac"  
            [3]=> string(2) "ab"  
            [4]=> string(2) "aa"  
         }

So, with this function you can sort any array in normal and reversed mode. I havnt commented the reversed block. Its just the same as the block above but with max() instead of min().

Greetings.

C4d
  • 3,183
  • 4
  • 29
  • 50
0

This is the bubble sort program I learned a long time ago. Probably easier ways to do it, but it works. I've included prints so you can see what is going on as the arrays change.

<?php

$numbers = '2,24,21,2,3,77,900,1,4,5';
$array = explode(',',$numbers);

function bubblesort($numbers){
    $array['mintomax'] = $numbers;
    $array['maxtomin'] = $numbers;
    while(true){
        $shift_detected = false;
        for($i=0;$i<count($numbers)-1;$i++){
            $next_Var = $i+1;
            if($array['mintomax'][$i]>$array['mintomax'][$next_Var]){
                echo $i.': '.$next_Var.':';
                $hold_var = $array['mintomax'][$i];
                $array['mintomax'][$i] = $array['mintomax'][$next_Var];
                $array['mintomax'][$next_Var] = $hold_var;
                $shift_detected = true;
                print_r($array['mintomax']);
                echo '<br />';
            }
        }
        if(!$shift_detected){
            echo '<br /><br />';
            break;
        }
    }
    while(true){
        $shift_detected = false;
        for($i=0;$i<count($numbers);$i++){
            $next_Var = $i+1;
            if($array['maxtomin'][$i]<$array['maxtomin'][$next_Var]){
                echo $i.': '.$next_Var.':';
                $hold_var = $array['maxtomin'][$i];
                $array['maxtomin'][$i] = $array['maxtomin'][$next_Var];
                $array['maxtomin'][$next_Var] = $hold_var;
                $shift_detected = true;
                print_r($array['maxtomin']);
                echo '<br />';
            }
        }
        if(!$shift_detected){
            echo '<br /><br />';
            break;
        }
    }
    return $array;
}

print_r(bubblesort($array));

?>
nwolybug
  • 462
  • 5
  • 12