<span style=
"color: #008080;"
> 1</span> <span style=
"color: #0000ff;"
>
function
</span> quickSort(<span style=
"color: #0000ff;"
>
array
</span> <span style=
"color: #800080;"
>
$array
</span><span style=
"color: #000000;"
>)
</span><span style=
"color: #008080;"
> 2</span> <span style=
"color: #000000;"
>{
</span><span style=
"color: #008080;"
> 3</span> <span style=
"color: #800080;"
>
$len
</span> = <span style=
"color: #008080;"
>
count
</span>(<span style=
"color: #800080;"
>
$array
</span><span style=
"color: #000000;"
>);
</span><span style=
"color: #008080;"
> 4</span> <span style=
"color: #0000ff;"
>
if
</span>(<span style=
"color: #800080;"
>
$len
</span> )
<span style=
"color: #008080;"
> 5</span> <span style=
"color: #000000;"
> {
</span><span style=
"color: #008080;"
> 6</span> <span style=
"color: #0000ff;"
>
return
</span> <span style=
"color: #800080;"
>
$array
</span><span style=
"color: #000000;"
>;
</span><span style=
"color: #008080;"
> 7</span> <span style=
"color: #000000;"
> }
</span><span style=
"color: #008080;"
> 8</span> <span style=
"color: #800080;"
>
$key
</span> = <span style=
"color: #800080;"
>
$array
</span>[0<span style=
"color: #000000;"
>];
</span><span style=
"color: #008080;"
> 9</span> <span style=
"color: #800080;"
>
$left
</span> = <span style=
"color: #0000ff;"
>
array
</span><span style=
"color: #000000;"
>();
</span><span style=
"color: #008080;"
>10</span> <span style=
"color: #800080;"
>
$right
</span> = <span style=
"color: #0000ff;"
>
array
</span><span style=
"color: #000000;"
>();
</span><span style=
"color: #008080;"
>11</span> <span style=
"color: #0000ff;"
>
for
</span>(<span style=
"color: #800080;"
>
$i
</span>=1; <span style=
"color: #800080;"
>
$i
</span>
$len
; ++<span style=
"color: #800080;"
>
$i
</span><span style=
"color: #000000;"
>)
</span><span style=
"color: #008080;"
>12</span> <span style=
"color: #000000;"
> {
</span><span style=
"color: #008080;"
>13</span> <span style=
"color: #0000ff;"
>
if
</span>(<span style=
"color: #800080;"
>
$array
</span>[<span style=
"color: #800080;"
>
$i
</span>]
$key
<span style=
"color: #000000;"
>)
</span><span style=
"color: #008080;"
>14</span> <span style=
"color: #000000;"
> {
</span><span style=
"color: #008080;"
>15</span> <span style=
"color: #800080;"
>
$left
</span>[] = <span style=
"color: #800080;"
>
$array
</span>[<span style=
"color: #800080;"
>
$i
</span><span style=
"color: #000000;"
>];
</span><span style=
"color: #008080;"
>16</span> <span style=
"color: #000000;"
> }
</span><span style=
"color: #008080;"
>17</span> <span style=
"color: #0000ff;"
>
else
</span>
<span style=
"color: #008080;"
>18</span> <span style=
"color: #000000;"
> {
</span><span style=
"color: #008080;"
>19</span> <span style=
"color: #800080;"
>
$right
</span>[] = <span style=
"color: #800080;"
>
$array
</span>[<span style=
"color: #800080;"
>
$i
</span><span style=
"color: #000000;"
>];
</span><span style=
"color: #008080;"
>20</span> <span style=
"color: #000000;"
> }
</span><span style=
"color: #008080;"
>21</span> <span style=
"color: #000000;"
> }
</span><span style=
"color: #008080;"
>22</span> <span style=
"color: #800080;"
>
$left
</span> = quickSort(<span style=
"color: #800080;"
>
$left
</span><span style=
"color: #000000;"
>);
</span><span style=
"color: #008080;"
>23</span> <span style=
"color: #800080;"
>
$right
</span> = quickSort(<span style=
"color: #800080;"
>
$right
</span><span style=
"color: #000000;"
>);
</span><span style=
"color: #008080;"
>24</span> <span style=
"color: #0000ff;"
>
return
</span> <span style=
"color: #008080;"
>
array_merge
</span>(<span style=
"color: #800080;"
>
$left
</span>, <span style=
"color: #0000ff;"
>
array
</span>(<span style=
"color: #800080;"
>
$key
</span>), <span style=
"color: #800080;"
>
$right
</span><span style=
"color: #000000;"
>);
</span><span style=
"color: #008080;"
>25</span> <span style=
"color: #000000;"
>}
</span><span style=
"color: #008080;"
>26</span>
<span style=
"color: #008080;"
>27</span> <span style=
"color: #0000ff;"
>
print
</span>
'<pre class="brush:php;toolbar:false">'
<span style=
"color: #000000;"
>;
</span><span style=
"color: #008080;"
>28</span> <span style=
"color: #008080;"
>print_r</span>(quickSort(<span style=
"color: #0000ff;"
>
array
</span>(1,4,22,5,7,6,9<span style=
"color: #000000;"
>)));
</span><span style=
"color: #008080;"
>29</span> <span style=
"color: #0000ff;"
>
print
</span> '