Welcome to the practice-based lesson dedicated to Simple Sorting Algorithms. Sorting is one of the most investigated classes of algorithms in computer science. Understanding different methods of sorting becomes more crucial as data size increases. In this lesson, we are going to revisit the basic sorting algorithms: Bubble
, Selection
, and Insertion
sorts. These are not only excellent exercises for those new to coding, but they also lay the groundwork for more complex sorting algorithms like QuickSort.
Before we dive into the basics, let's peek into more complex territory by examining QuickSort
, a popular divide-and-conquer sorting algorithm. The idea behind it is to pick a pivot
element from the array and partition the other elements into two arrays according to whether they are less than or greater than the pivot. The pivot is then placed in its correct sorted position between the two subarrays. This process is recursively applied to each of the two arrays around the pivot, leading to a sorted array.
Here's how you can implement QuickSort
in PHP:
php1<?php 2 3function quickSort(&$arr, $low, $high) { 4 if ($low < $high) { 5 $pi = partition($arr, $low, $high); 6 7 // Recursively sort elements before partition and after partition 8 quickSort($arr, $low, $pi - 1); 9 quickSort($arr, $pi + 1, $high); 10 } 11} 12 13function partition(&$arr, $low, $high) { 14 $pivot = $arr[$high]; 15 $i = $low - 1; 16 17 for ($j = $low; $j < $high; $j++) { 18 if ($arr[$j] <= $pivot) { 19 $i++; 20 // Swap $arr[$i] and $arr[$j] 21 $temp = $arr[$i]; 22 $arr[$i] = $arr[$j]; 23 $arr[$j] = $temp; 24 } 25 } 26 27 // Swap $arr[$i + 1] and $arr[$high] (or pivot) 28 $temp = $arr[$i + 1]; 29 $arr[$i + 1] = $arr[$high]; 30 $arr[$high] = $temp; 31 32 return $i + 1; 33} 34 35$arr = array(10, 7, 8, 9, 1, 5); 36$n = count($arr); 37 38quickSort($arr, 0, $n - 1); 39 40echo "Sorted array: \n"; 41for ($i = 0; $i < $n; $i++) { 42 echo $arr[$i] . " "; 43} 44// Output: 1 5 7 8 9 10
Here's a quick explanation of how the QuickSort
algorithm works:
- Recursive Function:
quickSort(&$arr, $low, $high)
sorts the array segment from$low
to$high
. The&
signifies that the array is passed by reference. - Base Case: The sorting continues as long as
$low < $high
, ensuring the segment is valid. - Partitioning: The
partition(&$arr, $low, $high)
function selects the last element aspivot
. It rearranges elements so that those less than or equal to thepivot
are on the left. Thepivot
is then placed in its sorted position, and its index is returned. - Recursion: The array is recursively sorted by applying
quickSort
to the left (quickSort($arr, $low, $pi - 1)
) and right (quickSort($arr, $pi + 1, $high)
) segments around thepivot
. - Result: The recursive division results in a fully sorted array.
QuickSort
uses this divide-and-conquer strategy to efficiently arrange data.
In addition to QuickSort
, we will reverse gears and return to the simple sorting algorithms — Bubble Sort
, Selection Sort
, and Insertion Sort
. These foundational algorithms will not only reinforce your understanding of the sorting principle but are often used as stepping stones to understanding more complex algorithms. Happy learning, and let's sort it out!