Logo

dev-resources.site

for different kinds of informations.

How do we sort an Array in Javascript without Sort function?

Published at
11/19/2024
Categories
javascript
arrays
angular
react
Author
imranshaik012
Categories
4 categories in total
javascript
open
arrays
open
angular
open
react
open
Author
13 person written this
imranshaik012
open
How do we sort an Array in Javascript without Sort function?

Sorting an array without using the default JavaScript sort function.
There are multiple ways to sort an array in JavaScript. one of the most popular is Bubble Sort

Problem - you have an array of integers, sort the array
Sorting can either be Ascending or descending.

const array = [5,3,8,6,2]
Enter fullscreen mode Exit fullscreen mode

To sort an array Without using the JavaScript sort function is bubble sort.

Bubble Sort
Bubble sort is one of the simplest sorting algorithm. It repeatedly step through the list of array and compares the adjacent elements, and swap them if they are in the wrong order otherwise No swap. This process continues until the list is in sorted order.

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

let array = [5,3,8,6,2]
console.log("sorted Array ", bubbleSort(array));
Enter fullscreen mode Exit fullscreen mode

Here's how Bubble Sort works: An illustrated explanation is provided below.

Pass 1:
Compare 5 and 3 โ†’ Swap โ†’ [3, 5, 8, 6, 2]
Compare 5 and 8 โ†’ No swap โ†’ [3, 5, 8, 6, 2]
Compare 8 and 6 โ†’ Swap โ†’ [3, 5, 6, 8, 2]
Compare 8 and 2 โ†’ Swap โ†’ [3, 5, 6, 2, 8]
Result after Pass 1: Largest element 8 is in its correct position.

Pass 2:
Compare 5 and 3 โ†’ No Swap โ†’ [3, 5, 6, 2, 8]
Compare 5 and 6 โ†’ No swap โ†’ [3, 5, 6, 2, 8]
Compare 6 and 2 โ†’ Swap โ†’ [3, 5, 2, 6, 8]
Compare 8 and 6 โ†’ No Swap โ†’ [3, 5, 2, 6, 8]
Result after Pass 2: Largest element 6, 8 is in its correct position.

In this way it will continue to swap the element until we get the sorted array i.e: [2, 3, 5, 6, 8]

arrays Article's
30 articles in total
Favicon
lodash._merge vs Defu
Favicon
Leetcode โ€” 2942. Find Words Containing Character
Favicon
Leetcode โ€” 3289. The Two Sneaky Numbers of Digitville
Favicon
Introduction to Arrays
Favicon
Growing Pains: How Dynamic Arrays Handle New Elements ?
Favicon
Array and Object Access
Favicon
How do we sort an Array in Javascript without Sort function?
Favicon
Leetcode โ€” Top Interview 150โ€“169. Majority Element
Favicon
arrayToDict function in tRPC source code
Favicon
mismatch() and compare() in Java
Favicon
Create a unique array using Set() in JavaScript.
Favicon
Shifting Non-Zero Values Right : A Common Array Interview Problem-2
Favicon
Shifting Non-Zero Values Left: A Common Array Interview Problem-1
Favicon
Understanding Array Basics in Java: A Simple Guide
Favicon
Data Structures: Arrays
Favicon
Just uploaded a new video on arrays in Java Dive in and check it out now!
Favicon
Pointers and Arrays
Favicon
Arrays de strings
Favicon
Tente Isto 5-1: Classifique um array
Favicon
Arrays multidimensionais
Favicon
Arrays
Favicon
How to sort correctly arrays in JavaScript?
Favicon
Arrays
Favicon
Simple way to obtain largest Number in an array or slice in golang
Favicon
โญ๏ธExploring the Dynamic World of JavaScript๐Ÿš€
Favicon
How to Master Joining and Splitting Numpy Arrays: A Comprehensive Guide
Favicon
Understanding NumPy: Datatypes, Memory Storage, and Structured Arrays.
Favicon
PHP - arrays e valores como chave
Favicon
Array Methods: TypeScript vs C# ๐Ÿงฎ
Favicon
NumPy Unleashed: Exploring the Power of Special Arrays

Featured ones: