Quicksort idea

1. Select a Flag node as a standard and define the left and right subtree groups. 2. Compare the elements of the array with flag. The elements smaller than the standard enter the left array, while those larger than the standard enter the right array. 3. Create a recursive condition for the left and right subarrays to recurse.

The algorithm of template

   function quickSort(arr) {
       // Stop conditions
        if (arr.length <= 1) return arr;

        // Determine the anchor element
        const index = parseInt(arr.length / 2);
        const flagValue = arr[index];

        const leftArr = [];
        const rightArr = [];

        // Get the left and right subsets
        for (let i = 0; i < arr.length; i++) {
            const item = arr[i];
            if (i === index) continue;
            if (item <= flagValue) leftArr.push(item);
            else rightArr.push(item);
        }

        // Query subsets recursively
        // return [...quickSort(leftArr), flagValue, ...quickSort(rightArr)];
        return quickSort(leftArr).concat(flagValue,quickSort(rightArr))
   }
Copy the code