Hello! You are confused because you never studied Algorithm Analysis or Complexity Analysis. It is basically the study of algorithm performance... this course is the core of a Computer Scientist degree, for example!

1) How can there be two ways of writing the code if the essential logical idea of the sort is the same? => When analysing algorithms, you will discover it is common to do the same thing in a lot of different manners. The main idea can be the same, but the way it is executed is different. Both of the versions of this Bubble Sort are looping through the array items a lot of times (more than N times, where N is the number of items in the array). Both of the versions also have the same idea of bubble sort, which is: Comparing pair of elements and swap them in a way that on the end of each loop, the highest element will be in the end of the array. You can discover that by running the bubbleSortConcept2 on your browser, by running this: bubbleSortConcept2([5, 4, 3, 2, 1]). It will show you the resulting array after each loop, which is: An array that have the highest element on the end of it. This is the main idea of bubble sort and both algorithms are doing that!

2) Why do you callthem pointers? => Pointers doesn't exist only in C++ or C level. In fact, most part of the languages (or all of them) use pointers inside. Some very high-level languages (Java, Ruby, Python, R, ...) just hide that information to you, but it is using the pointers in the depths, since these high-level languages convert the code to a low-level language and they need to deal with pointers! C/C++ are considered high-level languages, but in a lower dimension than the ones I just mentioned, since C/C++ needs to make you deal pointers. I will try to be very succinct: Pointers are just variables that points to another variables. That's it. (In deep therms, it points to a memory address of another variable, but you can study more of that if you want). Now, let's go to the first sorting example... The i and the j variables are pointers, because they points to the position (index) of an element in the array. :) On the second sorting example, the index is the pointer, for the same reason.

3) Also I didn't understand what o(n2) meant => Yeah, that is a bit complex to explain to you, as it envolves the core of Algorithm Analysis, haha, but I will try. The N is the array length, as you noticed. The algorithm loop through the array length more than N times, the question is: Can you figure out how many comparisons (if's) this algorithm will have on the total? This will show the nature of the bubble sort. You can check the quantity of comparisons by adding this comparisonCount here...

As you can see, the quantity of Comparisons is growing much more faster than the N (length), this indicates that the order of magnitude of this algorithm is greater than N, now you need to find the order of magnitude in which the Comparisons is greater than the N (length).
You need to find the equation that gives you the quantity of Comparisons based on the N (length). On the bubble sort, this equation is:

c=n(n-1)/2

You can verify it applying to different examples, let's take the last one...

bubbleSortConcept1([20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1])N(length):20Comparisons:190// Replacing it on the equation you found...c=20(20-1)/2c=20(19)/2c=380/2c=190// 190 comparisons

Now, why it is O(n^2) after all of this explanation? n(n - 1)/2 is the same as: (n^2 - n)/2.
You need to study what is the O notation / Big O notation to enter on the details, but, in summary, you need to extract the function that grows faster on this equation: (n^2 - n)/2.

Hello! You are confused because you never studied Algorithm Analysis or Complexity Analysis. It is basically the study of algorithm performance... this course is the core of a Computer Scientist degree, for example!

1)

How can there be two ways of writing the code if the essential logical idea of the sort is the same?=> When analysing algorithms, you will discover it is common to do the same thing in a lot of different manners. The main idea can be the same, but the way it is executed is different. Both of the versions of this Bubble Sort are looping through the array items a lot of times (more than N times, where N is the number of items in the array). Both of the versions also have the same idea of bubble sort, which is: Comparing pair of elements and swap them in a way that on the end of each loop, the highest element will be in the end of the array. You can discover that by running thebubbleSortConcept2on your browser, by running this:bubbleSortConcept2([5, 4, 3, 2, 1]). It will show you the resulting array after each loop, which is: An array that have the highest element on the end of it. This is the main idea of bubble sort and both algorithms are doing that!2)

Why do you callthem pointers?=> Pointers doesn't exist only inC++orClevel. In fact, most part of the languages (or all of them) use pointers inside. Some veryhigh-levellanguages (Java,Ruby,Python,R, ...) just hide that information to you, but it is using the pointers in the depths, since thesehigh-levellanguages convert the code to alow-levellanguage and they need to deal with pointers!C/C++are considered high-level languages, but in a lower dimension than the ones I just mentioned, sinceC/C++needs to make you deal pointers. I will try to be very succinct:Pointers are just variables that points to another variables. That's it. (In deep therms, it points to amemory addressof another variable, but you can study more of that if you want). Now, let's go to the first sorting example... Theiand thejvariables are pointers, because they points to the position (index) of an element in the array. :) On the second sorting example, theindexis the pointer, for the same reason.3)

Also I didn't understand what o(n2) meant=> Yeah, that is a bit complex to explain to you, as it envolves the core of Algorithm Analysis, haha, but I will try. TheNis the array length, as you noticed. The algorithm loop through the array length more thanNtimes, the question is: Can you figure out how many comparisons (if's) this algorithm will have on the total? This will show the nature of the bubble sort. You can check the quantity of comparisons by adding this`comparisonCount`

here...Here are some results:

As you can see, the quantity of

Comparisonsis growing much more faster than theN (length), this indicates that the order of magnitude of this algorithm is greater thanN, now you need to find the order of magnitude in which theComparisonsis greater than theN (length).You need to find the equation that gives you the quantity of

Comparisonsbased on theN (length). On the bubble sort, this equation is:You can verify it applying to different examples, let's take the last one...

Now, why it is O(n^2) after all of this explanation?

`n(n - 1)/2`

is the same as:`(n^2 - n)/2`

.You need to study what is the

O notation/Big O notationto enter on the details, but, in summary, you need to extract the function thatgrows fasteron this equation:`(n^2 - n)/2`

.n^2grows faster thann? Yes! So it isO(n^2)Thank you, this was super helpful! Your explanation of how n

^{2}works was in particular useful.