there are even more algorithms

To start working with javascript first you need to understand some basic programming concepts.

What is programming?

A program is a set of instructions to tell the computer what to do.

When

there are even more algorithms

To start working with javascript first you need to understand some basic programming concepts.

What is programming?

A program is a set of instructions to tell the computer what to do.

When computer programming started, programming languages didn't exist,

computer programs where computer processor instructions based on binary code,

01100011 01101111 01100100 01100101

C O D E

Word code represented in ASCII binary code.

Since binary code is very cryptic thus hard to remember to how the human brain works, humans invented programming languages, a programming language looks something like this.

var name = "Israel";

What the computer does in background is:

store string value "Israel" in variable name

This is a very basic instruction we give to the computer.

Javascript is a language used to tell our computers how to solve problems through algorithms, what is an algorithm? In simple terms, is a formula which describes instructions to address a particular issue.

In this series, I will show you the basic bricks of the language javascript, with which you will be able to solve any algorithmic problem you can encounter.

First chapter: Into Javascript

The basic units of the language you will use to formulate your instructions are the fo

Like the letters of the abecedary and the numbers you use to do day to day math, there are certain types of units in a programming language; in javascript, they are as follows.

Glossary:

Strings

Numbers

Booleans

Object

Null/Undefined

Function

Symbol

But as in English grammar, programming languages also have grammar rules and names, the most basic unit of a program is called a statement, and looks like this:

if used as is, the sorting occurs converting elements to strings and using the Unicode value of the first letter of the string

Example

`let arr = [1,3,2,4,6,11,3,6,8,7,`

if used as is, the sorting occurs converting elements to strings and using the Unicode value of the first letter of the string

Example

`let arr = [1,3,2,4,6,11,3,6,8,7,9,5,10]; `

`arr.sort(); `

`console.log(arr); `

`// (12) [1, 10, 11, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9]`

The Unicode Character 10 comes before 2, so the result is what we see in the example.

Since number 10 or 11 does not appear on the Unicode list, the algorithm takes the first number it sees on the array to process the sorting.

The same happens when sorting words, the algorithm take the first letter to sort the array, let's see an example.

`let arr = ["house", "papaya", "GrandFleet", "Housing"]; `

`arr.sort(); `

`console.log(arr); `

`// ["GrandFleet", "Housing", "house", "papaya"];`

Capital letters come before lowercase letters in Unicode list, so the algorithm sort GrandFleet and Housing before house and papaya in the array, the next factor to take into account is the order of the letters in the alphabet.

However, what if we want to specify the condition by which to sort the array.

then we should pass a [compareFunction] parameter to the sort function

Syntax

arr.sort([compareFunction])

For example, lets sort from lowest to highest.

`let arr = [1,3,2,4,6,11,3,6,8,7,9,5,10]; `

`arr.sort((a, b) => { `

`return a - b; `

`}); `

`// (13) [1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11]`

The compare function takes control of the sorting mechanism, and the result is an array sorted from lowest to highest.

If you want to go deeper on sorting algorithms explained, check my post on sorting algorithms explained

bibliographyhttps://en.wikipedia.org/wiki/Sorting_algorithmhttps://unicode-table.com/es/#control-characterhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

]]>**What is an Algorithm?**

A process or set of rules to be followed in calculations or a set of problems. - solving operations, especially

Today we are going to go deeper into sorting algorithms to see the number of times they run to complete their cycle and also explore stability.

**What is an Algorithm?**

A process or set of rules to be followed in calculations or a set of problems. - solving operations, especially by a computer.

For this exercise, we are going to compare Sorting algorithms.

**Bubble sort**

This one is easy to understand, and that's why bubble sort is recommended for beginners.

For bubble sort algorithm we use do/while loop (probably you will never use it in production because it is not effective compared to other solutions.

```
let numbers = [10,5,3,8,2,6,4,7,9,1];
// lets sort it with bubble sort, first we create a function
const swapper = (numbers) => {
// to acumulate number of iterations
var iterations = 0;
do {
// if swapping is set to false, this will be the last time do run
var swapped = false;
// lets use i to add logic to our sorting algorithm
for (let i = 0; i < numbers.length; i++) {
// we want to sort from less than greater numbers so ->
if (numbers[i] > numbers[i+1]) { // 10 > 8 then
// we save our first number in an acumulator variable
var temp = numbers[i]; // 10
// swap the first number for the secnd
numbers[i] = numbers[i+1]; // change 10 for 8 in first loop
// swap second number for first number
numbers[i+1] = temp; // change 8 for 10 in first loop
// it swapped, lets do other iteration
swapped = true;
console.log(numbers);
// we want to know the number of iterations used to solve the sorting
console.log("iterations: "+ iterations++);
}
}
} while (swapped);
}
swapper(numbers);
// iterations: 25
```

**Insertion sort**

It's called insertionSort because we use splice to insert into the array when sorting.

Gist example Insertion sort

```
let nums = [10,5,3,8,2,6,4,7,9,1];
let insertionSort = nums => {
let iterations = 0;
// for each number starting on 2nd slot
for (let i = 1; i < nums.length; i++) {
//console.log("outer loop: " + nums[i]); // 5 (second number of array)
for (let j = 0; j < i; j++) {
//console.log("inner loop: " + nums[j]); // 10
if (nums[i] < nums[j]) { // if 5 is less than 10
// get one element from slot i (take from array, array gets modified)
let spliced = nums.splice(i, 1);
//insert spliced[0] in slot j, and displace array (if replace 0 for 1 slot 0 gets replaced instead of displaced)
nums.splice(j, 0, spliced[0]);
console.log("spliced: " + nums);
console.log("number of iterations: "+ ++iterations)
}
}
}
};
insertionSort(nums);
// number of iterations to sort: 9
```

The number of iterations got cut by half, neat!

**Merge sort**

Merge sort is the Algorithm most engines use when you run array.sort();

What merge sort does, is split array into 2 parts, then split those 2 arrays in 2, now we have 4 arrays, when the arrays can no longer be split in half, the algorithm starts to merge the numbers in order.

Merge sort algorithm is very stable because it does not move indexes if the numbers do get repeated.

Code example Merge sort

```
let nums = [10,5,3,8,2,6,4,7,9,1];
var iterations = 0;
const mergeSort = (nums) => {
++iterations;
console.log(iterations);
if(nums.length < 2) {
return nums;
}
const length = nums.length;
const middle = Math.floor(length / 2);
const left = nums.slice(0, middle); //left part of the array
const right = nums.slice(middle, length);
const sortedLeft = mergeSort(left);
const sortedRight = mergeSort(right);
return stitch(sortedLeft, sortedRight);
}
const stitch = (left, right) => {
const results = [];
while(left.length && right.length) {
if (left[0] <= right[0]) {
results.push(left.shift());
} else {
results.push(right.shift());
}
}
while(left.length) {
results.push(left.shift());
}
while(right.length) {
results.push(right.shift());
}
return results;
}
let result = mergeSort(nums);
console.log(result);
```

This representation of algorithms is for informational purposes; you should not use them in production, I recommend you use array.sort() native function for this purposes, since the code is written for humans and we should make our code understandable and semantic.

bibliography

http://www.visualcapitalist.com/intro-to-algorithms/

THOMAS H. CORMEN

CHARLES E. LEISERSON

RONALD L. RIVEST

CLIFFORD STEIN

Introduction to algorithms

2008

Massachusetts Institute of technology