Because JS does not have C language pointer, so we here simulate array just simulate the idea of array operation.
Because my level is limited, welcome you to correct, read can point a thumbs-up would be better, thank you.
Start by creating an ArrayList class.
class ArrayList {
list: any[]
length: number
constructor(list = []) {
this.list = list
this.length = list.length
}
}
Copy the code
Add (push)
As we know from ordinary use, the array push method is to add elements to the last bit of the array, the array length will be this.length + 1, the array subscript of the last bit of the array is this.length. So our new method can be written as:
appendChild(item: any) {
this.list[this.length] = item
this.length++
}
Copy the code
Delete (specifies the subscript of the element to be deleted)
Assume that the deleted element has an index of I
- Start by finding the elements that need to be deleted
this.list[i]
- Array elements subscript from
i
To start,this.list[i] = this.list[i + 1]
, the element is deleted, and all subsequent elements move forward one bit. - Modifying the array length
- Returns the deleted element
removeChild(index: number) {
let removeItem = this.list(index)
letLength = this.length // Move forward from the deleted elementfor (leti = index; i < length - 1; I ++) {this.list[I] = this.list[I + 1]} // Delete this.list[length-1] this.list -- this.length--return removeItem
}
Copy the code
reverse
- Set two variables
i,j
, corresponding to the end of the array that needs to be reversed - I switch heads and tails,
i++, j--
- when
i >= j
Stop swapping - Return a new array
// Inversion inversion(arys = null) {let list = arys || this.list
letLength = list.length - 1 // I represents the header and j represents the tail subscriptlet i = 0, j = length - i, t: any;
while (j > i) {
t = list[i]
list[i] = list[j]
list[j] = t
i++
j--
}
return list
}
Copy the code
insert
This method takes two parameters, the index of the insertion position and the element being inserted.
- Get the array length
- Move the element in the inserted position back one bit, starting with the last bit
this.list[length] = this.list[length - 1]
- Insert the element at the inserted position
- Modifying the array length
// Insert (index: number, item: any) {letLength = this.length // Moves elements from the last digit to the digit before the inserted subscriptfor (let i = length; i > index; i--) {
this.list[i] = this.list[i - 1]
}
this.list[index] = item
this.length++
return true
}
Copy the code
Sort (quicksort)
- Find the base index of the sorted array (find the middle digit to simplify operations)
- Find the reference value according to the subscript
- Loop through the array, placing the number greater than the reference value
right
Array of numbers less than the reference valueleft
An array of - right
The right and left
Array, recursive traversal sort - The output
left
+At baseline
+right
An array of
quicksort(arys, ) {
const ary = arys.slice(0)
if (ary.length <= 1) {
returnAry} // Base value subscriptletpivotIndex = Math.floor(ary.length / 2); At baselinelet pivot = ary.splice(pivotIndex, 1);
const left = [], right = [];
for (leti = 0; i < ary.length; I++) {// the current element is greater than the reference valueif (ary[i] > pivot) {
right.push(ary[i])
} else {
left.push(ary[i])
}
}
return this.quicksort(left).concat(pivot, this.quicksort(right))
}
Copy the code
Welcome to guide the message, thank you for your support.