## 2 - Arrays & Sorting

How many ways can you sort a complete mess of numbers? Is there *one* fastest method? In this unit we will begin to build our **library** of useful functions, beginning with random number generators, then building randomized arrays, and finishing with common sorting algorithms.

This course focuses on **Computer Science** and *not* specifically JavaScript. We will avoid using built-in functions that do the work for us. Once we become CS geniuses, we can use language-specific tools. For now, let's learn.

### 2.1 - Random Library Functions

We will build a * library* for ourselves that contains useful functions. Look for the

*project in our new Repl.it course (see 3.0 above).*

**Helper Library**- randInt(min, max, inclusive = true)

*numbers*, 'inclusive' is a

*boolean*to determine if min and max are included or excluded (defaults to true). This function has been coded for you.

- randNumOfLength(precision)

**Note:**The number 487 is

*not*0487 or 000487. Precision is exact.

- round(number, precision)

- randChar(letterCase = 1)

*character either from A to Z (if 'letterCase' is 1 or 'upper') or a to z (if 'letterCase' is 2 or 'lower'). letterCase will default to 1.*

**Latin letter**- randString(maxLength, letterCase = 0)

**The string is made up of alpha letters A-Z and/or a-z**. 'letterCase' can be 1 or 'upper' for all uppercase, 2 or 'lower' for all lowercase, and 0 or 'mixed' for a mix of letters. See the hints section for help on this. letterCase will default to zero.

**Important: **Do not use .toFixed() as it does not round correctly.

2.1 - Hints

- Characters (letters) are stored using ASCII codes which are stored in a table.

- String.fromCharCode(num) will return a character from the ASCII table represented by 'num'
- You can add to the end of a string (called concatenation)

- Math.pow(), Math.round() and Math.random() will be your friends.
- If you want a random number 4 digits long, that is a random number between 1000 and 9999
*inclusive*. Interesting fact: 9999 = 10 000 - 1 - Zero is usually a special case. You might need to take that into account, especially in
*randNumOfLength( )*because randNumOfLength(1) should return a number from 0 to 9.

### 2.2 - Array Library Functions

Now let's create some functions that deal with arrays.

First - at the top of your code document, *just* after the heading comment block, add the following line exactly:

"use strict";

That will ensure you are following correct JavaScript guidelines.

- inArray(item, array)

**Important note:**"3" != 3 Strings and Numbers should be considered

*unequal*.

- arrayOfInts(length, min, max, inclusive, allowDuplicates)

*random*integer from min to max. 'inclusive' is a

*boolean*to determine if min and max are included or excluded (defaults to true). 'allowDuplicates' defaults to true and determines if duplicates are permitted. Heads up, for testing purposes - if allowDuplicates is false, the range from min to max

*must*be >= length. You do not need to error check for that, it's just a note for your own sanity.

- arrayOfStrings(length, maxStrLength, letterCase)

*random*strings, each of length 1 to

**maximum**'maxStrLength'. 'letterCase' can be 1 or 'upper' for all uppercase, 2 or 'lower' for all lowercase, and 0 or 'mixed' for a mix of letters. See the hints section for help on this.

**Note:** we are not permitted to use .sort(), .reverse(), .split(), .join(), or .includes() If you are unsure about any built-in functions ask the teacher.

2.2 - Hints

- Create an empty array variable:

- Add an element to (or change) an array:

- .length gives the number of elements in the array

*zero*.

- .push(x) will add 'x' to the end of an array (can be multiple items) and return the new length.
- .pop() will remove
*and*return the last element. - .shift() will remove
*and*return the first element, shifting all elements left one. - .unshift(x) will add 'x' to the beginning of the array (can be multiple items) and return the new length.

### 2.3 - Insertion Sort

2.3 - Hints

- You will need a 'for' loop that goes through each item of the array starting at index 1 (the second element).
- Begin looking left of that position and shift elements to the right (one-by-one) if they are larger than our temporary value.
- Each time you finish looking left and insert the item, that is called a completed
*pass*.

2.4 - Hints

- You know how to swap items in an array from Insertion Sort.
- shakerSort( ) is an adjustment of bubbleSort( )
- Reduce the length of each pass by 1 as items bubble to the appropriate end.

2.5 - Hints

- You
*should*know how to go through an array - You
*should*know how to reduce the search window by 1, similar to Bubble Sort - You
*should*know how to swap items in an array

### Useful Links

- JavaScript Arrays
- JavaScript Math Object
- JavaScript Math.random() examples
- Univ. of San Fran. Visual Sorting Comparison
- xSortLab - Visualization of Sorting
- Wikipedia Article on Sorting Algorithms