Sorting algorithm | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Sorting algorithm

I have two objects, Artists and Songs. Artists={ A0: [ "a0", "a1", ...], A1: [ "a21", "a69", ...], ... //255 artists } Songs={ s0:{ ... album: "a0", ... }, s1:{ ... album: "a182", ... }, ... //26,500 songs } I need to find ten artists with most songs. I used this naive approach: let artists=Object.keys(Artists) .map(m=>[ m, Artists[m].map(n=> Object.keys(Songs) .filter(f=>Songs[f].album==n).length) .reduce((mm,n)=>mm+n) ]) .sort((m,n)=>n[1]-m[1]) .slice(0,10); But it takes 70 seconds to execute. Could you come up with a much faster approach? There are 805 albums. Artists object is here http://remi.rf.gd/JS/artists.js Songs object is here http://remi.rf.gd/JS/songs.js https://code.sololearn.com/WK4UfhwmJhrB/?ref=app

2nd Jun 2022, 12:34 AM
Fernando Moceces
Fernando Moceces - avatar
5 Answers
+ 1
ODLNT thank you very much for the insight. I have included the Artists and Songs objects on my post. Could you please try executing it again with your machine? Much appreciated.
3rd Jun 2022, 8:25 AM
Fernando Moceces
Fernando Moceces - avatar
0
Fernando Moceces based on your post I wrote the code below. I ran the algorithm provided, on my laptop got an average time of 3.9 seconds. I also ran it on bluestack virtual android phone and got an average time of 5.7second. I tracked the time using window.performance(), not for precision but for general knowledge. Take a look and let me know if the objects are formatted correctly. https://code.sololearn.com/WoM17HXrVrND/?ref=app
2nd Jun 2022, 5:23 PM
ODLNT
ODLNT - avatar
0
Fernando Moceces Great now were on the same page. When I run your algorithm on my laptop with the objects you provided I see an average time of 3.6 seconds. I was able to come up with something faster using same object and timer on my laptop I get an average time of 0.015 seconds. I included screenshots and algorithm below https://imgur.com/Km2uHq3 https://imgur.com/yjeRc8c https://code.sololearn.com/WI34X9d66qF0 I'm very interested what results you get on your device.
3rd Jun 2022, 12:14 PM
ODLNT
ODLNT - avatar
0
ODLNT excuse me but apparently you have your code set private so I can only see snippet of it. Also your google drive links seem to be inaccessible to public as well, they require access request.
3rd Jun 2022, 11:29 PM
Fernando Moceces
Fernando Moceces - avatar
0
My bad, I updated my previous post with public links to the screen shots and the code is now public
4th Jun 2022, 12:20 AM
ODLNT
ODLNT - avatar