
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript QuickSort Recursive
In this article, we will learn to implement Quicksort recursively in JavaScript. Quicksort is one of the most efficient and widely used sorting algorithms, known for its divide-and-conquer approach.
What is Quicksort?
Quicksort is a divide-and-conquer sorting algorithm that sorts an array by selecting a pivot element and partitioning the array into two subarrays: elements smaller than the pivot and elements greater than or equal to the pivot. It then recursively sorts the subarrays.
Algorithm
Quicksort follows the below steps ?
-
Step 1 ? Make any element the pivot (preferably first or last, but any element can be the pivot).
-
Step 2 ? Partition the array based on pivot.
-
Step 3 ? Apply a quick sort on the left partition recursively.
- Step 4 ? Apply a quick sort on the right partition recursively.
Pseudocode for Quicksort ?
QUICKSORT(arr, low, high):if low < high: pivotIndex = PARTITION(arr, low, high) QUICKSORT(arr, low, pivotIndex - 1) // Left subarray QUICKSORT(arr, pivotIndex + 1, high) // Right subarray
Example
Below is an example of sorting an array using quicksort recursively ?
const arr = [5,3,7,6,2,9]; const swap = (arr, leftIndex, rightIndex) => { let temp = arr[leftIndex]; arr[leftIndex] = arr[rightIndex]; arr[rightIndex] = temp; }; const partition = (arr, left, right) => { let pivot = arr[Math.floor((right + left) / 2)]; let i = left; let j = right; while (i <= j) { while (arr[i] < pivot) { i++; }; while (arr[j] > pivot) { j--; }; if (i <= j) { swap(arr, i, j); //sawpping two elements i++; j--; }; }; return i; } const quickSort = (arr, left = 0, right = arr.length - 1) => { let index; if (arr.length > 1) { index = partition(arr, left, right); if (left < index - 1) { quickSort(arr, left, index - 1); }; if (index < right) { quickSort(arr, index, right); }; } return arr; } let sortedArray = quickSort(arr); console.log(sortedArray);
Output
[ 2, 3, 5, 6, 7, 9 ]
Complexity Analysis
- Best Case: O(nlog?n), occurs when the pivot divides the array into roughly equal halves at each step.
- Average Case: O(nlog?n), the pivot usually divides the array into reasonably balanced subarrays.
- Worst Case: O(n^2), happens when the pivot is the smallest or largest element, resulting in unbalanced subarrays.
- Auxiliary Space: O(n) in the worst case due to recursive calls and partitioning.
- For balanced partitions, the space complexity is O(log?n).
Conclusion
Quicksort is a powerful sorting algorithm with excellent performance in most cases. The recursive implementation in JavaScript is intuitive and easy to understand, making it a great starting point for learning the algorithm. However, for practical applications, consider optimizations like in-place partitioning and better pivot selection to improve performance and efficiency.