Skip to content
This repository was archived by the owner on Dec 29, 2019. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions quick_sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
function quick_sort($my_array)
{
$loe = $gt = array();
if(count($my_array) < 2)
{
return $my_array;
}
$pivot_key = key($my_array);
$pivot = array_shift($my_array);
foreach($my_array as $val)
{
if($val <= $pivot)
{
$loe[] = $val;
}elseif ($val > $pivot)
{
$gt[] = $val;
}
}
return array_merge(quick_sort($loe),array($pivot_key=>$pivot),quick_sort($gt));
}
$temp = fopen("php://stdin","r");
$test = (int)fgets($temp);
//echo $test;
$my_array = array();
for ($i = 0; $i < $test; $i++) {
$temp = fopen("php://stdin","r");
array_push($my_array, (int)fgets($temp));
}
//$my_array = array(5, 0, 2, 5, -1, 4, 1);
echo ('Original Array : '.implode(',',$my_array)."\n");
$my_array = quick_sort($my_array);
echo ('Sorted Array : '.implode(',',$my_array)."\n");
?>