What sort algorithm does PHP use?

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

You could find the information by looking at the php manual. http://php.net/sort says PHP uses an implementation of Quicksort. Failing that, you could always trudge through the PHP source code itself.,Internally speaking, which algorithm(s) does PHP use to implement the various sort functions it offers? It seems like the usort variants might use a different algorithm than the built in sorts, but I wanted to know.,For sorting, PHP uses an implementation of quicksort that can be found in Zend/zend_sort.c, which takes a comparison function and an array of elements. The default comparison function for sort() is defined in ext/standard/array.c and is called php_array_data_compare(). So basically, it's the same algorithm for all sorting functions, except that they take different comparison functions., Did the universe need the presence of matter and radiation to start expanding?

load more v
88%

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.,You can test how long your algorithm takes to run by using the following PHP benchmarking function. This will convert the result of the php function microtime() into a float value.,There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations, especially when sorting things like dates as PHP usually guesses which value is higher than the other and can produce odd results. However, there are plenty of sorting algorithms available than can allow you to sort an array in any way you want., Programming, the need to order the collections of data or objects that must then be manipulated often arises. Ordering a list can be useful in cases where you have to do…

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.

function bubbleSort($array) {
   if (!$length = count($array)) {
      return $array;
   }
   for ($outer = 0; $outer < $length; $outer++) {
      for ($inner = 0; $inner < $length; $inner++) {
         if ($array[$outer] < $array[$inner]) {
            $tmp = $array[$outer];
            $array[$outer] = $array[$inner];
            $array[$inner] = $tmp;
         }
      }
   }
}
load more v
72%

Note: Like most PHP sorting functions, sort() uses an implementation of » Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays. This is however an implementation detail you shouldn't rely on. ,The comparison of array sorting functions, The fruits have been sorted like natcasesort(). ,Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
load more v
65%

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.,You can test how long your algorithm takes to run by using the PHP benchmarking function.,There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations, especially when sorting things like dates as PHP usually guesses which value is higher than the other and can produce odd results. However, there are plenty of sorting algorithms available than can allow you to sort an array in any way you want.,PHP has a built in debugging and testing tool called assertions, which is essentially the assert() function and a few configuration options.

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.

function bubbleSort($array) {
   if (!$length = count($array)) {
      return $array;
   }
   for ($outer = 0; $outer < $length; $outer++) {
      for ($inner = 0; $inner < $length; $inner++) {
         if ($array[$outer] < $array[$inner]) {
            $tmp = $array[$outer];
            $array[$outer] = $array[$inner];
            $array[$inner] = $tmp;
         }
      }
   }
}
load more v
75%

The sort() function is an inbuilt function in PHP and is used to sort an array in ascending order i.e, smaller to greater. It sorts the actual array and hence changes are reflected in the original array itself. The function provides us with 6 sorting types, according to which the array can be sorted.,Program 3 : Program to demonstrate the use of sort() function to sort the string case-insensitively.,Program 2 : Program to demonstrate the use of sort() function to sort the string case-sensitively.,Return Value: It returns a boolean value, TRUE on success and False in failure. It sorts the original array in ascending order which is passed as a parameter.

Syntax:

bool sort($array, sorting_type)

Input: $array = [3, 4, 1, 2]
Output:
   Array(
      [0] => 1[1] => 2[2] => 3[3] => 4
   )

Input: $array = ["geeks2", "raj1", "striver3", "coding4"]
Output:
   Array(
      [0] => coding4[1] => geeks2[2] => raj1[3] => striver3
   )

Output:

Array
   (
      [0] => 1[1] => 2[2] => 3[3] => 4
   )

Output:

Array
   (
      [0] => RAj[1] => Raj[2] => coding[3] => geeks[4] => striver
   )

Output:

Array
   (
      [0] => coding[1] => geeks[2] => Raj[3] => RAj[4] => striver
   )
load more v
40%

13. Write a PHP program to sort a list of elements using Bead sort.Go to the editor Click me to see the solution,12. Write a PHP program to sort a list of elements using Radix sort. Go to the editor Click me to see the solution,10. Write a PHP program to sort a list of elements using Bucket sort. Go to the editor Click me to see the solution,Searching and Sorting

Example:

$obj = (object) array('qualitypoint', 'technologies', 'India');
load more v
22%

Each bucket is sorted individually. It is possible to use another sorting algorithm, or continue to use bucket sorting in a recursive manner.,Insertion sorting is a simple and intuitive sorting algorithm.,Selective sorting is a simple and intuitive sorting algorithm.,It is not a stable sorting algorithm: selective sorting, quick sorting, Hill sorting and heap sorting

function quickSort($arr) {
   $len = count($arr);
   //Set the end condition first to judge whether to continue
   if ($len <= 1) {
      return $arr;
   }

   //Select the first element as the base element
   $pivot = $arr[0];

   //Initialize left array
   $left = $right = array();

   //Initializes a right array larger than the base element
   $right = array();

   //Traverse all elements except the reference element and put them into the left and right arrays according to the size relationship
   for ($i = 1; $i < $len; $i++) {
      if ($arr[$i] < $pivot) {
         $left[] = $arr[$i];
      } else {
         $right[] = $arr[$i];
      }
   }

   //Then sort the left and right arrays in the same way
   $left = quickSort($left);
   $right = quickSort($right);

   //Merge base elements and left and right arrays
   return array_merge($left, array($pivot), $right);
}
load more v
60%

The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.,The following example sorts the elements of the $cars array in descending alphabetical order:,The following example sorts the elements of the $cars array in ascending alphabetical order:,The following example sorts the elements of the $numbers array in descending numerical order:

$colors = array("red", "green", "blue", "yellow");;

Other "undefined-undefined" queries related to "What sort algorithm does PHP use?"