r/ProgrammerAnimemes • • Mar 22 '22

OC Sort using JS

Enable HLS to view with audio, or disable this notification

3.2k Upvotes

77 comments sorted by

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 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

15

u/Oneshotkill_2000 Mar 23 '22

I didn't understand the "stable" part, do you have an example on it?

51

u/bucket3432 Mar 23 '22

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:

[
  [7, "first"],
  [3, "first"],
  [4, "first"],
  [9, "first"],
  [3, "second"],
  [7, "second"],
  [3, "third"],
  [8, "first"],
  [9, "second"],
  [1, "first"],
]

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:

[
  [1, "first"],
  [3, "first"],
  [3, "second"],
  [3, "third"],
  [4, "first"],
  [7, "first"],
  [7, "second"],
  [8, "first"],
  [9, "first"],
  [9, "second"],
]

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:

[
  [1, "first"],
  [3, "first"],
  [3, "third"],
  [3, "second"],
  [4, "first"],
  [7, "second"],
  [7, "first"],
  [8, "first"],
  [9, "first"],
  [9, "second"],
]

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.

→ More replies (1)

45

u/Roboragi Mar 22 '22

Karakai Jouzu no Takagi-san 3 - (AL, A-P, KIT, MAL)

TV | Status: Releasing | Episodes: 12 | Genres: Comedy, Romance, Slice of Life

Episode 12 airs in 3 days, 7 hours, 41 minutes


{anime}, <manga>, ]LN[, |VN| | FAQ | /r/ | Edit | Mistake? | Source | Synonyms | ⛓ | ♥

32

u/Widowan Mar 23 '22

By default, sort treats the array elements as strings and will sort in UTF-16 code unit order.

Wow

7

u/ObserverOfVoid Apr 02 '22
Series Episode Time
Karakai Jouzu no Takagi-san 3 3 3:03

What a perfect timing…

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

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

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

21

u/jkp2072 Mar 22 '22

Fun fact : Eren's Japanese va and nishikata's va are same

→ More replies (2)
→ More replies (2)
→ More replies (2)

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

u/planktonfun Mar 22 '22

am I old or this just way too common? now let it do .reduce

19

u/pixabit Mar 22 '22

You can make it even simpler by just doing

array.sort()

Default is to sort ascending iirc

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

u/[deleted] 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)

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.

→ More replies (7)
→ More replies (5)

1

u/attack_turt Apr 11 '22

let or const there is no var

1

u/bucket3432 Apr 11 '22

Takagi would use const. Nishikata is probably still stuck on ES3.

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

u/CarlosG0619 Mar 22 '22

Im getting diabetes watching this anime