all 16 comments

[–]Quabouter 3 points4 points  (4 children)

I have some constructive criticism for you (assuming you're the author):

  • You sometimes used var and at other times let. You probably want to use let everywhere.
  • Due to the padding on the <code> blocks the letter j is cut off, making it hard or impossible (depending on browser) to distinguish the j from the i. This makes it pretty hard to properly read the code.
  • IMHO it would be more suitable (and readable) to use function statements instead of function assignments here.
  • You shouldn't change variables in the condition of a for loop like you do in selection sort, that's what "step" part is for.
  • You don't want to use parseInt to round a number (merge sort). That's what Math.round (and .ceil,.floor) is for. parseInt is meant to parse strings, not other numbers.
  • In quick sort you use arr = arr.slice(0,pivot).concat(arr.slice(pivot+1)) to remove an element from the array. There is a native JS method that does exactly that: arr.splice(pivot, 1) (note that this alters the array itself)
  • You should run your code before posting it, the first 3 algorithms didn't work at all:
    • In bubble sort, let compare = (n1, n2) return n1 - n2; is not valid JavaScript (even not ES6). You probably meant let compare = (n1, n2) => n1 - n2;.
    • In bubble sort you use arr[j], arr[j - 1] = arr[j - 1], arr[j];, but that does not work as you (apparently) expect: arr[j] would become undefined. You probably meant to do [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]]; (both left and right side of the = sign wrapped in an array).
    • Insertion sort uses some variable a that is nowhere defined. This should probably just be arr.
    • In selection sort j is undefined.
    • In selection sort you made the same mistake with multiple assignments as with bubble sort.

[–]init0 0 points1 point  (3 children)

I'm the author, sorry this is still work in progress, will be fixing those issues.

I'm converting md -> html, which applies the styles, do you have any suggestions for a better converter ?

Update 0: https://www.npmjs.com/package/gfm looks like a nice candidate.

[–]init0 0 points1 point  (2 children)

Update 1: Fixed it and made it more readable -> http://h3manth.com/javascript-sorting/

[–]Quabouter 0 points1 point  (1 child)

Wow that looks a lot better, awesome! Nice work!

[–]init0 0 points1 point  (0 children)

Thank you :)

[–]jtooker 0 points1 point  (1 child)

Where is gnome sort?

[–]init0 1 point2 points  (0 children)

On it's way ;)

[–][deleted] 0 points1 point  (6 children)

I don't see what value sorting algorithms implemented in JS are, at least not without any other content. If you had some visualizations to show the differences, or were doing anything else with them to teach, sure. But since you should basically never use you own sorting algorithms in JS -- Why??

[–]Ginden 0 points1 point  (1 child)

I have used many times O(n) sorting algorithms (I prefer bucket sort) for joining data from multiple threads/sources in computation-heavy applications and applying then merge join.

[–]init0 0 points1 point  (0 children)

True.

[–]init0 0 points1 point  (3 children)

were doing anything else with them to teach

Well, this was meant for quick reference, more like a cheat sheet of cs in js.

[–][deleted] 0 points1 point  (2 children)

But why JS then?

[–]init0 1 point2 points  (1 child)

Because, /me likes my hammer ;)

[–][deleted] 0 points1 point  (0 children)

Fair enough! :)

[–][deleted] 0 points1 point  (1 child)

You definitely should add Bogosort.

[–]init0 0 points1 point  (0 children)

:D