DEV Community

Cover image for Coding Problem Interview Series: How to Implement Bubble Sort?
Ben Halpern for CodeNewbie

Posted on

Coding Problem Interview Series: How to Implement Bubble Sort?

📣 Calling experienced devs and recent interviewees! Join the "Coding Problem Interview Series" to help code newbies tackle interview questions assessing problem-solving skills, algorithmic knowledge, and implementation of sorting, string manipulation, and data structure algorithms.

Share your expertise and insights! Pleas share multiple perspectives and tips for standout answers!

Today's question is:

Can you explain how the bubble sort algorithm is implemented?

Follow the CodeNewbie Org and #codenewbie for more discussions and online camaraderie!

Top comments (5)

Collapse
 
sarahokolo profile image
sahra 💫 • Edited

In the simplest way put: Bubble sort is a very basic sorting algorithm, that modifies and sorts out a given list, by swapping the position of each element in the list, and rearranging it to either ascending or descending order.

It compares each of the element in the list to the next element beside it and either swap them or leave them according to the given condition.

For example, let's say we have a list of numers 1-9, but they are all scrambled up like: [3,7,9,2,1,5,4,8,6]. We can use the bubble sort algorithm to arrange it in ascending or i.e 1-9 or descending 9-1.

So if we decide to sort in ascending order, the algorithm takes the list, and first compares (3,7), 3 is greater than 7, so the list stays the same, then it goes ahead to compare (7,9), still stays the same, now it goes to compare (9,2), so here 9 is greater than 2 so they swap places, moving 2 a step closer to the front.

This same process goes out till the end. Once it reaches the end of the list, if all the elements haven't already been arranged properly in their correct order, it goes ahead and repeat that same process all over again, and it does this over and over till eventually the list is fully sorted out in ascending order and now looks like this: [1,2,3,4,5,6,7,8,9].

So you can see how it got its name, because it bubbles each element in the list either up or down to form the perfect sorted list.

Hope you were able to get a basic understanding of it.

Collapse
 
villelmo profile image
William Torrez • Edited

How to translate this explanation to code?.

Can put this explanation in pseudo-code?

A lot of letter is complicated for understand, the best option is use data flow chart or pseudo-code for explain an algorithm.

Collapse
 
official_fire profile image
CoderZ90

simple sorting algorithm that repeatedly steps through the list to be sorted and it compares the adjacent element and then swaps them if they are in the wrong order
it runs until the complete list is sorted

for example

Initial array: [5, 2, 8, 1, 3]

Pass 1:
[2, 5, 8, 1, 3]  // Swap 5 and 2
[2, 5, 8, 1, 3]  // No swap
[2, 5, 1, 8, 3]  // Swap 8 and 1
[2, 5, 1, 3, 8]  // Swap 8 and 3

Pass 2:
[2, 5, 1, 3, 8]  // No swap
[2, 1, 5, 3, 8]  // Swap 5 and 1
[2, 1, 3, 5, 8]  // Swap 5 and 3

Pass 3:
[1, 2, 3, 5, 8]  // No swap

Sorted array: [1, 2, 3, 5, 8]
Enter fullscreen mode Exit fullscreen mode

in this you can see the checks on how it performs its task (a visualization of the algorithm in action, assuming we have an array of integers:)

I hope i am correct :)

Collapse
 
bbkr profile image
Paweł bbkr Pabian • Edited

I'm not sure what is the purpose of repeating algorithm description already available in multiple, accessible formats.

Also I do not like format "Calling experienced devs". Do you expect other people to create content for you under your posts? That is not the purpose of the comments section.

Collapse
 
villelmo profile image
William Torrez

I can't explain this algorithm, i don't know how implement this algorithm nor how work. Only can make a supposition because i don't have clarity.

Bubble sort in ascending order.