- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement quick sort in JavaScript?
In this article, we are going to discuss how to implement quick sort in JavaScript with suitable examples.
Quick sort
The Quick sort is a divide and conquers algorithm similar to the merge sort. In this, we pick a pivot element and divide the array around the pivot element. There are many ways to pick the pivot element.
Always pick the first element as a pivot element.
Always pick the last element as a pivot element. (Implemented in the below program)
Pick a random element as pivot element
Pick the middle element as pivot element.
The main process in quick sort is partition. The aim of partition is, given an array and consider an element x in the array as pivot element. Keep the pivot element at the correct position in sorted array. Then put all the elements which are smaller than the pivot element in left side and the elements which are greater than the pivot element will be on the right side.
Input and Output Scenarios
Consider an array having some elements in it in a random order which are not sorted, we can sort the elements by performing merge sort. Let’s check the scenario below.
Input = [0, 10, 4, 1, 3]; Output = 0, 1, 3, 4, 10
Simple Partition and Quick Sort Function
Selecting an element x as pivot. The elements which are smaller than pivot element will be on left side of pivot element and the elements which are greater than pivot element will be on right side of pivot element.
How quick sort works?
To know how the quick sort is working, let’s assume an array arr=[7, 4, 10, 6, 3, 9]
Indexes are 0 1 2 3 4 5
Low = 0, High = 5 and pivot element = 9
Initially the index of smaller element, i = -1
By comparing the first element to the pivot element and 7 is less than 9. So, 7 is placed at index 0 and i moved to i=0.
Now, let’s compare 4 with pivot element and 4 is less than 9, So, i will be at i=1 and 4 will be placed there.
Compare 10 with pivot and 10 is greater than 9. i will not increment ( i = 1).
Let’s move to the next element 6, as 6 is smaller than pivot it will be in i=2. 9 will be in moved to i = 3.
Comparing 3 with pivot and 3 is less than 10. So i will increment and become i=3 and 10 and 3 will be swapped.
Now compare 10 with pivot. As 10 is greater pivot element it will be placed in i=4 and 10 will be in i=5.
Partition
Now, let’s perform partition around pivot element.
Pivot = 9.
Perform the partition at 9, elements less than 9 should be on left and greater 9 should be right.
Then consider the last element as pivot and continue the partition as shown in the figure below.
Now, let’s implement the above process of quick sort in the following program.
Example
In the following example, we are considering the last element as the pivot –
<!DOCTYPE html> <html> <head> <title>Implementation of Quick Sort</title> </head> <body> <script> function Quicksort(array){ if (array.length < 2){ return array; } let pivot_element = array[array.length - 1] let left_sub_array = []; let right_sub_array = []; for (let i = 0; i < array.length - 1; i++){ if (array[i] < pivot_element) { left_sub_array.push(array[i]) } else { right_sub_array.push(array[i]) } } return [...Quicksort(left_sub_array), pivot_element, ...Quicksort(right_sub_array)]; } const array = [0, 10, 4, 1, 3]; document.write(Quicksort(array)); </script> </body> </html>
- Related Articles
- C++ Program to Implement Quick Sort Using Randomization
- Merge sort vs quick sort in Javascript
- C++ Program to Implement Quick Sort with Given Complexity Constraint
- How to implement insertion sort in JavaScript?
- How to implement merge sort in JavaScript?
- Quick Sort
- Program to implement Bucket Sort in JavaScript
- Code to implement bubble sort - JavaScript
- Sorting an array of literals using quick sort in JavaScript
- Difference Between Quick Sort and Merge Sort
- C# program to perform Quick sort using Recursion
- Python Program for Iterative Quick Sort
- Java Program for Iterative Quick Sort
- Explain the quick sort technique in C language.
- Implement Bubble sort with negative and positive numbers – JavaScript?
