Bubble Sort Algorithm Tutorial in Java - How Fast Is It?

preview_player
Показать описание
Coding the Bubble Sort algorithm in Java!
This is a very beginner friendly beginner's Java coding lesson tutorial, where we'll write our own implementation of the Bubble Sort sorting algorithm.

Bubble Sort isn't the fastest sorting algorithm, but it's a great algorithm for beginners to learn.

Learn or improve your Java by watching it being coded live!

Hey, I'm John! I'm a Lead Java Software Engineer who has been in the industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java developers.

Let me know what else you'd like to see!

Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.

📕 THE best book to learn Java, Effective Java by Joshua Bloch

📕 One of my favorite programming books, Clean Code by Robert Martin

🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial

🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)

📹Phone I use for recording:

🎙️Microphone I use (classy, I know):

Donate with PayPal (Thank you so much!)

☕Complete Java course:

Рекомендации по теме
Комментарии
Автор

I love your video style! bite size videos that really tackle the nitty gritty of the topic, not just being vague / bridging gaps with your own knowledge. You make learning these topics a lot easer! thank u!

parker
Автор

I’ve been writing blogs to explain to my friends some programming topics myself so I totally understand the amount of work one have to put in to explain a topic so well and still keep it concise. Keep up the great job mate!

miigon
Автор

I can't belive you don't have more subs, ur videos are great

TTaiiLs
Автор

I hadn't seen this approach to the Bubble Sort. We did it as a loop inside of another loop.
This one seems cleaner.

FGj-xjrd
Автор

Almost perfect. This is one of the many sorting algorithms that is mandatory in German higher education computer science classes. We also evaluate the complexity by measuring the time and we talk about the mathematical representation of the complexity.
I said almost perfect because we try to optimize the algorithm, e.g. by reducing the length of the array that must be checked after each cycle. As you explained, the last and last but one element don't have to be checked. The question also arises whether using two for loops and a boolean is faster.

micleh
Автор

Love watching your videos to learn Java. Keep it up John. Best tutorial on YouTube. And hey can you tell us how to develop logic i mean after i see a video then only i understand the logic and i can code before that i don't have an idea.

moinvohra
Автор

Thank you for sharing with us this tutorial. Really helpful.

theblindprogrammer
Автор

going to pass my Algos class because of this channel! Love your channel my friend.

iskrega
Автор

Very nice. One of the best java teacher on the YouTube. Nice explanation. Thanks ❤

destroyer
Автор

Here's a little improvement to the Bubble Sort implementation:

For every loop, there will be one highest element put to the rightmost of the array. So instead of hardcoding the numbers.length, put it in some variable before for-loop (ex: index = numbers.length). At the end of the for-loop block, decrease the index by one (index--). Because, you don't need in anyway checking the rightmost elements that have been checked in the previous loop. Those elements are already sorted.

Here's the implementation:

public static String bubbleSort(int[] numbers) {

boolean swappedSomething = true;

while(swappedSomething ) {
swappedSomething = false;

int index = numbers.length-1;
for (int i = 0; i < index; i++) {
if(numbers[i] > numbers[i+1]) {
swappedSomething = true;
int temp= numbers[i];
numbers[i] = numbers[i+1];
numbers[i+1] = temp;
index--;

}
}
}

ihsannuruliman
Автор

Thank you John! Your work is much appreciated, you're an excellent teacher. Hats off!

michael_scarn_
Автор

Clearly amazing, wow, while loop was clearly making sense! Thank you for explaining this sorting algorithm

CanHaktan
Автор

Very carefully explained. Thank you very much for the video

nguyentranbao
Автор

You could also just use another int to declare the last switched index, so you can just leave the rest, sorted array untouched, this should safe you a lot of time with big arrays, because it just gets at least one iteration smaller each round.

linushoja
Автор

Very clear explanation i even enjoyed learning by watching it..

orusuvenkatesh
Автор

Ooh i was doing it the wrong way, but happily i can do it nicely thanks to you John, the way you did it, is pretty clean and understandable. I was doing it with the explicit 2 loops, the old way haha, still cannot improve the performance but at least the code is easy to understand

francksgenlecroyant
Автор

That's the best implementation of bubble sort I have ever seen

programwithviggo
Автор

You jst got urself a new sub, thanks a lot for nice content.

bwprogrammers
Автор

this is the best solution to understand, thank you very much!

alexevd
Автор

we need to address the elephant in the room -> Kramer painting :) Your channel is pure gold, keep up the good work!

BlekSteva