Bubble sort is a very simple sorting algorithm. The algorithm goes through every element and compares it to the next element; if the element is bigger than the next, then swap. This is done until the list is sorted. It's often the first sort that's introduced because of its simplicity. However, simple as it may be, it's a very inefficient sort with a time complexity of O(n2 ), which means that the algorithm goes through each of the n elements of the list n times. In contrast, quicksort, considered one of the best sort algorithms, has an average time complexity of O(n log n), which means it goes through each of the n elements log n times.
Many JavaScript engines use Timsort as the underlying algorithm for Array.prototype.sort. It is a stable sorting algorithm derived from merge sort and insertion sort, first used in Python. Since 2019, Array.prototype.sort has been specified as stable, meaning values that are equivalent stay in the same order relative to themselves. Before then, it was not guaranteed to be stable.
The argumenent to Array.prototype.sort is a comparator function. By default, sort treats the array elements as strings and will sort in UTF-16 code unit order. To sort an array of numbers properly, a comparator function that compares arguments as numbers must be given. Given arguments a and b to compare, the comparator function must return a negative number if a should sort before b, a positive number if a should sort after b, and 0 if they're equivalent.
Sauce: {Karakai Jouzu no Takagi-san 3} Base Subtitles: NovaWorks Subtitle file and code snippets: sort-using-js
Sure. It's not something that comes up with simple values like numbers because you can't distinguish between a 3 and another 3; they're identical. But it's important when you have more complex data. Take this array of arrays as an example:
Suppose we want to sort the arrays in ascending order based on the first item of each array, disregarding the second item. A stable sort will preserve the order of equivalent items relative to each other, like this:
Notice how all of the 3s retain their "first", "second", "third" order, which is the same order that they were in relative to each other in the original array. Same thing for the 7s and the 9s.
An unstable sort does not have this guarantee. A non-stable sort may produce something like this instead:
The items are still ordered according to the numbers, but notice how the items with the same numbers aren't necessarily in the same order as in the original array.
Stable sorts allow sorts to be stacked, so you can sort by one key, then by another (e.g. by last name, then first name). You can't do that with an unstable sort because once it goes through an unstable sort, the previous order is gone.
I do that with a lot of my memes, actually! If there's a teachable moment, I take advantage of it and provide an explanation so that you can hopefully learn something from it while enjoying the meme.
That's a fun one. My personal favourite is spaghetti sort, but it's not something you can really implement. My favourite comparison-based sort has to be heapsort: it just looks like magic.
Heapsort uses the heap as its primary data structure. A heap is a special type of tree, most often a binary tree, that satisfies two properties:
Every level of the tree is filled out sequentially from left to right. This means that every level of the tree is filled except possibly the last, which has its nodes on the left.
The value of every parent node is greater than or equal to the values of all of its child nodes; this is a max heap. Alternatively, if the values of the parent nodes are less than or equal to that of all of its children, it's a min heap.
The first property makes it really easy to flatten the tree into an array: because the tree is almost completely filled, you can map node positions to array indices and vice versa. The second means that the root of the tree is always the largest value in a max heap or the smallest in a min heap.
Two operations that you can do to a heap that are relevant for heapsort are push and pop. The details of these two operations are out of scope for this explanation, but both operations will preserve the two properties.
The algorithm is split up into two stages. The first stage is building the heap. The algorithm iterates through the array from left to right and builds a heap on the left, pushing each element it encounters into the heap. The second is destroying the heap. Assuming you want to sort in ascending order and you built a max heap, all you have to do is repeatedly pop off the root of the heap and place it at the end of the array from right to left. When the heap is empty, all of the nodes will have been popped off in descending order and you're left a sorted array.
That is hecking cool (or hacking cool if you’re into that), today I discovered both the existence of this subreddit and these algorithms because of you, thank you kind sir/maam/heli
Wow. Says the guy that puts "iirc" on a post making it sound as if it's obvious af. It doesn't take 10 minutes of learning JS to know that's not how JS sort works.
This was literally a 2 minute video and an accompanying explanation about the JS sort function and you had the audacity of commenting the absolute wrong way to use it like you somehow knew better. You didn't even bother.
You do realize “iirc” stands for “if I remember correctly”. How on earth does that imply that it’s obvious? You’re making yourself look like a clown
If you took two seconds to read the replies I even admitted that I was wrong. Apparently you have trouble reading though? Or maybe because it’s not scribble marks?
I’m sorry I can’t remember how every built in works. I’m too busy actually succeeding in my career instead of trying to get internet Ws on a two week old post. I’m assuming you’re shit at your job and so you feel the need to attack someone just to get hard with an ego as inflated as yours.
Tell me, what’s it feel like to have your parents be disappointed in you being a bottom barrel junior engineer instead of a doctor or surgeon?
You started insulting me over my 'shitty job', assuming you're way more successful than me, or that I'm some junior engineer.
Just because I called you a noob. Wow.
This is what happens when you don't get to hear criticism often. Getting triggered on the slightest of things. Never used to hearing that you're wrong. What a narcissistic asshole.
Back in January I was in the interview process. Normally I use Java. In fact, always until one interview.
The question came and I stammered, "oh, I'm going to switch CoderPad to use JavaScript". The question was trivial in JavaScript. Incredibly trivial. And a few lines.
It was suppose to be a forty-five minute coding interview. It was five. Including introduction. I felt like Ichigo catching Aizen's zanpukto.
No ok about the anime, where can I watch the first and the third season? Seriously on Netflix there's only the second season, Im pretty sure that on crunchyroll there's the first and the second, but the third?
Ok that's kinda weird, why would they post the first season only on crunchyroll, the second basically everywhere, the third on hidive and the movie only in theaters? What's the point of doing that?
And accidentally typo one of the numbers, or worse, embarrassingly misplace one of them? No thank you! That's what computers are for.
There's a small chance that I put it through sort, but i think actually did sort it by hand for the sorted versions, then proceeded to triple-check it to make sure I did it correctly.
300
u/bucket3432 Mar 22 '22 edited Mar 22 '22
Bubble sort is a very simple sorting algorithm. The algorithm goes through every element and compares it to the next element; if the element is bigger than the next, then swap. This is done until the list is sorted. It's often the first sort that's introduced because of its simplicity. However, simple as it may be, it's a very inefficient sort with a time complexity of O(n2 ), which means that the algorithm goes through each of the
nelements of the listntimes. In contrast, quicksort, considered one of the best sort algorithms, has an average time complexity of O(n log n), which means it goes through each of thenelementslog ntimes.Many JavaScript engines use Timsort as the underlying algorithm for
Array.prototype.sort. It is a stable sorting algorithm derived from merge sort and insertion sort, first used in Python. Since 2019,Array.prototype.sorthas been specified as stable, meaning values that are equivalent stay in the same order relative to themselves. Before then, it was not guaranteed to be stable.The argumenent to
Array.prototype.sortis a comparator function. By default,sorttreats the array elements as strings and will sort in UTF-16 code unit order. To sort an array of numbers properly, a comparator function that compares arguments as numbers must be given. Given argumentsaandbto compare, the comparator function must return a negative number ifashould sort beforeb, a positive number ifashould sort afterb, and 0 if they're equivalent.Sauce: {Karakai Jouzu no Takagi-san 3}
Base Subtitles: NovaWorks
Subtitle file and code snippets: sort-using-js
67
u/VPLGD Mar 22 '22
Q U A L I T Y post
And OP attached an explanation with the meme too! Awesome
3
u/The_Real_Slim_Lemon Mar 22 '22
Stalin sort has entered the chat
19
u/pixabit Mar 22 '22
You can make it even simpler by just doing
array.sort()Default is to sort ascending iirc
228
u/Nichiku Mar 22 '22
This is so well done and the plot twist is hilarious lol
91
u/TigreDemon Mar 22 '22
Lmao, I'm actually watching season 2 right now, I'm going to follow on the third one very soon after
42
u/NoTarget5646 Mar 22 '22
She is exactly how I'd be if I had to run code interviews lol.
8
u/planktonfun Mar 22 '22
am I old or this just way too common? now let it do .reduce
1
u/attack_turt Apr 11 '22
let or const there is no var
1
u/AlexCode10010 Nov 13 '22
No ok about the anime, where can I watch the first and the third season? Seriously on Netflix there's only the second season, Im pretty sure that on crunchyroll there's the first and the second, but the third?
6
u/CarlosG0619 Mar 22 '22
Im getting diabetes watching this anime
2
u/RedditMarcus_ Mar 03 '23
i would win because I use quantum bogosort
1
u/All54321_Gaming Mar 23 '22
What is the anime?
1
u/-Redstoneboi- Mar 23 '22
1:50 just got jigglypuff rested
1
u/SaikyouMegane Mar 23 '22
I’m here for Takagi san!
1
u/noobiesofteng Mar 23 '22
u/savevideo
1
u/Ill-Ad-9644 Mar 23 '22
Get rektd
1
u/GearDifferent3261 Apr 03 '22
u/savevideo
1
u/khrocksg Apr 07 '22
p much a non-programmer here: i half-expected the punchline to just be printing the already-sorted array
this is better
1
u/kaputass May 24 '22
Meanwhile me using Pigeonhole sort : I am 4 Parallel Universes ahead of you
1
u/Sporium Jun 16 '22
u/savevideo
1
u/Exciting-Insect8269 Jun 28 '22
I was expecting Stalin sort
1
u/[deleted] Jul 02 '22
Nearly flipped my table
-2
u/Johanno1 Mar 22 '22
Easy and probably even faster.
0
u/[deleted] Mar 22 '22
[deleted] — view deleted comment
0
u/[deleted] Apr 25 '22
[deleted] — view deleted comment
0
u/[deleted] Mar 23 '22
[deleted] — view deleted comment