Hi!
Does anybody knows the right answer?
Thanks.
You really should learn it instead… Bubble sort and O(N^2)
Thanks!
I will study it, for sure!
?? The inside of Bubble sort is a swap, but it only examines adjacent elements. It also stops early if it notices the list is sorted (unlike almost every other sort). The outer loop is “while not sorted”. The sort in that image (arrg! do not post images of code!) is just junk. If it had a name it might be “Billy’s first sort”.
Where did you get that thing?
Hi Owen!
That was a question in an exam, I didn’t know the answer, so I took a picture to study later.
Sorry about the picture.
Why is it ^2 ?
Two Loops = ^2 ?
This has nothing to do with Unity. You say it was for a class – maybe you can read the chapter on Big-O notation again? You should also be able to find many explanations in an internet search. You can’t say “two loops is this big-O” since it depends on what the loops do.
You have two loops (one of it is nested) going through the same array. The number of iteration is N on the power of two. ^ sign means power.
Bubble sort was one of the reasons I left college at the end of the first year. The teacher wanted this bubble sort, I made a better one, he gave me an equivalent of an “F” (Eastern Europe, different grades), since it wasn’t the assignment. I said eff it, so went back to work in the software industry.
Could it stop early? If not, it’s not a Bubble sort. I mean, selection sort is the easiest sort to write and is clearly the “fastest”, but it can’t speed up on mostly-sorted lists, which is what most lists are. I’ve never assigned anyone to write a bubble sort since I show them one. There’s one in the book. I guess you could assign a backwards version – where after the 1st pass the smallest item is on A[0].
As far as two loops, you get students confused about nested vs. not. It takes awhile to explain how “{ foreach(n in N) …; } foreach(n in N)” is just a boring old O(n).