A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/sorting-algorithms-in-javascript/ below:

Sorting Algorithms in JavaScript - GeeksforGeeks

Sorting Algorithms in JavaScript

Last Updated : 12 Feb, 2025

Sorting is an important operation in computer science that arranges elements of an array or list in a certain order (either ascending or descending).

Example:

Input: [64, 34, 25, 12, 22, 11, 90]
Output: [11, 12, 22, 25, 34, 64, 90]

Input: [1, 2, -3, 3, 4, 5]
Output: [-3, 1, 2, 3, 4, 5]

Bubble Sort

Bubble Sort is one of the simplest sorting algorithms. It repeatedly compares adjacent elements and swaps them if they are in the wrong order.

Why is Bubble Sort Stable?

In Bubble Sort, adjacent elements are compared, and they swap only if the first element is greater than the second. If two elements are equal, they remain in the same order as they were in the original list because there is no unnecessary swapping of equal elements.

JavaScript
// JavaScript Program for Bubble Sort

function bubbleSort(arr, n) {
    let swapped = false;
    for(let i = 0;i < n; i++){
        swapped = false;
        for(let j = 0 ; j < n - i -1; j++){
            if( arr[j] > arr[j+1]){
                [arr[j], arr[j+1]] = [arr[j+1], arr[j]];
                swapped = true;
            }
        }
        
        if( swapped === false) break;
    }
    return arr;
}

let a = [2, 1, 3, 4];
a = bubbleSort(a, 4);
console.log(a);

Time Complexity: O(n2)
Auxiliary Space: O(1)

Selection Sort

Selection Sort is a simple comparison-based algorithm. It divides the array into two parts: sorted and unsorted. In each iteration, it selects the smallest (or largest) element from the unsorted part and moves it to the sorted part. It is in place algorithm.

Notes:

JavaScript
// JavaScript Program for selectionSort

function selectionSort(arr) {
    let n = arr.length;
    for (let i = 0; i < n - 1; i++) {
        let minIndex = i;
        for (let j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        // Swap arr[i] and arr[minIndex]
        let temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
    return arr;
}

let arr = [64, 25, 12, 22, 11];
console.log(selectionSort(arr));

Output
[ 11, 12, 22, 25, 64 ]

Time Complexity: O(n2)
Auxiliary Space: O(1)

Insertion Sort

Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time. It works by iterating through the array and inserting each element into its correct position in the already sorted portion of the array.

Why is Insertion Sort Stable?

Insertion Sort is considered a stable sorting algorithm. Stability in sorting means that if two elements have the same value, their relative order in the sorted array will be the same as their relative order in the original array

Characteristics of Insertion Sort:

JavaScript
// JavaScript Program for InsertionsSort
function insertionSort(arr) {
    let n = arr.length;
    for (let i = 1; i < n; i++) {
        let key = arr[i];
        let j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
    return arr;
}

// Example usage:
let arr = [12, 11, 13, 5, 6];
console.log(insertionSort(arr));

Output
[ 5, 6, 11, 12, 13 ]

Time Complexity: O(n2)
Auxiliary Space: O(1)

Merge Sort

Merge Sort is a divide-and-conquer algorithm. It divides the array into two halves, recursively sorts them, and merges the sorted halves.

JavaScript
// JavaScript Program for Merge Sort
function mergeSort(arr) {
    if (arr.length <= 1) {
        return arr;
    }
    const mid = Math.floor(arr.length / 2);
    const left = mergeSort(arr.slice(0, mid));
    const right = mergeSort(arr.slice(mid));
    return merge(left, right);
}

function merge(left, right) {
    let result = [];
    let i = 0;
    let j = 0;
    while (i < left.length && j < right.length) {
        if (left[i] < right[j]) {
            result.push(left[i]);
            i++;
        } else {
            result.push(right[j]);
            j++;
        }
    }
    return result.concat(left.slice(i), right.slice(j));
}
// Example usage
let arr = [38, 27, 43, 3, 9, 82, 10];
console.log(mergeSort(arr)); 

Output
[
   3,  9, 10, 27,
  38, 43, 82
]

Time Complexity: O(n log n)
Auxiliary Space: O(n)

Quick Sort Using Lomuto

Quick Sort is another divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot.

Lomuto Partition:

Hoare's Partition:

JavaScript
// JavaScript Program for Quick Sort
function quickSort(arr) {
    if (arr.length <= 1) {
        return arr;
    }
    const pivot = arr[arr.length - 1];
    const left = [];
    const right = [];
    for (let i = 0; i < arr.length - 1; i++) {
        if (arr[i] < pivot) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }
    return [...quickSort(left), pivot, ...quickSort(right)];
}

// Example usage:
let arr = [10, 7, 8, 9, 1, 5];
console.log(quickSort(arr));

Output
[ 1, 5, 7, 8, 9, 10 ]

Time complexity: O(n log n), but in worst case it takes O(n2) time.
Auxiliary Space: O(n)

Easy Problem On Sorting in JavaScript Medium Problem On Sorting in JavaScript Hard Problem On Sorting in JavaScript
Overview of Sorting Algorithms Stability in Sorting Algorithm

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4