Heapsort (PHP)

Source: Internet
Author: User
Heapsort (PHP) is a program for heap sorting. <P> & lt ;? <Br/> // heap sorting application <br/> classheapsort <br/> & nbsp ;{< br exercises a heap sorting program

// Heap sorting application
Class heapsort
{
Var $;
Function setarray ($ a) // retrieves an array
{
$ This-> a = $;
}
Function runvalue ($ B, $ c) // $ a indicates the array, $ B indicates the sorting heap, and $ c indicates the end point,
{
While ($ B <$ c)
{
$ H1 = 2 * $ B;
$ H2 = (2 * $ B + 1 );
If ($ h1> $ c)
Break;
Elseif ($ h1 = $ c)
{
If ($ this-> a [$ B]> $ this-> a [$ h1])
{
$ T = $ this-> a [$ B];
$ This-> a [$ B] = $ this-> a [$ h1];
$ This-> a [$ h1] = $ t;
$ La = 1;
}
Else
$ La = 1;
}
Elseif ($ this-> a [$ B]> $ this-> a [$ h1]) | ($ this-> a [$ B]> $ this-> a [$ h2])
{
If ($ this-> a [$ h1] >=$ this-> a [$ h2])
{
$ T = $ this-> a [$ h2];
$ This-> a [$ h2] = $ this-> a [$ B];
$ This-> a [$ B] = $ t;
$ B = $ h2;
}
Else
{
$ T = $ this-> a [$ h1];
$ This-> a [$ h1] = $ this-> a [$ B];
$ This-> a [$ B] = $ t;
$ B = $ h1;
}
}
Else
$ La = 1;
If ($ la = 1)
Break;
}
}
Function getarray ()
{
$ All = count ($ this-> );
$ B = Floor ($ all-1)/2 );
For ($ I = $ B; $ I >=1; $ I --) // create arrays first
{
$ This-> runvalue ($ I, ($ all-1 ));
}
For ($ I = 1; $ I <$ all; $ I ++)
{
$ A1 = ($ all-$ I );
If ($ I = 1)
{
$ T = $ this-> a [1];
$ This-> a [1] = $ this-> a [$ a1];
$ This-> a [$ a1] = $ t;
}
Else
{
$ End = ($ all-$ I );
$ This-> runvalue (1, $ end );
$ T = $ this-> a [1];
$ This-> a [1] = $ this-> a [$ end];
$ This-> a [$ end] = $ t;
}
}
Return $ this->;
}
}
//////
Class sortarr
{
Var $;
Function setarray ($ a) // retrieves an array
{
$ This-> a = $;
}
Function runvalue ($ I)
{
$ Max = $ this-> a [$ I];
$ Id = $ I;
For ($ j = ($ I + 1); $ j A); $ j ++)
{
If ($ this-> a [$ j]> $ max)
{
$ Max = $ this-> a [$ j];
$ Id = $ j;
}
}
If ($ id! = $ I)
{
$ T = $ this-> a [$ id];
$ This-> a [$ id] = $ this-> a [$ I];
$ This-> a [$ I] = $ t;
}
}
Function getarray ()
{
For ($ I = 1; $ I <(count ($ this-> a)-1); $ I ++)
$ This-> runvalue ($ I );
Return $ this->;
}
}
//////
$ S = microtime ();
$ St = explode ('', $ s );
$ St1 = $ st [0];
$ St2 = $ st [1];
//////
$ V = 10000; // sorting array length
$ Brr [0] = 0;
For ($ I = 1; $ I <$ v; $ I ++)
{
$ Brr [$ I] = rand ();
}
$ Check = 2; // 1 stand for heapsort 2 stand for another sort
Echo 'after sort !!
';
If ($ check = 1)
{
$ Arr = new heapsort;
$ Arr-> setarray ($ brr );
$ OK = $ arr-> getarray ();
For ($ I = 1; $ I <$ v; $ I ++)
{
$ J = ($ I + 1)> ($ V-1 ))? ($ V-1) :( $ I + 1 ));
/*
If ($ OK [$ j] <$ OK [$ I])
Echo ''. $ OK [$ I].'
';
Else
Echo $ OK [$ I].'
';*/
}
}
Elseif ($ check = 2)
{
$ Arr = new sortarr;
$ Arr-> setarray ($ brr );
$ OK = $ arr-> getarray ();
For ($ I = 1; $ I <$ v; $ I ++)
{
$ J = ($ I + 1)> ($ V-1 ))? ($ V-1) :( $ I + 1 ));/*
If ($ OK [$ j] <$ OK [$ I])
Echo ''. $ OK [$ I].'
';
Elseif ($ OK [$ j]> $ OK [$ I])
Echo ''. $ OK [$ I].'
';
Else
Echo $ OK [$ I].'
';*/
}
}
Elseif ($ check = 3)
{
Sort ($ brr );
$ OK = $ brr;
For ($ I = 1; $ I <$ v; $ I ++)
{
$ J = ($ I + 1)> ($ V-1 ))? ($ V-1) :( $ I + 1 ));/*
If ($ OK [$ j] <$ OK [$ I])
Echo ''. $ OK [$ I].'
';
Elseif ($ OK [$ j]> $ OK [$ I])
Echo ''. $ OK [$ I].'
';
Else
Echo $ OK [$ I].'
';*/
}
}
Else
{
Echo 'parameter input error !!
';
}
//////
$ S = microtime ();
$ St = explode ('', $ s );
$ Sta = $ st [0];
$ Stb = $ st [1];
$ Ss1 = $ sta-$ st1;
$ Ss2 = $ stb-$ st2;
If ($ check = 1)
$ Word = 'heap sorted ';
Elseif ($ check = 2)
$ Word = 'regular sorted ';
Elseif ($ check = 3)
$ Word = 'normal sorted ';
Else
$ Word = 'nonsorted ';
Echo $ word. 'consumes'. ($ ss2 + $ ss1). 'second time' to sort arrays with '. $ v.' elements ';
//////
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.