Search
 
SCRIPT & CODE EXAMPLE
 

PHP

heap sort php

<?php
 function build_heap(&$array, $i, $t){
  $tmp_var = $array[$i];    
  $j = $i * 2 + 1;

  while ($j <= $t)  {
   if($j < $t)
    if($array[$j] < $array[$j + 1]) {
     $j = $j + 1; 
    }
   if($tmp_var < $array[$j]) {
    $array[$i] = $array[$j];
    $i = $j;
    $j = 2 * $i + 1;
   } else {
    $j = $t + 1;
   }
  }
  $array[$i] = $tmp_var;
 }

 function heap_sort(&$array) {
  //This will heapify the array
  $init = (int)floor((count($array) - 1) / 2);
  // Thanks jimHuang for bug report
  for($i=$init; $i >= 0; $i--){
   $count = count($array) - 1;
   build_heap($array, $i, $count);
  }

  //swaping of nodes
  for ($i = (count($array) - 1); $i >= 1; $i--)  {
   $tmp_var = $array[0];
   $array [0] = $array [$i];
   $array [$i] = $tmp_var;
   build_heap($array, 0, $i - 1);
  }
 }

// Demo
$array = array(9,8,7,6,5,4,3,2,1,0,10,1000,0);
heap_sort($array);
print_r($array);
?>
Comment

PREVIOUS NEXT
Code Example
Php :: PHP Parses a time string according to a specified format 
Php :: Use external variable in array_filter 
Php :: wordpress how to display breadcrumb in child theme programmatically 
Php :: user order by role spatie laravel 
Php :: intellisense in visual studio code for php-oop 
Php :: wpdb get column 
Php :: wc php get acf fields product category 
Php :: laravel validation if another record is not deleted / not null 
Php :: check array has keys in php 
Php :: Find category name & link 
Php :: log magento 1 
Php :: octobercms mail register 
Php :: How to Delete Multiple Records using Checkbox in Laravel 
Php :: Get a list of the arrays keys 
Php :: + php quantifer 
Php :: required_unless laravel 
Php :: php insert char before each letter using regex 
Php :: laravel reroute 419 
Php :: mysql between all months days even null 
Php :: Route pattern cannot reference variable name more than once. laravel 
Php :: Add current year on WordPress using Shortcode 
Php :: How to remove from a multidimensional array all duplicate elements including the original 
Php :: Web servers supported by php 
Php :: PHP Iterables 
Php :: google recaptcha varification in php codeigniter 
Php :: wp add_action 
Php :: The Process class relies on proc_open, which is not available on your PHP installation cpanel 
Php :: date subtraction php 
Php :: server.php not found 
Php :: firebase realtime database get all data 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =