What are some alternative sorting algorithms in PHP besides bubble sort, and how do they compare in terms of efficiency and complexity?

When sorting data in PHP, using alternative sorting algorithms like quicksort, mergesort, or insertion sort can often be more efficient than bubble sort. These algorithms have different time complexities and efficiency levels, with quicksort typically being the fastest on average.

// Using quicksort algorithm for sorting in PHP
function quicksort($array) {
    if (count($array) < 2) {
        return $array;
    }

    $pivot = $array[0];
    $left = $right = [];

    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i] < $pivot) {
            $left[] = $array[$i];
        } else {
            $right[] = $array[$i];
        }
    }

    return array_merge(quicksort($left), [$pivot], quicksort($right));
}

// Usage
$array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
$sortedArray = quicksort($array);
print_r($sortedArray);