Arrays are used to store multiple values in a single variable.
Each value is called an element, and each element has a numeric position in the array, known as its index.
Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.
Arrays can contain any data type, including numbers, strings, and objects.
const arr1 = [2, 4, 6]; array
arr1[0]; element at index 0 → 2
arr1[1]; element at index 1 → 4
arr1[2]; element at index 2 → 6
arr1[3]; element at index 3 → undefined index not found
Strings are a sequence of zero or more characters written inside quotes used to represent text.
Strings may consist of letters, numbers, symbols, words, or sentences.
Strings are immutable, they cannot be changed.
Each character in a string has an index.
The first character will be index 0 the second character will be index 1 and so on.
There are two ways to access an individual character in a string.
charAt() method
const str1 = "abc"; string
str1.charAt(0); character at index 0 → "a"
str1.charAt(1); character at index 1 → "b"
str1.charAt(2); character at index 2 → "c"
str1.charAt(3); character at index 3 → "" index not found
Alternatively use at() or slice() methods
bracket notation []
const str2 = "abc"; string
str2[0]; character at index 0 → "a"
str2[1]; character at index 1 → "b"
str2[2]; character at index 2 → "c"
str2[3]; character at index 3 → undefined index not found
Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Sort an array using the Quick Sort algorithm using ↴
recursion → programming technique where a function calls itself repeatedly to solve a problem.
for loop → executes a block of code a number of times.
push() method → adds specified elements to the end of an array and returns the new length of the array.
Recursion The act of a function calling itself.
Recursion is used to solve problems that contain smaller sub-problems.
A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).
Use recursion to find the factorial of 5.
let x = 5;
function factorial(num) {
if (num > 1) { Recursion call
return num * factorial(num - 1);
}
else { Base case
return 1;
};
}
call function
factorial(x); returns ↴
120 factorial of 5 → 120
for loop repeatedly executes a block of code until a specified condition evaluates to false.
The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.
for (let x = 0; x < 4; x++) {
console.log(x);
}
Loop variable x is initialized to 0
Condition x < 4 is checked before each iteration.
The loop will continue to run as long as x is less than 4
The loop repeatedly executes a block of code 4 times, from 0 to 3
For each iteration of the loop, the current value of x is printed to the console.
After each iteration, x is incremented by 1 x++
When x reaches 4 the condition evaluates to false, terminating the loop.
0
1
2
3 → printed to console
push() method adds new elements to the end of an array.
Add 4 to end of array.
const arr2 = [1, 2, 3];
arr2.push(4);
console.log(arr2); returns ↴
[1, 2, 3, 4] → 4 added to end of array
The push() method changes the length of the array.
arr2 is modified.
Using the spread operator creates a new array.
Add 4 to a new array.
const arr3 = [1, 2 , 3];
const arr4 = [...arr3, 4];
console.log(arr4); returns ↴
[1, 2, 3, 4] → 4 added to new array
console.log(arr3); returns ↴
[1, 2 ,3]
arr3 remains unchanged.
length property returns the number of elements in an array.
const arr5 = [1, 2, 3, 4, 5, 6];
arr5.length; returns ↴
6 → there are 6 elements in the array
Arrays are zero indexed, the first element will be index 0
The last element will be at index length -1
To find the last index in an array.
const arr6 = [1, 2, 3, 4, 5, 6];
arr6.length - 1; returns ↴
5 → end index of array
spread syntax ... unpacks the elements of an iterable object, like arrays.
It allows two or more arrays to be merged into one array.
const arr7 = [1, 2, 3, 4, 5];
const arr8 = [4, 5, 6, 7, 8];
const arr9 = [...arr7, ...arr8];
console.log(arr9); returns ↴
[1, 2, 3, 4, 5, 4, 5, 6, 7, 8] → values are not unique
Initialize an array to sort.
const array1 = [10, 4, 8, 2, 6]; → user input
Define a function quickSort() to sort an array.
function quickSort(arr) {}
The function takes an array as input arr and returns a new array sorted in ascending order, using the Quick Sort algorithm.
The original array will be unchanged.
Quick Sort is a recursive algorithm that works by selecting a pivot element from the array and partitioning the other elements into two sub-arrays: those less than the pivot and those greater than the pivot.
The process is repeated recursively for the sub-arrays until the base case is reached, where the array has one or zero elements, which are inherently sorted.
Recursion is a programming technique in which a function calls itself repeatedly until it reaches a certain condition.
It is used to solve problems that can be broken down into smaller, simpler subproblems.
Base case halting point of the recursive function.
If input array arr has one or zero elements, it is already sorted, and the function returns the array.
if (arr.length <= 1) {
return arr
}
Recursive call array has more than one element.
The choice of pivot significantly affects the efficiency of the Quick Sort algorithm.
Common pivot selection strategies include choosing the first, last, or middle element, or using the median of three random elements.
The last element of the array is selected as the pivot.
const pivot = arr[arr.length - 1] pivot
Initialize an empty array for elements less than the pivot
const left = [] left
Initialize an empty array for elements greater than the pivot
const right = [] right
Iterate through the loop, excluding the pivot, the last element.
arr.length - 1 loop terminates before reaching last element.
for (let x = 0; x < arr.length - 1 x++)
The loop iterates through the array, excluding the pivot,
to compare each element with the pivot.
Elements less than the pivot are pushed to the left array,
otherwise, the other elements go to the right array.
Compare the current element arr[x] with the pivot
If arr[x] less than pivot
if (arr[x] < pivot) {}
add to left array
left.push(arr[x])
else, if arr[x] greater than pivot add to right array
right.push(arr[x])
The function calls itself recursively on the left and right arrays,
combining the results with the pivot using the spread operator
return [...quickSort(left), pivot, ...quickSort(right)]
Call the function with ↴
quickSort(array1);
Sort an array using the Quick Sort algorithm
Last element of the array is selected as the pivot
If element less than the pivot move to left.
If element greater than the pivot move to right.
[7, 2, 9, 1, 4, 8, 3, 6, 5] unsorted array
Selected pivot is the last element: 5
pivot5
[7, 2, 9, 1, 4, 8, 3, 6, 5]
← less than | greater than →
[2, 1, 4, 3] 5 [7, 9, 8, 6]
pivot3 : pivot6
[2, 1, 4, 3] 5 [7, 9, 8, 6]
[2, 1] 3 [4] 5 [] 6 [7, 9, 8]
pivot1 : pivot4 : pivot[] : pivot8
[2,1] 3 [4] 5 [] 6 [7, 9,8]
[] 1 [2] 3 [4] 5 6 [7] 8 [9]
If the array has one or zero elements, it is already sorted.
left array + pivot + right array
[1, 2, 3, 4] + 5 + [6, 7, 8, 9] returns ↴
[1, 2, 3, 4, 5, 6, 7, 8, 9] → sorted array
Sort an array using the Quick Sort algorithm
const array1 = [7, 2, 9, 1, 4, 8, 3, 6, 5];
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length - 1];
const left = [];
const right = [];
for (let x = 0; x < arr.length - 1; x++) {
if (arr[x] < pivot) {
left.push(arr[x]);
} else {
right.push(arr[x]);
}
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
call function
quickSort(array1); returns ↴
[1, 2, 3, 4, 5, 6, 7, 8, 9]
To sort a mixed numeric alphanumeric array ↴
Separate the strings and numbers into different arrays, sort them individually, and then merge them back together.
const arr1 = [1, 2, 3]; sorted numbers
const arr2 = ["a", "b", "c"]; sorted strings
const arr3 = [...arr1, ...arr2]; merge
console.log(arr3); returns ↴
[1, 2, 3, "a", "b", "c"]
Alternatives to sort order of array.
Change the comparison test ↴
arr[x] < pivot ⇡ ascending order
arr[x] > pivot ⇣ descending order
Alternative → combine arrays and pivot using concat() method ↴
return [...quickSort(left), pivot, ...quickSort(right)]; ↴
return quickSort(left).concat(pivot, quickSort(right));