3 - Algorithms & Efficiency

How many ways can you sort a complete mess of numbers? Is there one fastest method? In this unit we will code and compare some common sorting algorithms and then dive further into algorithm design in Unit 4.

Insertion Sort DemoSwfung8 / CC BY-SA

3.0 - Library of Functions (lib)

The library.js file you have been using for round() and randInt() has been updated. The functions are documented inside library.js and VSCode will pick up the documentation. Most of the added functions are to help you with creating test data.

Brief Descriptions of the most useful functions (see library.js for fill list):

Returns a random integer where min and max are included.
Returns a rounded version of 'number' to the given 'precision' of decimals (>= 0).
Returns a random whole number where 'numOfDigits' controls the number of digits in the number (> 0). Note:ย  No leading zeros. 487 is not 0487 or 000487. numOfDigits is exact.
Returns a random Latin letter character either from A to Z (if 'upperCase' is true) or a to z (if 'upperCase' is false). upperCase will default to true.
Returns a random string of maximum length 'maxLength' (> 0). The string is made up of alpha letters A-Z and/or a-z. 'letterCase' can be 0 for all uppercase, 1 for all lowercase, and 2 for a mIX Of CaSEs. See the hints section for help on this. letterCase will default to zero (mixed).
Returns an array of size 'length' where each element is a random integer from min to max, inclusively.ย
Returns an array (of length 'length') containing random strings, each of length 1 to maximum ย 'maxStrLength'. 'letterCase' can be 0 for all uppercase, 1 for all lowercase, and 2 for a mix of cases. See the hints section for help on this.
Returns an array (of length 'length') containing random strings or numbers. Strings are of length 1 to maximum ย 'maxStrLength'. 'letterCase' can be 0 for all uppercase, 1 for all lowercase, and 2 for a mix of cases. Numbers are any random integer from min to max, inclusive.

3.1 - Hints

3.2 - Bubble (Shaker) Sort

ย ย ย ย (shaker visualization video)

3.2 - Hints

3.3 - Selection Sort

3.3 - Hints

Wait for Mr. Brash to explain before you click the link below.

Reminder

Arrays in JS are passed & copied by reference

What does this mean - by reference?

It means that when you try to make a copy or pass an array into a function, it does not actually duplicate every item in the array to create separate arrays. It makes a link or shortcut to the array.

Example

let origSheeps = ['๐', '๐'];
let sheeps2 = origSheeps;

sheeps2.push('๐บ');

console.log(sheeps2);
ย  ย  // [ '๐', '๐', '๐บ' ]

console.log(origSheeps);
ย  ย  // [ '๐', '๐', '๐บ' ]

๐ฑ , our original sheeps have changed?!

(source)