Thanks a lot for your brief, well and precise explained tutorial. 2 questions I still have: why you used 2 for-loops and in consequence 2 different variables for each " i, j "? why exactly did you write in the loop conditions a '-i' at 03:25 ? I'd be grateful if you could explain it a bit in details.
If you do not use the condition j < size - i - 1 in the inner loop and the outer loop does not run until i < size - 1, the program will not sort the array correctly. Here's what will happen in different scenarios: Inner Loop Without j < size - i - 1 If you use the condition j < size instead of j < size - i - 1 in the inner loop, you will encounter two primary issues: Out-of-Bounds Access: When j reaches size - 1, array[j + 1] will access an element beyond the end of the array, leading to undefined behavior. Inefficiency: The inner loop will make unnecessary comparisons and swaps even after the largest elements have bubbled to the end of the array. Outer Loop Without i < size - 1 If you use the condition i < size instead of i < size - 1 in the outer loop, the program will make an unnecessary extra pass through the array. This extra pass won't harm the correctness but will reduce efficiency slightly.
Bro code, I had learnt python,html/css, JavaScript and now currently learning this beautiful playlist of c language . Thanks man May God bless you with more knowledge
for (int i = 0; i < size - 1; i++) for (int j = 0; j < size - i - 1; j++) Example Walkthrough with Array [5, 3, 8, 4, 2] Initial State of the Array: [5, 3, 8, 4, 2] First Pass (i = 0): Outer loop starts with i = 0. Inner loop runs with j ranging from 0 to 3 (size - i - 1 = 5 - 0 - 1 = 4). Comparisons and swaps within this pass: j = 0: Compare 5 and 3, swap → [3, 5, 8, 4, 2] j = 1: Compare 5 and 8, no swap → [3, 5, 8, 4, 2] j = 2: Compare 8 and 4, swap → [3, 5, 4, 8, 2] j = 3: Compare 8 and 2, swap → [3, 5, 4, 2, 8] End of first pass: largest element 8 is at the end. Second Pass (i = 1): Outer loop continues with i = 1. Inner loop runs with j ranging from 0 to 2 (size - i - 1 = 5 - 1 - 1 = 3). Comparisons and swaps within this pass: j = 0: Compare 3 and 5, no swap → [3, 5, 4, 2, 8] j = 1: Compare 5 and 4, swap → [3, 4, 5, 2, 8] j = 2: Compare 5 and 2, swap → [3, 4, 2, 5, 8] End of second pass: second largest element 5 is in position. Third Pass (i = 2): Outer loop continues with i = 2. Inner loop runs with j ranging from 0 to 1 (size - i - 1 = 5 - 2 - 1 = 2). Comparisons and swaps within this pass: j = 0: Compare 3 and 4, no swap → [3, 4, 2, 5, 8] j = 1: Compare 4 and 2, swap → [3, 2, 4, 5, 8] End of third pass: third largest element 4 is in position. Fourth Pass (i = 3): Outer loop continues with i = 3. Inner loop runs with j ranging from 0 to 0 (size - i - 1 = 5 - 3 - 1 = 1). Comparisons and swaps within this pass: j = 0: Compare 3 and 2, swap → [2, 3, 4, 5, 8] End of fourth pass: fourth largest element 3 is in position. After these passes, the array is sorted: [2, 3, 4, 5, 8]. Summary i: Controls the number of passes. Each pass ensures the next largest unsorted element is moved to its correct position at the end. j: Controls the comparisons within each pass. Its range decreases with each pass as the largest elements are sorted to the end. Bubble Sort Mechanism: The largest unsorted element "bubbles up" to its correct position during each pass of the outer loop. Visualization Initial Array: [5, 3, 8, 4, 2] After Pass 1: [3, 5, 4, 2, 8] After Pass 2: [3, 4, 2, 5, 8] After Pass 3: [3, 2, 4, 5, 8] After Pass 4: [2, 3, 4, 5, 8] The sorted array is achieved after these passes, demonstrating how i and j control the sorting process.
Did an ascending order and the first few index has random values and my inputted values are sorted starting approximately from the middle of the array size.
@03:29, you added -i to j < size -1 .. the result came out the same with or without the -1 . What do you mean by optimize? WIll it affect other things with or without optimization?
optimize: put less load on the program by removing redundant(unneeded) processes. When he does j < size - i - 1, he is basically reducing the amount of elements that he needs to check, I'll show you what I mean by example. 1. At start of loop, when i = 0, j = 0, here's what the array looks like: {9, 1, 8, 2, 7, 3, 6, 4, 5} 2. After one iteration of j (i = 0, j = 1), the array will look like this: {1, 9, 8, 2, 7, 3, 6, 4, 5}, the 9 has essentially moved to the right of 1, because it satisfies the condition of the if statement within the 2nd for loop (i.e. 9 > 1) 3. After 2 iterations of j (i = 0, j = 2), it will look like this: {1, 8, 9, 2, 7, 3, 6, 4, 5}, i.e. 8 and 9 have been swapped for the same reasons mentioned in step 2. 4. Similarly after all iterations of j have been finished for the case where i = 0 (i = 0, j = 8), it will look like this {1, 8, 2, 7, 3, 6, 4, 5, 9}, basically extending the reasoning from step 2 to all cases of j in one go. 5. Now for the first iteration of i, steps 1-4 are repeated again, however j is capped at 7 (as opposed to 8, because the for loop has condition j < size - i - 1), this is what it would look like at i = 1, j = 7: {1, 2, 7, 3, 6, 4, 5, 8, 9}, Notice how the 9 is unchanged? This is because Bro coded the algorithm in such a way that the largest number of the array will always be the last element of the array (and hence the 2nd largest would be 2nd last, and 3rd largest would be 3rd last, etc...). This means we essentially don't need to check for the last number of the array as it is definitely larger than the element we are currently checking (at i = 1), and similarly we don't need to check for the last 2 numbers of the array as they are definitely larger than the element we are currently checking when i = 2. Bro simply expresses this such that the computer can understand by typing j < size - i - 1, where i is the last n elements we already put at the end of the list (that we don't need to check anymore). This may not be useful for such a small program, but suppose the array had 5000 elements, You would save so much operating time by not having to check all 5000 elements every iteration.
Basically bro the size is the same amount of loops the for will undergo so if u have 9 numbers by the 8th loop all the numbers will be sorted so to get rid of the last unnecessary loop size - 1 do the job
from what I understood, a computer counts from 0, so if we have array[ ] = {1, 2, 3}; the size is obviously 3, while the last element is actually array[2] = 3. Hope I made a bit of sense lol
what is the reason for size-1? is it because we only compare up to the last 2 element? if we go one more, the last element wouldn't have anything to compare itself to??
Hey! I was thinking the same thing. I think it is because you don't need to sort the last item. If all the other numbers are in their place, the finishing integer will also be in its place.
The elements 1-9 are actually counted (indexed) from 0. So 0-8 here (0 is the first and 1 is the second etc. so if n = 9 the last index is 9-1, 8. I think lol!
Hey I was wondering how you are able to modify an array inside a function and pass it to the next function without returning the modified array from the first, if someone could explain, that would be dope.
Hi, for what i learnt so far when you pass an array in a function you are indeed passing the adress of the 0th element of the array (the first one) so you're not creating a new array inside the function but you're referring to the same array you have in the main.
I did not understand why the for loop goes from 0 to size-1 with strict disequality, this means it goes from 0 (first element) to the n-2 element since if size = 5 elements of the array goes from index 0 to 4 so size-1=4 but with the "
@Benjamín Ramírez Puyol yea you're right, the reason of My confusion Is that I Always did the quicksort with a discendent for loop inside an ascendent for loop where i goes from 1 to n-1 and j from i to 0
Basically bro the size is the same amount of loops the for will undergo so if u have 9 numbers by the 8th loop all the numbers will be sorted so to get rid of the last unnecessary loop size - 1 do the job
Here's a bubble sort I made: #include #include #include #include #include void bubbleSort(int myArray[], int arraySize); void printArray(int myArray[], int arraySize); int main(){ int myArray[] = {1,3,5,11,12,800,2,6,4,8,9,10,17,7}; int arraySize = sizeof(myArray)/sizeof(myArray[0]); bubbleSort(myArray , arraySize); printArray(myArray , arraySize); return 0; } void bubbleSort(int myArray[], int arraySize){ int temporary; for(int i = 0; i < arraySize - 1; i++){ for(int j = 0; j < arraySize - 1 - i; j++){ if(myArray[j] > myArray[j+1]){ temporary = myArray[j]; myArray[j] = myArray[j+1]; myArray[j+1] = temporary; } } } } void printArray(int myArray[], int arraySize){ for(int i = 0; i < arraySize; i++){ printf("%d ",myArray[i]); } } // This was incredibly easy for me because I already watched brocode's c++ videos and this is basically the same thing.
Why do we need the nested loop though ( ik without it the loop would be incompleted ). I dont understand the logic behind the numbers of time why we need to redo the Swapping loops
well it doesnt work on me... I have writen all the logical stuff and it doesn't sort correctly. So I straight up copied your code to my text editor and still doesn't work. If you can help me id appreciate it a lot... (check the replies on the comment :) )
I had the same issue even though it was able to run my code a week before. Try this: 1. CTRL+SHIFT+X 2. Type in search box Code Runner 3. Install the extension 4. Then click on File - Preferences - Settings 5. Type code run in search box. 6. Scroll down until you find Code-runner: Run In Terminal. Check the box Wheather to run code in Integrated Terminal. 7. Restart VS Code I already had a code runner installed so I skipped to steps 6 and 7. Hope this helps!
Arrays count indexes by starting on 0. Imagine if an array had 3 elements. The first element would be array[0], the second element is array[1], and the third element is array[2], there is no such thing as array[3] because it is "out of bounds" of the array. Hence we have to do size - 1 so the program doesn't return an out of bounds error.