يختار الترتيب السريع قيمة محورية، ويقسّم القيم حولها، ثم يكرر الطريقة على كل جانب. هنا نختار دائمًا أول قيمة. إذا تقارب حجم الجزأين فالزمن Θ(n log n). وإذا كانت القيمة المحورية في كل مرة أصغر قيمة أو أكبرها، يرتفع الزمن إلى Θ(n²). يحدث ذلك بهذه القاعدة حتى لو كانت القيم مرتبة أصلًا.
مثال بسيط
اختر 2 قيمة محورية. انقل 1 إلى يسارها و3 إلى يمينها. الآن على كل جانب عنصر واحد.
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15
إعلانات
تتبّع المثال خطوة بخطوة
ابدأ بـ [15, 3, 12, 6, -9, 9, 0].
اختر القيمة الأولى، وهي 15، محوراً. ابدأ الفحص من الطرفين وتحرك نحو الوسط.
حرّك المؤشر الأيسر إلى اليمين ما دامت قيمته أقل من المحور أو تساويه وما دام المؤشر دون الحد الأعلى. وحرّك المؤشر الأيمن إلى اليسار ما دامت قيمته أكبر من المحور.
إذا لم يلتقِ المؤشران بعد، فبدّل القيمتين اللتين وُجدتا في الجهة الخطأ ثم واصل الفحص.
عندما يلتقي المؤشران أو يتجاوز أحدهما الآخر، بدّل القيمة الأولى مع القيمة عند المؤشر الأيمن. عندئذ يصبح المحور في موضعه النهائي.
طبّق الترتيب نفسه على القيم قبل ذلك الموضع وبعده. إذا كان الجزء يحتوي على أقل من عنصرين فهو مرتب بالفعل.
الترتيب السريع بلغة PHP خطوة بخطوة
الكود المصدري بلغة PHP
<?php
class QuickSortExample {
public function quickSort(&$arr, $low, $high) {
if($low < $high) {
// Find the pivot index of the lower bound of an array
$pivot = $this->partition($arr, $low, $high);
// Apply Divide and conquer strategy
// to sort the left side and right side of an array
// respective to the pivot position
// Left hand side array
$this->quickSort($arr, $low, $pivot - 1);
// Right hand side array
$this->quickSort($arr, $pivot + 1, $high);
}
}
private function partition(&$arr, $low, $high) {
// Pick a lowest bound element as an pivot value
$pivot = (int) $arr[$low];
$i = $low;
$j = $high;
while($i < $j) {
// Increment the index value of "i"
// till the left most values should be less than or equal to the pivot value
while((int) $arr[$i] <= $pivot && $i < $high) {
$i++;
}
// Decrement the index value of "j"
// till the right most values should be greater than to the pivot value
while((int) $arr[$j] > $pivot && $j > $low) {
$j--;
}
// Interchange the values of present
// in the index i and j of an array only if i is less than j
if($i < $j) {
$temp = (int) $arr[$i];
(int) $arr[$i] = (int) $arr[$j];
(int) $arr[$j] = $temp;
}
}
// Interchange the element in j's poisition to the lower bound of an array
// and place the Pivot element to the j's position
$arr[$low] = (int) $arr[$j];
$arr[$j] = $pivot;
// Finally return the appropriate index position of the pivot element
return $j;
}
public function printArray(&$arr) {
// Loop the item and print each item in the console with white space seperated
for($i = 0; $i < sizeof($arr); $i++) {
echo($arr[$i] . " ");
}
echo("\n");
}
}
$obj = new QuickSortExample();
$arr = array();
array_push($arr, 15, 3, 12, 6, -9, 9, 0);
echo("Before Sorting: ");
$obj->printArray($arr);
$obj->quickSort($arr, 0, sizeof($arr) - 1);
echo("After Sorting: ");
$obj->printArray($arr);
المخرجات
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15