Skip to content

Instantly share code, notes, and snippets.

@kentatogashi
Created July 24, 2013 22:19
Show Gist options
  • Save kentatogashi/6075135 to your computer and use it in GitHub Desktop.
Save kentatogashi/6075135 to your computer and use it in GitHub Desktop.
Quick Sort
<?php
/*
* QuickSort
* &$a:リファレンス渡し
* */
function quickSort(&$numArr,$left = 0,$right)
{
//(1)
if($left >= $right) {
return;
}
//(2)
swap($numArr,$left,ceil(($left+$right)/2));
$last = $left;
//(3)
for($i = $left+1;$i<=$right;$i++) {
if($numArr[$i] < $numArr[$left]) {
swap($arrNum,++$last,$i);
}
}
//(4)
swap($numArr,$left,$last);
//(5)
quickSort($numArr,$left,$last - 1);
//(6)
quickSort($numArr,$last + 1,$right);
}
function swap(&$numArr,$left,$right)
{
$tmp = $numArr[$right];
$numArr[$right] = $numArr[$left];
$numArr[$left] = $tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment