bubble sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ehcy
    New Member
    • Oct 2007
    • 22

    bubble sort

    hello everyone i need your help about my simple program exercise.. this is my created php code..
    [code=php]
    <?php

    echo "<u>Output: </u></br></br>";

    echo $_POST["num"]. "</br></br>";

    ?>


    <html>
    <head>

    <title></title>
    </head>
    <body>
    <h5>Input 10 Numbers:</h5>
    <form action = "9.php" method = "POST">
    <p style="backgrou nd-color:lavender; padding:10px;wi dth=10%">
    <input type = "box" name = "num" value = "<?php echo $num; ?>" />
    <p><input type = "submit" value = "Enter" onclick = "SortedArra y()"/>
    <span id="SortedArray Out"></span>
    </form>

    </body>
    </html>
    [/code]
    i need to sort the 10 numbers that i'll input the textbox but i don't know how.. and i need to declare the highest and lowest number.. example..
    i'll input
    2 56 3 76 35 54 4 7 31 0..

    the output will be
    0, 2, 3, 4, 7, 31, 35, 54, 56, 76

    therefore.. 0 is lower than 2 and 3 is higher than 2...

    tnx again!!!! godbless you all!!!!
    Last edited by ak1dnar; Oct 17 '07, 03:21 PM. Reason: [code=php] .......... [/code] is missing
  • hawkenterprises
    New Member
    • Oct 2007
    • 10

    #2
    I hope this isn't homework however

    [php]
    $arr = explode(' ',$_POST['num']); //assumes space delimted
    arsort($arr);
    print_r($arr);
    [/php]

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      sort() will do the job but arsort() is for reverse order.
      [CODE=php]
      <?php
      echo "<u>Output: </u></br></br>";
      $arr = explode(' ',$_POST['num']); //assumes space delimted
      sort($arr);

      echo implode(',',$ar r );

      ?>


      <html>
      <head>

      <title></title>
      </head>
      <body>
      <h5>Input 10 Numbers:</h5>
      <form action = "<?php $PHP_SELF ?>" method = "POST">
      <p style="backgrou nd-color:lavender; padding:10px;wi dth=10%">
      <input type = "box" name = "num" value = "<?php echo $num; ?>" />
      <p><input type = "submit" value = "Enter" onclick = "SortedArra y()"/>
      <span id="SortedArray Out"></span>
      </form>

      </body>
      </html>
      [/CODE]

      Comment

      • ehcy
        New Member
        • Oct 2007
        • 22

        #4
        function swap (x,y)

        how can i put the lowest number in left side one by one?

        example.. i'll put the 5 numbers in input box..

        5 23 4 6 8

        number 4 is lower than 23 so it will move at left sides of number 23 and number 5.. it will be come..

        4 5 23 6 8

        then the next is 5.. the question will ask.. 5 is higher than 4 and lower than 23..

        then the next is 6.. 6 is lower than 23 so it will move at 23's left's side..

        4 5 6 23 8

        then the next is 8.. 8 is lower than 23, so it will move at left side..

        4 5 6 8 23..

        then it says.. 4 is lowest number and 23 is the highest number..

        plz help me.. this is my first time to used php that's why i'm 0 knowledge bout this..

        thankx!!!!c",

        Comment

        • JeremyMiller
          New Member
          • Sep 2007
          • 69

          #5
          How about this?

          [php]
          <?php
          $numbers = "5 23 4 6 8";
          $number_array = explode(" ",$numbers) ;
          asort($number_a rray);
          echo "Numbers sorted: ".implode(" ",$number_array );
          ?>
          [/php]

          Comment

          • ehcy
            New Member
            • Oct 2007
            • 22

            #6
            but.. i have a textbox and i need to input the 5 numbers in textbox.. not in program.. so it depend on me what numbers i will input.. i used $_POST["num"] as a name of textbox.. thank you!

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              [php]
              <?php
              $number_array = explode(" ",$_POST['num']);
              asort($number_a rray);
              echo "Numbers sorted: ".implode(" ",$number_array );
              ?>
              [/php]

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Bubble sort works like this:

                Find the size of your array: $size.
                Analyze your array, starting at index 0, moving to index $size - 1, and find the position of your largest element: $maxPos.
                Swap the values of position $size and position $maxPos.
                Decrement $size: $size--.
                Repeat until $size == 0.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Merged duplicate threads.

                  Comment

                  • ehcy
                    New Member
                    • Oct 2007
                    • 22

                    #10
                    thank you.. it's worked but i need it manually.. the code you gave to me is automatically sorted.. how can i separate the 5 numbers i will input in textbox?..

                    example.. i'll input in textbox..

                    5 2 3 54 4

                    the output will be..
                    -----------------------
                    5 2 3 54 4
                    2 is lower than 5
                    -----------------------

                    and the number of 2 will move at the left side of 5..
                    the output will be...
                    ------------------------
                    2 5 3 54 4
                    2 is lower than 5, ang 5 is higher than 2

                    thank you again..

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      Originally posted by ehcy
                      thank you.. it's worked but i need it manually.. the code you gave to me is automatically sorted.. how can i separate the 5 numbers i will input in textbox?..

                      example.. i'll input in textbox..

                      5 2 3 54 4

                      the output will be..
                      -----------------------
                      5 2 3 54 4
                      2 is lower than 5
                      -----------------------

                      and the number of 2 will move at the left side of 5..
                      the output will be...
                      ------------------------
                      2 5 3 54 4
                      2 is lower than 5, ang 5 is higher than 2

                      thank you again..
                      Please read through my second post in this thread, I delineate the algorithm there for you.
                      Last edited by Motoma; Oct 22 '07, 01:29 PM.

                      Comment

                      • ehcy
                        New Member
                        • Oct 2007
                        • 22

                        #12
                        Originally posted by Motoma
                        Bubble sort works like this:

                        Find the size of your array: $size.
                        Analyze your array, starting at index 0, moving to index $size - 1, and find the position of your largest element: $maxPos.
                        Swap the values of position $size and position $maxPos.
                        Decrement $size: $size--.
                        Repeat until $size == 0.

                        how can i convert it in php code?.. sori but i really don't know because i'm 0 knowledge in php that's why it's difficult for me.. plz show me some example of it.. plz.. i have no idea.. tnx!

                        Comment

                        • ehcy
                          New Member
                          • Oct 2007
                          • 22

                          #13
                          sorting numbers manually

                          hello! it's me again.. i want to know how can i sort the numbers manually by not using sort.. only using substr and strpos

                          Comment

                          • JeremyMiller
                            New Member
                            • Sep 2007
                            • 69

                            #14
                            There are a number of sorting algorithms. See http://en.wikipedia.org/wiki/Sorting_algorithm for a list of them. Is there a particular one you want implemented. Also, why are you making this harder on yourself?
                            Last edited by pbmods; Oct 22 '07, 12:24 PM. Reason: Fixed link.

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Ehcy.

                              The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. You have not asked a question. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

                              Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

                              Then when you are ready post a new question in this thread.

                              MODERATOR

                              Comment

                              Working...