Home  >  Article  >  Web Front-end  >  Bubble Sorting, Insertion Sorting & Selection Sort Algorithm Using Javascript

Bubble Sorting, Insertion Sorting & Selection Sort Algorithm Using Javascript

PHPz
PHPzOriginal
2024-08-18 00:00:07652browse

Bubble Sorting, Insertion Sorting & Selection Sort Algorithm Using Javascript

Bubble Sort and Insertion Sort are 2 basic sorting algorithms are there. I implemented these algorithms using JavaScript.

Bubble Sort

const arr = [5,4,3,2,1];

for (let i = 0; i < arr.length; i++) {
    for (j = 0 ; j< arr.length-i; j++) {
        if (arr[j] > arr[j+1]) {
            let temp = arr[j];
            arr[j] = arr[j+1];
            arr[j+1] = temp;
        }
    }
}

console.log(arr); // [1,2,3,4,5]

Insertion Sort

it is better than bubble sort + if you know the array is almost sorted it is best algorithm

const arr = [5,4,3,2,1];
for (let i = 0; i < arr.length; i++) {
    for (let j = i+1; j < arr.length; j++) {
        if (arr[i] > arr[j]) {
            const temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
    } 
}


console.log(arr); // [1,2,3,4,5]

Selection Sort

const arr = [5,4,3,2,1];
for (let i = 0; i< arr.length; i++) {
    let min = Infinity;
    let pos = -1;
    for(let j = i; j < arr.length; j++) {
        if (min > arr[j]) {
            min = arr[j];
            pos = j;
        }
    }

    const temp = arr[i];
    arr[i] = arr[pos];
    arr[pos] = temp;
}


console.log(arr); // [1,2,3,4,5]

The above is the detailed content of Bubble Sorting, Insertion Sorting & Selection Sort Algorithm Using Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn