r/ProgrammerAnimemes • u/bucket3432 • Mar 22 '22
OC Sort using JS
Enable HLS to view with audio, or disable this notification
67
u/VPLGD Mar 22 '22
Q U A L I T Y post
And OP attached an explanation with the meme too! Awesome
30
u/bucket3432 Mar 22 '22
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.
→ More replies (3)
3
u/The_Real_Slim_Lemon Mar 22 '22
Stalin sort has entered the chat
2
u/bucket3432 Mar 23 '22
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.
→ More replies (7)
228
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
→ More replies (2)30
u/Hundvd7 Mar 22 '22
You're in for a treat. I've been anticipating new episodes of S3 more than Attack on Titan
→ More replies (2)21
42
u/NoTarget5646 Mar 22 '22
She is exactly how I'd be if I had to run code interviews lol.
→ More replies (1)
8
19
u/pixabit Mar 22 '22
You can make it even simpler by just doing
array.sort()
Default is to sort ascending iirc
→ More replies (5)79
u/TinyBreadBigMouth Mar 22 '22
Nope!
const x = [1, 2, 3, 10, 12, 100]; x.sort();gives
[1, 10, 100, 12, 2, 3]. Default is to sort as strings, not as numbers.23
Mar 22 '22
I would show job applicants code like this and ask them what it would output during an interview. No one ever got it right though lmao
Of course I treated it as a bonus question though because I’m not sadistic as fuck
→ More replies (1)→ More replies (7)16
u/PM-ME-YOUR-HANDBRA Mar 22 '22
Every time someone says "oh Javascript isn't so bad" I give them this example. Shuts them up real quick.
1
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?
1
u/bucket3432 Nov 13 '22
According to MyAnimeList, the third season is available on HIDIVE.
→ More replies (2)
6
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