Menu
CodingPanel.com
  • Home
  • Technologies
    • Angular
    • C++
    • Java
    • JSON
    • JavaScript
    • Kotlin
    • PHP
    • React
  • Topics
    • Algorithms
    • Computer Science
    • Design Patterns
    • Software Design
    • Software Engineering
    • Web Development
  • Interview Questions
    • C# Interview Questions
  • Learn
    • My Learning
    • Data Structure
    • Programming
  • Jobs
    • Find a Job
    • Post a Job
    • Job Dashboard
CodingPanel.com

How to sort an array in JavaScript using Quick Sort Algorithm?

Posted on July 29, 2020February 10, 2021 by CodingPanel Editor

 

Quick Sort algorithm uses the divide and conquers approach. It allows you to divide the array into smaller sub-arrays and perform the sorting on these sub-arrays.

How Quick Sort Algorithm works?

The following steps explain how quicksort works.

First, select an element – as a pivot element

Compare all elements with the selected pivot element and arrange these in an order such that the elements less than the pivot element are placed on its left side and elements greater than the pivot are placed on its right side. You need to use a swap function to place elements at their place.

Next, perform the same operation on the left and right side elements of the pivot elements in a recursive fashion.

Example of Quick Sort Algorithm using JavaScript

Let’s say you have to sort an array containing integer values.

let arr = [7,3,4,9,1,2];

The following function swaps two numbers in JS.

function swap(arr, left, right) {
    var temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
}

To perform the partition on the array, use the following function.

function partition(arr, left, right) {

    let middle = arr[Math.floor((right + left) / 2)];
    let i = left;
    let j = right;

    while (i <= j) {
        while (arr[i] < middle) {
            i++;
        }
        while (arr[j] > middle) {
            j--;
        }
        if (i <= j) {
            swap(arr, i, j);
            i++;
            j--;
        }
    }
    return i;
}

The next step is to call partition() function recursively to divide and sort the arrays.

function quickSort(arr, left, right) {
    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;
}

To call the function, simply pass the array to quicksort() function as a parameter

let arr = [7, 3, 4, 9, 1, 2];
console.log("Array before sorting :" + arr);
var sorted_arr = quickSort(arr, 0, arr.length - 1);
console.log("Array after sorting :" + sorted_arr);

Output:

Related posts:

  1. Calculate the Median value of two arrays using C#
  2. How to convert an Array to String in PHP?
  3. What is an Algorithm in coding?
  4. How to parse JSON data with JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Blog Authors

avatar for Asha S.Asha S. (1)

IT specialist Veteran

avatar for CodingPanel EditorCodingPanel Editor (34)

Blog Hero

avatar for Feras SFeras S (4)

Software Engineer

Programming Courses

  • Learn C Programming
    by CodingPanel Editor
    32 Lessons
  • Learn C++ Programming
    by CodingPanel Editor
    12 Lessons
  • React Tutorial
    by CodingPanel Editor
    21 Lessons
©2021 CodingPanel.com | Powered by WordPress & Superb Themes