For the second for-loop for(int j = 0; j < array.length - i - 1; j++) Why must we subtract i and one? I understand why we need to subtract one but why i?
I know it's kind of late, but I'll reply anyway in case someone has the same question. We subtract i, so that way we won't check the numbers that are already sorted!
we subtract 1 so there wont be overflow from the array. You can avoid using -1 by doing the following steps: 1) start the j-loop from 1. 2) when you do comparison, you do arr[j-1] and arr[j] comparisons. Using this version of the loop might be more intuitive to understand.
In Bubble Sort, we compare pairs of numbers in the list and swap them if they are in the wrong order. After one complete pass through the list, the biggest number will be at the end, so we don't need to check it again in the next pass. Now, let's focus on this part of the loop: j < array.length - i - 1 What is j? j is the position in the array we are currently checking (the current pair of numbers being compared). What is array.length? array.length is the total number of elements in the array. For example, if the array has 6 numbers, array.length is 6. Why do we subtract 1? We subtract 1 because when we compare the element at j, we also compare it with the element at j + 1. If j were at the very end of the array, trying to check j + 1 would cause an error, because there’s no number after the last one. So, we stop one position early, by subtracting 1. Why subtract i? Every time we finish one pass through the array (one loop of the outer for loop), the largest number gets "bubbled up" to its correct position at the end of the array. In the second pass (i = 1), the second-largest number will be in its correct position. In the third pass (i = 2), the third-largest number will be in place, and so on. So, each time, we don’t need to check as many numbers as before because some of them are already sorted at the end. Subtracting i makes the loop shorter each time, so we don't waste time checking numbers that are already in the right place. Example with 6 Numbers: Imagine we have this array: {5, 2, 9, 1, 5, 6} (6 numbers). First pass (i = 0): We check and compare all numbers from position j = 0 to j = 4 (because array.length - i - 1 = 6 - 0 - 1 = 5). Second pass (i = 1): The largest number (9) is already in place, so we don’t need to compare it again. Now, we compare from j = 0 to j = 3 (because array.length - i - 1 = 6 - 1 - 1 = 4). Third pass (i = 2): The two largest numbers (9 and 6) are in place, so we only need to compare from j = 0 to j = 2. Each time, i gets bigger, and we check fewer numbers because the largest numbers are already sorted at the end!
These videos are insanely helpful at any level. The way that u help me visualize how the sort works at the start of the video is great. Thanks A bunch!!
I'm watching your videos from Brazil and I want to thank you for taking the time to teach us. It was very didactic to understand your explanation. Thank you very much.
I know it already but I appreciate your effort. I currently know bubble sort, selection sort, insertion sort, binary and linear search as per my school syllabus. I will be waiting for your quick sort tutorial, I seriously want to learn it.
Ey Brooooo ... i love you man ... not even my uni lecturer explained sorting this well .. saw this video 1 day prior to my exam and it helped meee soo much.. really appriciate BROOO ..Much Love from Sri Lanka ❤
I am happy to announce that I will be building a complementary video series (Algorithmic Complexity Analysis) that analyzes many of the defined concepts within your talks to demonstrate it using a rigorous mathematical framework that describes algorithms on a meta-level and forms the perfect combination, when watched in conjunction with your video series
After the first iteration the highest number will ended up in the highest position. so, in the next iteration we don't need to check again for that number (he's at the right position). After the second iteration the second highest number will ended up in the second highest position, so we don't need to check again for that number or the number in the highest position. And so on for the rest of the iterations.
Thank You Bro! YOU ARE GREAT MY FRIEND! (People who dislikes are not from Computer Science background! LOL)! Well, I'm comfortable in Python programming. So, I didn't watch your Java code. But, your explanation is so perfect that it was building an easy understanding about the algorithm. So nicely explained! Bro, Do you also solve leet code problems?
for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length - 1; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } This code worked for me. Why? My second for loop doesn't subtract i from array.length.
Great videos, you are very helpfull! One question, in second for loop, can we write "int j = i" instead of 0, because we do not want to compare elements that are already sorted?
thx man for the amazing video , I wanted to note that since the inner loop has 8 elements to be compared and the last element won't get compared because the second element then will already be sorted, so for that the outerloop should be array.length - 2 right ?
The n - i - 1 expression in the inner for loop of the bubbleSort method is used to reduce the number of iterations of the inner loop as the array gets partially sorted. Each pass of the outer loop places the largest element at the end of the array, so in the next pass, there's no need to compare that element again. This is why i is subtracted from n in the condition of the inner loop. This way, the inner loop only needs to iterate over the unsorted part of the array. By reducing the number of comparisons, the algorithm's efficiency is improved and the sorting process is faster.
thank you so much, it makes me easy, cause I watched alot of tutorials but don't understand. yesterday I bought a C# course on udemy, the first video is talking how to make a Bubble sort, but i don't understand. then my mind said: go on yotube, may be you can find a good teacher that makes simply to this fucking Bubble sort. then i got you. Now i understand the Bubble sort. thank you so much..
public class Main{
// bubble sort = pairs of adjacent elements are compared, and the elements
// swapped if they are not in order.
// Quadratic time O(n^2)
// small data set = okay-ish
// large data set = BAD (plz don't)
public static void main(String[] args) {
int array[] = {9, 1, 8, 2, 7, 3, 6, 4, 5};
bubbleSort(array);
for(int i : array) {
System.out.print(i);
}
}
public static void bubbleSort(int array[]) {
for(int i = 0; i < array.length - 1; i++) {
for(int j = 0; j < array.length - i - 1; j++) {
if(array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
}
Hello, can i get this code?
Practicing...
Ascending order
public class Main
{
public static void main(String[] args) {
int array[] = {7,3,2,1,4,0,8,6,5};
bubbleSort(array);
for(int i: array){
System.out.print(i);
}
}
public static void bubbleSort(int array[]){
for(int i = 0; i < array.length - 1; i++){
for(int j = 0; j < array.length - i -1; j++){
if(array[j] > array[j + 1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1]= temp;
}
}
}
}
}
*************************
Descending order
public class Main
{
public static void main(String[] args) {
int array[] = {7,3,2,1,4,0,8,6,5};
bubbleSort(array);
for(int i: array){
System.out.print(i);
}
}
public static void bubbleSort(int array[]){
for(int i = 0; i < array.length - 1; i++){
for(int j = 0; j < array.length - i -1; j++){
if(array[j] < array[j + 1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1]= temp;
}
}
}
}
}
@@andreamixvlog7478yeah you can get it
Thank u bro you made my day 🗿(Even though I am posting this comment at night)
You are one of the best computer teacher that anyone can get🗿
The comparison with the two cups was really intuitive! I wish someone had explained me variable swapping with that example.
You are saving my life in cs classes. Thank you so much.
sweet! I'm glad the videos are helping!
Defeat the algorithm, Bros.
For the second for-loop
for(int j = 0; j < array.length - i - 1; j++)
Why must we subtract i and one? I understand why we need to subtract one but why i?
I know it's kind of late, but I'll reply anyway in case someone has the same question. We subtract i, so that way we won't check the numbers that are already sorted!
we subtract 1 so there wont be overflow from the array. You can avoid using -1 by doing the following steps: 1) start the j-loop from 1. 2) when you do comparison, you do arr[j-1] and arr[j] comparisons. Using this version of the loop might be more intuitive to understand.
In Bubble Sort, we compare pairs of numbers in the list and swap them if they are in the wrong order. After one complete pass through the list, the biggest number will be at the end, so we don't need to check it again in the next pass.
Now, let's focus on this part of the loop:
j < array.length - i - 1
What is j?
j is the position in the array we are currently checking (the current pair of numbers being compared).
What is array.length?
array.length is the total number of elements in the array. For example, if the array has 6 numbers, array.length is 6.
Why do we subtract 1?
We subtract 1 because when we compare the element at j, we also compare it with the element at j + 1. If j were at the very end of the array, trying to check j + 1 would cause an error, because there’s no number after the last one. So, we stop one position early, by subtracting 1.
Why subtract i?
Every time we finish one pass through the array (one loop of the outer for loop), the largest number gets "bubbled up" to its correct position at the end of the array.
In the second pass (i = 1), the second-largest number will be in its correct position.
In the third pass (i = 2), the third-largest number will be in place, and so on.
So, each time, we don’t need to check as many numbers as before because some of them are already sorted at the end. Subtracting i makes the loop shorter each time, so we don't waste time checking numbers that are already in the right place.
Example with 6 Numbers:
Imagine we have this array: {5, 2, 9, 1, 5, 6} (6 numbers).
First pass (i = 0):
We check and compare all numbers from position j = 0 to j = 4 (because array.length - i - 1 = 6 - 0 - 1 = 5).
Second pass (i = 1):
The largest number (9) is already in place, so we don’t need to compare it again. Now, we compare from j = 0 to j = 3 (because array.length - i - 1 = 6 - 1 - 1 = 4).
Third pass (i = 2):
The two largest numbers (9 and 6) are in place, so we only need to compare from j = 0 to j = 2.
Each time, i gets bigger, and we check fewer numbers because the largest numbers are already sorted at the end!
@@codewithschoolteacher5118thanks a lot
Bro you are just amazing , i really appriciate your works .. Please keep going
thanks for watching!
These videos are insanely helpful at any level. The way that u help me visualize how the sort works at the start of the video is great. Thanks A bunch!!
legit only channel I have notifications on for, lifesaver!
thanks Poromoro! I'm glad these videos are helping
U r videos are my go to for all the sorting algos, Thank you for making these, Great help, Keep up the great work!
Out of all the basic ways of sorting, this is the slowest of the basic ways to sort
Ya but the easiest to execute
These videos are great. Really helpful for my studying, thank you
I'm watching your videos from Brazil and I want to thank you for taking the time to teach us. It was very didactic to understand your explanation. Thank you very much.
Bro is the best when it comes to explaining. I hope this guy blows up all over yt. I will support him
Bubble sort is a very easy algorithm to implement, but is quite inefficient with large data sets
I still come back to this video from time to time to rehash my BubbleSort.
Thanks BRO!
I know it already but I appreciate your effort. I currently know bubble sort, selection sort, insertion sort, binary and linear search as per my school syllabus. I will be waiting for your quick sort tutorial, I seriously want to learn it.
quicksort will be coming up soon I believe
you are great, just 15k subs away from the milestone! congrats
Yeah that's coming up soon!
the best channel on youtubeeee
Thanks bro!
always waiting for your video !!! keep doing this , love u
Thanks! I will continue
you are a truly gigachad... really golden video. keep going!
I'm from Colombia and I can understand all with this video, thx u (I love the manual example)
a lot better than uni teachers, thank you
Great Video mate!
Really helpful video. I could not understand the concept of bubble sort before but now i do and it has helped me code my programs better thanks bro
Thanks from Egypt❤🔥
Thanks again, bro :)
Thanks from Italy!
this helped me out thank you
Thanks for watching Qwikz!
💯 thank you!
Good explantation, Thanks for sharing
Really can't be better than this, thanks so much it was great tutorial❤❤
New bro here loved you video brooooo🤘🤘🤘
Ey Brooooo ... i love you man ... not even my uni lecturer explained sorting this well .. saw this video 1 day prior to my exam and it helped meee soo much.. really appriciate BROOO ..Much Love from Sri Lanka ❤
Everyone does not share information's easily and free, mostly want money, Thanks for sharing free INFORMATIONS BRO
no problem! Thanks for watching Saber
I love u!!!! Thank u for making the concept so clear n understandable💝
As always... excellent videos! Thanks!
Thank you. It was so helpful!!
Thank bro code you is my hero about progamming thanks bro
You're really saving my ass studying for that computer science exam - thanks bro!
you deserve more subscribers.
thanks Mohit!
thank you for the video, very easy to understand with your explanation
Thank you for your video. I think you need to break the loop if the array is already sorted for efficiency.
another simple implementation of bubble-sort algorithm
public static void bubble(int[] arr) {
int temp;
for(int i=0; i
Very helpful video, thank you!
Thank you bro!
I am happy to announce that I will be building a complementary video series (Algorithmic Complexity Analysis) that analyzes many of the defined concepts within your talks to demonstrate it using a rigorous mathematical framework that describes algorithms on a meta-level and forms the perfect combination, when watched in conjunction with your video series
holy moly, way better than my professor lol :))
Thank you very much bro, you put so much time and effort into your videos!
very useful, thanks!
Great explanation, thank you so much.
very well done!
Thank you for the algorithm!
Thanks for commenting to help with the UA-cam algorithm!
@@BroCodez Bro, can you do quick sort , please?
Explanation of j< array.length-i-1 ? please...
After the first iteration the highest number will ended up in the highest position. so, in the next iteration we don't need to check again for that number (he's at the right position).
After the second iteration the second highest number will ended up in the second highest position, so we don't need to check again for that number or the number in the highest position.
And so on for the rest of the iterations.
I will savour this moment that does not involve recursion
can you do this for every sorting algorithm? thanks in advance
I'll try
Many thanks!
Bro thanks just what i was looking
awesome! Thanks for watching Yehan!
You've got it sorted out.😎
Thanks Mate!!
Clear and Concise. Thanks.
Thanks for watching John!
Thank You Bro! YOU ARE GREAT MY FRIEND! (People who dislikes are not from Computer Science background! LOL)! Well, I'm comfortable in Python programming. So, I didn't watch your Java code. But, your explanation is so perfect that it was building an easy understanding about the algorithm. So nicely explained! Bro, Do you also solve leet code problems?
Thank You
Thanks for watching Vehan!
@@BroCodez 😊❤
Thanks!
Love you bro❤
Like always...legendary!
Thanks Nikitos!
it was perfect.
all of it
thanks!
Amazing, thanks alot
all I keep hearing is bulbasaur 😆Thanks for the video!!
Bro is the goat 🐐
thank you!
Great job..
Please Start complete video series on node KS as back-end technology..
maybe! I'll let you guys vote on future topics when I release polls
Great video! Please explain QuickSort , Heap and Binary Trees!!
Thanks Abdul! We'll get to those topics eventually!
thank you!!!
thanks for clarify this
thanks man
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
This code worked for me. Why? My second for loop doesn't subtract i from array.length.
i dont understand the "J" loop, why do you substract "i"? could someone explain pls
Great videos, you are very helpfull! One question, in second for loop, can we write "int j = i" instead of 0, because we do not want to compare elements that are already sorted?
he already does that
but its not the first i elements that are sorted
its the last i
Very easy to understand, but I wish u made those tutorial in C.
DROP a COMMENT for this awesome video.
why is it j
Me watching this as i have an exam tomorrow (im saved by this chad)
thx man for the amazing video , I wanted to note that since the inner loop has 8 elements to be compared and the last element won't get compared because the second element then will already be sorted, so for that the outerloop should be array.length - 2 right ?
thanks bro :D
Thanks very much bro code, i'm really like you
Thank you
thank you (:
Hello brother :)
Hi
Hey Duckers!
Ayy!! It's me
Moonlight!
@@BroCodez heheheh, hewo!
great man
can someone help me... why is it that we use " i < array.length - 1 " coz "
I love you bro thanks 😘💙❤️😘
THanks bro!
Thank you ;)
thanks for watching Yu!
Hey! bro or guys, why are u using a nested loop? still didnt get it
Great video
Thanks Michal!
Introduction for me😂😂😂😂
Ohhh yeah😾🎭🎭🎭🎭
Thanks for video
np! Thanks for watching EMbo!
why making the second loop j
i was woundering the same thing...
figure it out? I was wondering the same
The n - i - 1 expression in the inner for loop of the bubbleSort method is used to reduce the number of iterations of the inner loop as the array gets partially sorted.
Each pass of the outer loop places the largest element at the end of the array, so in the next pass, there's no need to compare that element again. This is why i is subtracted from n in the condition of the inner loop. This way, the inner loop only needs to iterate over the unsorted part of the array.
By reducing the number of comparisons, the algorithm's efficiency is improved and the sorting process is faster.
thank you so much, it makes me easy, cause I watched alot of tutorials but don't understand.
yesterday I bought a C# course on udemy, the first video is talking how to make a Bubble sort, but i don't understand.
then my mind said: go on yotube, may be you can find a good teacher that makes simply to this fucking Bubble sort.
then i got you.
Now i understand the Bubble sort.
thank you so much..