Skip to content

Instantly share code, notes, and snippets.

@QuocCao-dev
Last active October 15, 2022 18:50
Show Gist options
  • Save QuocCao-dev/abd609e79594109d5621d058fc478955 to your computer and use it in GitHub Desktop.
Save QuocCao-dev/abd609e79594109d5621d058fc478955 to your computer and use it in GitHub Desktop.
//
// O(1) Time Complexity, O(1) Space complexity
const push = (array: number[], num: number): number[] => {
array[array.length] = num;
return array;
};
// O(1) Time Complexity, O(1) Space complexity
const pop = (array: number[]): number[] => {
array.length--;
return array;
};
// shift : xoá ở đầu
// O(n) Time Complexity, O(1) Space complexity
const shift = (array: number[]): number[] => {
for (let i = 0; i < array.length; i++) {
array[i] = array[i + 1];
}
array.length--;
return array;
};
// unshift : thêm vào đầu
// O(n) Time Complexity, O(1) Space complexity
const unshift = (array: number[], number: number): number[] => {
for (let i = array.length - 1; i >= 0; i--) {
array[i + 1] = array[i];
}
array[0] = number;
return array;
};
unshift([1, 2, 3, 4], 0);
// [1,2,3,4]
// newArray []
// Bubble Sort O(n^2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment