Radix Sort is Fun… but it’s Useless in JavaScript

I just spent an hour writing a Radix sort algorithm. It turns out that it is 25% the speed of the built in JavaScript `.sort()` function.

Apparently all this sorting has been figured out in the past. Strange that it’s part of learning to code these days…

const performance = require('perf_hooks').performance;


function getDigit(num, location) {
  let stringNum = num.toString();
  let resultNum = parseInt(stringNum[stringNum.length - 1 - location]);
  if (resultNum) {
    return resultNum;
  } else {
    return 0;
  }
}

function getDigitCount(num) {
  let digitCount =  Math.abs(num).toString().length;
  return digitCount;
}

function mostDigits(arr) {
  let maxDigits = -Infinity;
  for (var i = 0; i < arr.length; i += 1) {
    var digitCount = getDigitCount(arr[i])
    if (maxDigits < digitCount) {
      maxDigits = digitCount;
    }
  }
  return maxDigits;
}

function radixSort(list) {
  let numberOfDigitsTheLargestNumberHas = mostDigits(list);

  for (let i = 0; i < numberOfDigitsTheLargestNumberHas; i += 1) {
    let bucket = [[],[],[],[],[],[],[],[],[]];

    for (var j = 0; j < list.length; j += 1) {
      let number = list[j];
      bucket[getDigit(number, i)].push(number);
    }
    list = bucket.flat(2);
  }
  return list.flat();
}

function sum(a, b) {
  return a - b;
}

var ourBeautifulArray = [50000, 1200,1133, 44, 22, 1, 400000000];
var time1 = performance.now();
console.log(radixSort(ourBeautifulArray));
var time2 = performance.now();
console.log(`RadixSort took ${time2 - time1} to execute`);

var time3 = performance.now();
console.log(ourBeautifulArray.sort(sum));
var time4 = performance.now();
console.log(`Normal JS sort took ${time4 - time3} to execute`);

It’s possible that there is room for improvement in the way I’m executing this algorithm. Perhaps using .flat is causing this.

Either way, even if it’s not a useful thing to be able to do, I think it’s interesting. Once you figure out how it works, it’s neat because it sorts things in non-obvious ways.

I just wish it performed better. LoL

If you want to see the code I’m working with to deepen my knowledge of JavaScript, check out this repository.

Leslie Boards Rose 2019

This was a learn your new camera day for me and I don’t like what I learned.

GoPro7 advertises that it has 240 frames per second which seemed awesome to me. Unfortunatly, none of my three video editing suites (Adobe Premiere CS6, Screenflow nor OpenShot Video Editor) could read these files. So yeah. Big loss on the GoPro7. I’m unsure that it has anything over the GoPro6. Indeed, the wasted time means I wish I still had had that GoPro6….

Alas, I’ll keep experimenting….

Thanks to Leslie for going riding with me.

Ruby vs. Python vs. JavaScript – What Programming Language To Learn First?

When I started programming, I struggled with deciding on a language. What programming language to learn first? How does one decide between Ruby, Python and/or JavaScript?

It’s an interesting question. I wish I could have seen the image below when I was trying to make sense of it:

FizzBuzz written in Python, Ruby and JavaScript

The three programs above all do the EXACT same thing. JavaScript on the left, Ruby in the middle and Python on the right.

All three print a count from one to fifteen. When the count is evenly divisible by 3 and 5, it prints ‘fizz_buzz’, when evenly divisible by 3 it prints ‘fizz’ and when evenly divisible by 5 it prints ‘buzz’. If none of those, it prints the number. You can see the count listed at the bottom of the image.

This is a classic computer programming exercise.

Aesthetics of Code

Whenever anyone shares their opinions on aesthetics, it’s important to remember that there are no right answers. Aesthetics are a matter of taste. So I’m just sharing my taste for code here.

The JavaScript file on the left requires considerably greater amount of keystrokes. It looks confusing. I see lots of opportunities to make mistakes.

Python is clearly the most concise. The Python code is three lines shorter than the other programs.

I like that Ruby doesn’t require semi-colons or colons. These symbols are annoying and require a close look. For me, it’s easy to mistake a semi-colon for a colon.

There is a question about how to write variables. One way is camel case (writingVariablesLikeThis), another option is to use underscore case (wirting_variables_like_this). Camel case is more concise, but I think it’s less aesthetically pleasing than the alternative.

In terms of aesthetics I can rank order the languages. Here they are from the prettiest to the ugliest (ugliest=JavaScript).

  1. Ruby
  2. Python
  3. JavaScript

Does that mean that I recommend learning Ruby as a first language?

Not really.

What Programming Language to Learn First?

Do this in order:

  1. HTML
  2. CSS
  3. JavaScript

JavaScript is the scripting language that you WILL learn if you care to write applications on the web.

All websites that have moving stuff use JavaScript.

Once you learn JavaScript, the other languages will be far easier to learn. I think switching from JavaScript to Ruby is actually interesting and fun.

If you come up learning Ruby, switching to JavaScript will seem like running with an irregularly sized weight chained to your hip.

Also, I’d suggest learning using Free Code Camp. I’m making a series of videos which I hope provide some special insights into the course work.

Enjoy!