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

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

Coding with John

3 года назад

68,178 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Prashanthi Vasu
Prashanthi Vasu - 21.09.2023 10:54

Pls tell me a good book for java for beginners

Ответить
Usagi ウサギ
Usagi ウサギ - 16.09.2023 21:18

may god bless you

Ответить
Can Haktan
Can Haktan - 31.08.2023 16:58

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

Ответить
Roberto D
Roberto D - 18.08.2023 05:11

Great explanation, great video!

Ответить
NadChel1
NadChel1 - 23.07.2023 08:30

I don't know how it happened, but I sorted a million-element int array with bubble sort in under 200 ms. It seems it's not that slow after all 🤔

Ответить
RoshanSourav
RoshanSourav - 04.07.2023 00:08

wooow

Ответить
Christopher C. Libre
Christopher C. Libre - 22.06.2023 08:42

omg, you explained this very good. I subscribed to you now

Ответить
Karl Schnurr
Karl Schnurr - 12.06.2023 21:22

Thanks, you’re great!

Ответить
Supun Perera
Supun Perera - 20.04.2023 14:25

John the man ! <3

Ответить
Abd Frehat
Abd Frehat - 30.03.2023 16:58

I think you should not print the elements of the array if you want to check the running time for the algorithm
most of the time is in writhing the elements to the screen

Ответить
keris_g.
keris_g. - 06.03.2023 18:40

what about from highest to lowest? badly need :(

Ответить
Program with Viggo
Program with Viggo - 07.02.2023 19:35

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

Ответить
lqwert1
lqwert1 - 07.02.2023 19:14

private static void printArray(int[] numbers){
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
} How to print the elements of the array in the same line with a semicolon seperator?
Thank you :)

Ответить
BlekSteva .1044
BlekSteva .1044 - 24.01.2023 12:37

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

Ответить
ZAINAB ALAYANDE
ZAINAB ALAYANDE - 05.01.2023 18:56

Fantastic! Fantastic!! Fantastic video John!!!

Ответить
Javlon Tursunov
Javlon Tursunov - 17.11.2022 16:33

I think bubble sort is efficient in terms of memory since it does require extra memory

Ответить
Javlon Tursunov
Javlon Tursunov - 17.11.2022 16:33

Man definitely something I have been looking for

Ответить
Sasu Kev
Sasu Kev - 07.11.2022 05:33

Does anyone know how bubble sort would work for a string data type??

Ответить
Brett Kolb
Brett Kolb - 17.10.2022 06:06

Honestly this made more sense than when my professor briefly explained how it worked then left it at that.

Ответить
Yaniv Cohen
Yaniv Cohen - 17.10.2022 00:52

Thank you

Ответить
F GJ
F GJ - 18.09.2022 22:46

Thanks :)

Ответить
Ian Flynn
Ian Flynn - 16.09.2022 06:35

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

Ответить
Prasad Jadhav
Prasad Jadhav - 25.08.2022 12:51

This visual representation is the best thing ever

Ответить
71 Zero290
71 Zero290 - 15.08.2022 10:13

Bro we need a lesson on selection sort,
And another on comparision between selection and insertion sort,
And lastly overall comparison between all other sorting algorithm (insertion, selection, bubble, quick, merge, heap) in terms of time, space complexity and when to use

Ответить
Navjot Singh
Navjot Singh - 12.08.2022 02:16

ty

Ответить
Dan R
Dan R - 11.07.2022 13:47

Is that Kramer's photo on the bottom right ?

Ответить
U2B音乐美景
U2B音乐美景 - 24.06.2022 17:19

Pretty cool,
I like it!
👍

Ответить
Al-Raffy Sarip
Al-Raffy Sarip - 21.06.2022 04:40

Do you have playlist for sorting algorithms?

Ответить
Joshua Modiba
Joshua Modiba - 17.06.2022 21:23

At first I didn't want to care abt this BIG O of N² mentions
But now... 😐 I'm thinking it's kind of important
I can do it 🙂Copium

Ответить
Marcelo Baldado
Marcelo Baldado - 14.06.2022 11:03

Hi genuine question
when you set the swappedSomething = false;
would that mean it will break through the while loop and never go to the for loop, Im fairly new to programming so im a lil bit confuse abt it

Ответить
Donkey Konga
Donkey Konga - 15.05.2022 18:54

i believe bubblesort is either bad explained or bad animated, actually compares first element to all, then second element to all from second element, then third element from all from third element and so on... not in pairs.

Ответить
lil thor
lil thor - 29.04.2022 23:27

If don't undertand something I don't give up, I just find another source of the same information. That's how I found your channel.
From a non native speaker that's sick of indians
Thank you!

Ответить
ahmad jammoul
ahmad jammoul - 28.04.2022 00:52

i love you man

Ответить
Rough Neck_Lorry
Rough Neck_Lorry - 21.04.2022 05:45

very vivid, thank you ,John

Ответить
Atai Abdiev
Atai Abdiev - 10.04.2022 11:40

I've question about this algorithm. When we do first loop the max element will be at the end of array. Can we decrement loop by 1 on every while loop. Shortly, I want to say that we can neglect the last element and in every while loop we can write loop like :
int length = list.length;
while (sorted!= true)
for(int i = 0;i < length-1;i++){
do something();
length -= 1;
}



please correct me if i've mistaken

Ответить
Richard Meyer
Richard Meyer - 22.03.2022 12:52

Just something to think about. Since every time you go through the for statement, the last number is in the correct place, you only have to go through it to "length -1" once and the next time is "length - 2" and you'd be doing one less comparison and loop each time, so wouldn't the complexity be less?

If you still have the source code, would you make the change and let me know if it makes a difference? Obviously with the way you have it set up, you'd have to generate a "permanent" array and copy it to sort it both ways.

Just a thought - and it's easy for me to hand out assignments to someone who's just trying to advance knowledge on the internet.

Thanks for the tutorial.

Ответить
Orusu Venkatesh
Orusu Venkatesh - 03.03.2022 11:37

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

Ответить
Nguyen Tran Bao
Nguyen Tran Bao - 01.03.2022 11:18

Very carefully explained. Thank you very much for the video

Ответить
Md Anis Ansari
Md Anis Ansari - 28.02.2022 20:11

Great explanation...

Ответить
Chethan S
Chethan S - 25.02.2022 11:14

Make a video to generate random numbers without using Randam class

Ответить
duvindu dinethmin
duvindu dinethmin - 18.02.2022 01:01

🖤

Ответить
Ihsan Nurul Iman
Ihsan Nurul Iman - 17.02.2022 14:30

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--;

}
}
}

Ответить
F. Gj
F. Gj - 08.02.2022 02:15

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

Ответить
germimonte
germimonte - 30.01.2022 04:35

is this a late april fools? you know that after each 'bubble' there will be one more ordered element? instead of a while just use 2 for loops, the first one moves the end point back and the second one moves the 'bubble'

Ответить
Asanka Siriwardena
Asanka Siriwardena - 23.01.2022 19:12

a beautiful piece of code ! 😍

Ответить
Random Things
Random Things - 23.01.2022 10:28

In java it takes time to even print on stdout...program will run faster without print

Ответить
Linus Hoja
Linus Hoja - 21.01.2022 09:29

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.

Ответить