Updated March 17, 2023
Introduction to Quick Sort in JavaScript
A sorting algorithm is one of the important parts of the data structure. Sorting is the way of arranging the group of items in a specified way. Whenever we discuss faster sorting algorithms, the QuickSort comes into play. This is one of the most popular sorting techniques as per execution time is concerned. This is comparatively a better choice for any developer or coder due to its performance. The Quick Sort works on the divide and conquers rule. That means it divides the list into two and then two lists further divided into 4 recursively and so on. In this article, we will see how the quick sort works with example code as well. Also, we will see how it is faster as compared to other various sorting algorithms. We will see the various component of this Quick Sort Algorithm.
Operations in Quick Sort
There are three main operations in the quick sort JavaScript:
- Partitioning of a list: Division or the array list using the divide and conquer. This is the first step we can say in this sorting technique. For this, we need to a Pivot element (middle element or close to the middle element).
- Swapping Items: This is the main purpose of any sorting algorithm to come to the desire list as an output. This is a mechanism to sort replace the value from one to another. For example, A = 10; B = 20; If someone asks to swap then the value of A will be 20 and the B will be 10.
- Recursive Operation: This plays a great role in Quick Sort. Doing things, again and again, is not that possible and reliable without having the recursive function. This is something a function call itself (same function) to get the job done. This plays a great role where we perform any task again and again with the same approach and in the same context.
Comparison of Sorting Algorithm
There is various type of sorting algorithm. Since JavaScript is a programming language it supports all the sorting algorithms with it. Each and every sorting algorithm has its pros and cons. Here is the list of sorting algorithms and its performance and other matrices:
Sorting Algorithm | Time Complexity | ||
Best Case | Average Case | Worst Case | |
Bubble Sort | Ω(N) | Θ(N2) | O(N2) |
Selection Sort | Ω(N2) | Θ(N2) | O(N2) |
Insertion Sort | Ω(N) | Θ(N2) | O(N2) |
Merge Sort | Ω(N log N) | Θ(N log N) | O(N log N) |
Heap Sort | Ω(N log N) | Θ(N log N) | O(N log N) |
Quick Sort | Ω(N log N) | Θ(N log N) | O(N2) |
As we can see in the list the QUICK sort is faster than the Bubble Sort, Selection Sort, Insertion Sort comparatively.
How Quick Sort Works in JavaScript?
Step 1: To get the Pivot element – In any Divide and Conquer the selecting right Pivot plays a vital role. So, usually, we try to get the middle element of the array as a Pivot element. This is the element from where we divide the single array into the peace of two to process the sorting.
Step 2: Start left pointers as a first element of the input array.
Step 3: Start right pointers as a last element of the input array.
Step 4: Now, we compare elements at the left pointer with the selected pivot element and we swap the value if required as per the business requirements. Then we compare the right pointer with the Pivot element.
Step 5: Move both to their next. All of the above steps follow again and again using a recursive approach.
Example of Quick Sort in JavaScript
This is a function to take care of the Quick Sort in JavaScript. In this, we will pass the complete list of the array as input and will get the sorted array as output.
Code:
<html>
<head>
<title>Quick Sort in JavaScript</title>
</head>
<script type="text/javascript">
function quick_Sorting(array) {
if (array.length <= 1) {
return array; // if there is only one element then return the same
} else
{
var left = [];
var right = [];
var outputArray = [];
var pivot = array.pop();
var length = array.length;
for (var i = 0; i < length; i++) {
if (array[i] <= pivot) {
left.push(array[i]);
} else {
right.push(array[i]);
}
}
return outputArray.concat(quick_Sorting(left), pivot, quick_Sorting(right));
}
}
var myList = [3, 10, 2, 5, -5, 4, 7, 1];
alert("Input Array List: " + myList);
var sortedList = quick_Sorting(myList);
alert("Output Array List: " + sortedList);
</script>
<body>
</body>
</html>
Due to its stunning performance, most coder uses this sorting technique to implement the build-in sorting functionality. In various programming languages, quicksort has been used for its build-in sorting functionality. There are various other ways to write a program to perform the Quick Sort operations and all the functions meet to a point that is Divide and Conquer. So, this Divide and Conquer is a thump rule to process with the Quick Sort in JavaScript. Not just in JavaScript but also in all the programming languages as well.
Output:
Recommended Articles
This is a guide to Quick Sort in JavaScript. Here we discuss how quicksort works in javascript, its operations, and comparison of sorting algorithm along with the respective example. You may also look at the following articles to learn more –