Bubble Sort in JavaScript17 Mar 2025 | 4 min read What is Bubble Sort?Sorting is the technique of shuffling the given array into increasing or decreasing order. There are various techniques or algorithms available to sort the array, like the bubble sort, insertion sort, merge sort, quick sort, radix sort etc. Bubble sort is the most popular sorting algorithm. We are given an array of integers, and we have to sort the given array using a bubble sort algorithm. AlgorithmIn this algorithm, we create the bubble of two adjacent elements, and we move this bubble from starting to the ending of the array. We will create the whole process maximum n times where n is the total number of elements in the array. In this algorithm, we will compare two adjacent elements, and if the smaller index value is greater than the larger index value, then we will swap these two values and move to the next two adjacent values. In one pass of the array, we will put one maximum element at its correct position. So maximum, we needed n pass or n iteration of this comparison. For example: ![]() In the above example : For i=0 For j=0 arr[j]<arr[j+1] ,so no swap and array = [5,6,3,1,2,4] For j=1 arr[j]>arr[j+1] so we will swap these values and array = [5,3,6,1,2,4] For j=2 arr[j]>arr[j+1] so we will swap these values and array = [5,3,1,6,2,4] For j=3 arr[j]>arr[j+1] so we will swap these values and array = [5,3,1,2,6,4] For j=4 arr[j]>arr[j+1] so we will swap these values and array = [5,3,1,2,4,6] After i=0 array is [5,3,1,2,4,6] Now for i=1 For j=0 arr[j]>arr[j+1] so we will swap these values and array = [3,5,1,2,4,6] For j=1 arr[j]>arr[j+1] so we will swap these values and array = [3,1,5,2,4,6] For j=2 arr[j]>arr[j+1] so we will swap these values and array = [3,1,2,5,4,6] For j=3 arr[j]>arr[j+1] so we will swap these values and array = [3,1,2,4,5,6] For j=4 arr[j]<arr[j+1] ,so no swap and array = [3,1,2,4,5,6] After i=1 array is = [3,1,2,4,5,6] For i=2 For j=0 arr[j]>arr[j+1] so we will swap these values and array = [1,3,2,4,5,6] For j=1 arr[j]>arr[j+1] so we will swap these values and array = [1,2,3,4,5,6] For j=2 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=3 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=4 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] After i=2 array is = [1,2,3,4,5,6] For i=3 For j=0 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=1 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=2 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=3 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=4 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] After i=3 array is = [1,2,3,4,5,6] For i=4 For j=0 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=1 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=2 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=3 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=4 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] After i=4 array is = [1,2,3,4,5,6] For i=5 For j=0 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=1 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=2 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=3 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] For j=4 arr[j]>arr[j+1] so no swap and array = [1,2,3,4,5,6] Javascript code:Output: ![]() Time complexity: O(N2), where N is the total number of elements in the array. Space complexity: O(1) Note: If the array is already sorted, we should check for further iterations. If the complexity increases, we can reduce it.For example, after i=2 array was sorted, we checked until i=5, which increased our complexity. So we will use flag variables to reduce the time complexity and reduce the number of iterations. Javascript code:Output: ![]() Time complexity: O(N2), where N is the total number of elements in the array. Space complexity: O(1) Note: If there are zero swaps for any i, then it means the array is sorted so that we will break the loop. |
The common non-linear data structure known as a tree. A tree illustrates a hierarchical structure in contrast to other data structures such an array, stack, queue, and linked list, which are linear in nature. A tree's ordering information is irrelevant. Two pointers and nodes make up...
4 min read
Problem Statement We are given a 0-indexed integer array nums. We can perform any number of operations, where each operation involves selecting a subarray of the array and replacing it with the sum of its elements. For example, if the given array is [1,3,5,6] and you select subarray...
5 min read
Binary trees, with their simple yet significant styles, have found various applications in the DSA field and are also rapidly growing. They offer a hierarchical representation of the data that supports searching, sorting, and other things. Determining the maximum sum level in a binary tree poses...
6 min read
Introduction A basic data structure in computer science, priority queues enable quick access to the element with the highest (or lowest) priority. Priority queues in C++ can be expanded to handle pairs, providing a flexible method of sorting according to the first or second element of the...
7 min read
Introduction: Graphs are a fundamental data structure used in computer science to model relationships between objects. One common problem associated with graphs is cycle detection, which involves determining whether there is a closed path (cycle) within the graph. Cycle detection is crucial in various applications: Network routing Deadlock detection Topological...
6 min read
? Introduction to Time Complexity: Efficiency is critical in the field of information technology and algorithm design. As developers, our goal is to build code that uses the least amount of resources possible while yet producing the desired results. Time complexity is one of the most important indicators...
9 min read
Counting non-leaf nodes in a binary tree is a big problem because it involves traversing the whole tree and visiting each one of the nodes personally. It involves that we have to figure out the number of nodes in a tree that contains at least one...
5 min read
Introduction: Sorting algorithms are essential components of computer science and data processing, facilitating the arrangement of data in a specific order. These algorithms find widespread applications in various fields, such as databases, information retrieval, and numerical analysis. One crucial application is in search algorithms, where sorted data...
4 min read
Each child node in a binary tree consists of just two nodes (left and right). Data are merely represented by tree topologies. Specialized forms of Binary Trees (BSTs) that adhere to these criteria include The left child node is smaller than its parent Right child's parent node is...
2 min read
Search in a row wise and column wise sorted matrix Introduction A basic computer science problem, searching for elements in matrices is essential to many applications, from image processing to databases. We can use more sophisticated search techniques to maximize the process when faced with a matrix that...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India