Tuesday, July 5, 2022
HomeData Structures and AlgorithmSimple Explanation on Sorting Algorithms with JavaScript | Part 1

# Simple Explanation on Sorting Algorithms with JavaScript | Part 1

Our next stop in the series of “Understanding Data Structure and Algorithms with JavaScript 🚀” is Sorting Algorithms. The sorting process is a very important fundamental concept in the computer science branch. Sorting Algorithms helps in arranging the given data either in ascending or descending order.

I am going to divide this blog into two parts so that we are able to get a better overview and understanding of different sorting algorithms. In this blog, we will discuss and understand the working and implementation of Selection, Bubble, and Insertion Sort and in another blog, we will discuss Merge, Heap, and Quick Sort.

## Why Sorting Algorithms are important?

Sorting Algorithms is an approach in arranging the list of data in order, such as alphabetical or numerical order, which eases our works in processing data into use.

• Sometimes an application needs to sort some data. For Example, A Cashier needs to check his current month highest earning from his total everyday monthly data, if he is able to sort the data in descending way, he will clearly able to find the highest data on top of the list.
• Another algorithms may use a sorting algorithm to sort the data and then apply search algorithm to find some data in ordered data.
• You can sort your files according to name in a computer, or you can able to arrange the list of customer according to their name, age, using sorting algorithms.
• Many application or process need sorted data inorder to reduce the time in processing and then providing the result such as when you search something on google, behind the scene google finds the right answer and then arrange them in best suited answer according tob your need.

## Selection Sort

The Selection Sort Algorithms sort the list of given data by finding first the minimum given element in the list and then placing that element in the first place and then again searches for the second minimum element in the list and then placing it next to the first element, And this loop goes on until the whole list is not sorted.

### Steps

• Find the minimum element in the array
• Place that minimum element at the first index and the lement placed at first index to that min element index
• Then again find the second minimum element in the array and placed it to the second index and the element at second index to that second min element index.
• Keep on doing this until the elemnt is sorted.

### Code Snippet

```function selectionSort(nums){
let minIndex,temp;
for(let i = 0; i < nums.length - 1; i++){
minIndex = i
for(let j = i+1; j < nums.length; j++){
if(nums[j] < nums[minIndex]){
temp = nums[minIndex]
nums[minIndex] = nums[j]
nums[j] = temp
}
}
}
}

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

selectionSort(arr)

console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 9]```

Best Time Complexity – O(n2)
Worst Time Complexity – O(n2)

## Bubble Sort

Bubble Sort Algorithms is one of the simplest sorting algorithms, continuously compares and swaps the two adjacent elements in the given list according to the order and then moves to the next elements till it gets sorted.

### Steps

• First, take two adjacent elements and then compare the order,
• If it satisfies the given order then moves two next two elements or swap the two elments
• In this way if it is in order of ascending, then largest number is placed at the lst index of array
• Then again loop starts comparing and swaping, till the whole array is not sorted.

### Code Snippet

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

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

bubbleSort(arr)

console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 9]```

Best Time Complexity – O(n)
Worst Time Complexity – O(n2)

Also read, Don’t Solve Problems, Eliminate Them

## Insertion Sort

Insertion Sort is also one of the simplest sorting algorithms, which works kind of similar to playing cards. Here, the array is split into two parts, sorted and unsorted part. Elements from unsorted are picked and placed at their correct position in the sorted part.

### Steps

• Iterate through from 1 index to the last n index of the array
• Compare the current element(key) to its previous element
• If the current element or key is smaller than its previous element then move the greater element to make space for swapped element.

### Code Snippet

```function insertSearch(nums){
for(let i = 1; i < nums.length; i++){
let temp = nums[i]
let j = i - 1
while(j >= 0 && nums[j] > temp){
nums[j+1] = nums[j]
j--
}
nums[j+1] = temp
}
}

let arr = [8,4,1,5,9,2]

insertSearch(arr)

console.log(arr)// [1, 2, 4, 5, 8, 9]```

Best Time Complexity – O(n)
Worst Time Complexity – O(n2)

## Final Words

I hope you like my article, and If you found this article helpful please share it more with your friends, family, and colleagues, Also do hit me up with few words so that I can improve and provide a more revised article.

And please bookmark our website, so that you can get new articles of the blog series “Understanding Data Structure and Algorithms with JavaScript 🚀” and to get more awesome articles related to business, software, and technology. Aaquib Ahmedhttps://aaquib.netlify.app
Hi, I'm Aaquib Ahmed, a passionate self-taught frontEnd web developer based in Bangalore, India. I tend to make use of modern web technologies to build websites that look great, feels fantastic, and functions correctly. I am especially focusing on Reactjs.
RELATED ARTICLES