PHP Examples

PHP Program - Radix Sort



Radix sort is based on the idea that the sorting of the input data is done digit by digit from least significant digit to most significant digit and it uses counting sort as a subroutine to perform sorting. Counting sort is a linear sorting algorithm with overall time complexity Θ(N+K) in all cases, where N is the number of elements in the unsorted array and K is the range of input data. The idea of radix sort is to extend the counting sort algorithm to get a better time complexity when K goes up.

Example:

To understand the radix sort, lets consider an unsorted array A = [101, 1, 20, 50, 9, 98, 27, 153, 35, 899] and discuss each step taken to sort the array in ascending order.

Step 1: According to the algorithms, the input data is first sorted based on least significant digit. Therefore, array A[ ] is sorted based on one's digit. After sorting it on one's digit it will become [20, 50, 101, 1, 153, 35, 27, 98, 9, 899].

Radix Sort

Step 2: In this step, the array A[ ] array is sorted based on ten's digit and after this step it will become [101, 1, 9, 20, 27, 35, 50, 153, 98, 899].

Radix Sort

Step 3: Finally, the array A[ ] is sorted based on hundred's digit (most significant digit) and the array will be sorted after this step and the final array will be [1, 9, 20, 27, 35, 50, 98, 101, 153, 899].

Radix Sort

Implementation of Radix Sort

<?php
// function for radix sort
function radixsort(&$Array, $n) {
  $max = $Array[0];
  //find largest element in the Array
  for ($i=1; $i<$n; $i++) {  
    if($max < $Array[$i])
      $max = $Array[$i];
  }

  //Counting sort is performed based on place. 
  //like ones place, tens place and so on.
  for ($place = 1; $max/$place > 0; $place *= 10) 
    countingsort($Array, $n, $place); 
}

function countingsort(&$Array, $n, $place) {   
  $output = array_fill(0,$n,0);
  
  //range of the number is 0-9 for each place considered.
  $freq = array_fill(0,10,0);
  
  //count number of occurrences in freq array
  for($i = 0; $i < $n; $i++)
    $freq[($Array[$i]/$place)%10]++;

  //Change count[i] so that count[i] now contains actual 
  //position of this digit in output[] 
  for ($i = 1; $i < 10; $i++) 
      $freq[$i] += $freq[$i - 1];    

  //Build the output array 
  for ($i = $n - 1; $i >= 0; $i--) { 
      $output[$freq[($Array[$i]/$place)%10] - 1] = $Array[$i]; 
      $freq[($Array[$i]/$place)%10]--; 
  }  

  //Copy the output array to the input Array, Now the Array will 
  //contain sorted array based on digit at specified place
  for ($i = 0; $i < $n; $i++) 
      $Array[$i] = $output[$i]; 
}
  
// function to print array
function PrintArray($Array, $n) { 
  for ($i = 0; $i < $n; $i++) 
    echo $Array[$i]." "; 
  echo "\n";
} 

// test the code
$MyArray = array(101, 1, 20, 50, 9, 98, 27, 153, 35, 899);
$n = sizeof($MyArray); 
echo "Original Array\n";
PrintArray($MyArray, $n);

radixsort($MyArray, $n);
echo "\nSorted Array\n";
PrintArray($MyArray, $n);
?>

The above code will give the following output:

Original Array
101 1 20 50 9 98 27 153 35 899 

Sorted Array
1 9 20 27 35 50 98 101 153 899 

Time Complexity:

The time complexity of radix sort is Θ((N+b)*logb(max)) in all cases, where:

  • N is the number of elements in unsorted array.
  • b is the base of input array, for example, for decimal system, b is 10.
  • max is the maximum element of the input array.