answersLogoWhite

0

Bubble sort in PHP

Updated: 9/15/2023
User Avatar

Wiki User

15y ago

Best Answer

Here it is:

$array_size = 250;

// If you use v4.2.0 or lower uncomment this

// srand((double)microtime()*1000000);

// Generate $array_size random numbers to be sorted.

for($x = 0; $x < $array_size; $x++)

$ran[$x] = rand(0, 500);

/* The bubble sort method. If you don't know how it works it's very

* simple, values are switched one at a time for each element. */

for($x = 0; $x < $array_size; $x++) {

for($y = 0; $y < $array_size; $y++) {

if($ran[$x] < $ran[$y]) {

$hold = $ran[$x];

$ran[$x] = $ran[$y];

$ran[$y] = $hold;

}

}

}

for($x = 0; $x < $array_size; $x++)

print $ran[$x] . "<br>";

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Bubble sort in PHP
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is another name for bubble sort?

Bubble sort is also known as sinking sort.


What is a PHP certificate?

PHP certification is received after doing some sort of PHP course, there are many out there to do. You can use them in your CV to help you get a job in PHP.


How do you sort variable in ascending or descending order both in php?

Assuming that the values are stored in an array, you can use the php function sort($array) to sort ascending, and rsort to sort descending. The following link gives a table that lists all of the built in PHP sort functions: http://php.net/manual/en/array.sorting.php


Who invented the bubble sort?

ramesh


Bubble sort Extra or in space?

Bubble sort is an "in place" algorithm. Other than a temporary "switch" variable, no extra space is required.


What is bubble sort and explain with examples?

bubbles


Can you draw a flow chart for bubble sort?

no


What are some sorting rules for math?

Binary sort and bubble sort are two.


How do you sort the given contents of an array?

You would sort the given elements of an array by a bubble sort or heap sort code!!


What are the advantages for bubble sort?

Bubble sort has no practical applications other than that it is often cited as an example of how not to write an algorithm. Insert sort is the best algorithm for sorting small lists of items and is often used in conjunction with quick sort to sort larger lists. Like insert sort, bubble sort is simple to implement and is a stable sort (equal items remain in the same order they were input). However, insert sort uses copy or move operations rather than swaps (which is actually three operations per swap) and is therefore quicker. The only time a bubble sort will work quicker than insert sort is when the array is already sorted, which renders the entire algorithm redundant. A modified algorithm that specifically tests if an array is sorted or not would be more efficient than a single-pass bubble sort.


What is a PHP function that can sort arrays by other arrays?

You cannot sort arrays by other arrays; that wouldn't make sense, anyway.


Can you modify the bubble sort algorithm to search to sort an array of characters instead of array of integers?

The bubble sort algorithm can be applied to an array of characters. Every character can be translated to an integer equivalent via the ascii table