473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

bubble sort

22
hello everyone i need your help about my simple program exercise.. this is my created php code..
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. echo "<u>Output:</u></br></br>";
  4.  
  5. echo $_POST["num"]. "</br></br>";
  6.  
  7. ?>
  8.  
  9.  
  10. <html>
  11. <head>
  12.  
  13. <title></title>
  14. </head>
  15. <body>
  16. <h5>Input 10 Numbers:</h5>
  17. <form action = "9.php" method = "POST">
  18. <p style="background-color:lavender;padding:10px;width=10%">
  19. <input type = "box" name = "num" value = "<?php echo $num; ?>" />
  20. <p><input type = "submit" value = "Enter" onclick = "SortedArray()"/>
  21. <span id="SortedArrayOut"></span>
  22. </form>
  23.  
  24. </body>
  25. </html>
  26.  
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!!!!
Oct 17 '07 #1
16 3117
I hope this isn't homework however

[php]
$arr = explode(' ',$_POST['num']); //assumes space delimted
arsort($arr);
print_r($arr);
[/php]
Oct 17 '07 #2
ak1dnar
1,584 Expert 1GB
sort() will do the job but arsort() is for reverse order.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "<u>Output:</u></br></br>";
  3. $arr = explode(' ',$_POST['num']); //assumes space delimted
  4. sort($arr);
  5.  
  6. echo implode(',',$arr );
  7.  
  8. ?>
  9.  
  10.  
  11. <html>
  12. <head>
  13.  
  14. <title></title>
  15. </head>
  16. <body>
  17. <h5>Input 10 Numbers:</h5>
  18. <form action = "<?php $PHP_SELF ?>" method = "POST">
  19. <p style="background-color:lavender;padding:10px;width=10%">
  20. <input type = "box" name = "num" value = "<?php echo $num; ?>" />
  21. <p><input type = "submit" value = "Enter" onclick = "SortedArray()"/>
  22. <span id="SortedArrayOut"></span>
  23. </form>
  24.  
  25. </body>
  26. </html>
  27.  
Oct 17 '07 #3
ehcy
22
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",
Oct 18 '07 #4
How about this?

[php]
<?php
$numbers = "5 23 4 6 8";
$number_array = explode(" ",$numbers);
asort($number_array);
echo "Numbers sorted: ".implode(" ",$number_array);
?>
[/php]
Oct 18 '07 #5
ehcy
22
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!
Oct 18 '07 #6
Motoma
3,237 Expert 2GB
[php]
<?php
$number_array = explode(" ",$_POST['num']);
asort($number_array);
echo "Numbers sorted: ".implode(" ",$number_array);
?>
[/php]
Oct 18 '07 #7
Motoma
3,237 Expert 2GB
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.
Oct 18 '07 #8
pbmods
5,821 Expert 4TB
Merged duplicate threads.
Oct 18 '07 #9
ehcy
22
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..
Oct 19 '07 #10
Motoma
3,237 Expert 2GB
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.
Oct 19 '07 #11
ehcy
22
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!
Oct 22 '07 #12
ehcy
22
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
Oct 22 '07 #13
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?
Oct 22 '07 #14
pbmods
5,821 Expert 4TB
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
Oct 22 '07 #15
pbmods
5,821 Expert 4TB
Merged duplicate threads.
Oct 22 '07 #16
Motoma
3,237 Expert 2GB
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!
Sit down and do some reading on PHP. You will need to know PHP before we can help you write PHP code.

Then work through the problem step by step: a good start would be to post the code you wrote to accomplish the first line of my instructions.
Oct 22 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
9
by: mosullivan | last post by:
I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. #include <stdio.h>...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.