DEV Community

Ajith R
Ajith R

Posted on

Quick sort that you dont want to know

Quick sort is a highly efficient, comparison-based sorting algorithm that uses a divide-and-conquer approach to sort an array. It works by selecting a "pivot" element from the array and partitioning the other elements into two subarrays according to whether they are less than or greater than the pivot. The subarrays are then recursively sorted.

Algorithm:

  1. Choose a Pivot: Select an element from the array to serve as the pivot. This can be done in various ways, such as selecting the first, last, middle, or random element.

  2. Partitioning: Reorder the array so that all elements less than the pivot are placed before it, and all elements greater than the pivot are placed after it. After this partitioning step, the pivot is in its final sorted position.

  3. Recursion: Recursively apply the above steps to the subarrays on the left and right of the pivot until the entire array is sorted.

JavaScript Implementation:

function quickSort(arr) {
    if (arr.length <= 1) {
        return arr;
    }

    const pivot = arr[Math.floor(arr.length / 2)];
    const left = [];
    const right = [];

    for (let i = 0; i < arr.length; i++) {
        if (i === Math.floor(arr.length / 2)) {
            continue; // Skip pivot element
        }
        if (arr[i] < pivot) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }

    return quickSort(left).concat(pivot, quickSort(right));
}
Enter fullscreen mode Exit fullscreen mode

Advantages of https://github.com/ajithr116/Data-Structures/tree/main/06-sorting/quicksort:

  1. Efficiency: Quick sort is one of the fastest sorting algorithms in practice. On average, it has a time complexity of O(n log n), making it suitable for sorting large datasets efficiently.

  2. In-Place Sorting: Quick sort can be implemented as an in-place sorting algorithm, meaning it requires only a small constant amount of additional memory beyond the input array. This makes it memory efficient and suitable for sorting large arrays.

  3. Adaptability: Quick sort's performance is not significantly affected by the initial order of elements in the array. It performs well even on partially sorted arrays and is not sensitive to input characteristics.

Disadvantages of quick sort:

  1. Worst-Case Time Complexity: In the worst-case scenario, quick sort may exhibit quadratic time complexity (O(n^2)) if the pivot selection results in highly unbalanced partitions. This typically occurs when the pivot is the smallest or largest element in the array.

  2. Unstable Sorting: Quick sort is not a stable sorting algorithm, meaning it may change the relative order of equal elements during the partitioning step. This property may be undesirable in certain applications.

  3. Recursive Nature: Quick sort relies on recursion, which may lead to stack overflow errors for very large arrays or deeply nested recursion. However, tail recursion optimization can mitigate this issue in some cases.

Conclusion:

https://github.com/ajithr116/Data-Structures/tree/main/06-sorting/quicksort is a highly efficient and widely used sorting algorithm known for its average-case performance and in-place sorting capability. While it may exhibit worst-case time complexity in rare scenarios, its adaptability and efficiency make it a popular choice for sorting large datasets in practice. By understanding its strengths and weaknesses, you can leverage quick sort effectively in various sorting tasks.


Top comments (0)