Alogrithms Flashcards
What is the condition to use binary search?
array must be sorted
What is an inplace sorting alogrithm?
An in-place algorithm is an algorithm which transforms input using no auxiliary data structure.
Write the selection sort formula
const array = [1,4,5,3,2]
function selectionSort(array){
for(let i = 0; i < array.length-1; i++){ let min = i; for(let j = i+1; j < array.length; j++){ console.log(array[j]) if(array[j] < array[min] ){ min = j; } } if(min !== i){ let temp = array[i]; array[i] = array[min]; array[min] = temp; } } return array; }
console.log(selectionSort(array))
What does selection sort do?
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning
What is bubble sort?
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
What is time complexity fo selection sort?
O of n squared
What is the time complexity of bubble sort
O of n squared
Write the algorithm for bubble sort
const array = [1,4,5,3,2] const array2 = [1,2,3,4,5]
function bubbleSort(array){ for(let i = 0; i < array.length-1; i++){ // let flag = 0; for(let j = 0; j < array.length; j++){ if(array[j]>array[j+1]){ let temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; flag++ } } if(flag===0) return array; }
return array;
}
console.log(bubbleSort(array2))
What is insertion sort?
Switch current element with all previous elements that are larger.
Write the insertion sort algorithm
const array = [1,4,5,3,2] const array2 = [1,2,3,4,5]
function insertionSort(array){ for(let i = 1; i < array.length; i++){ let value = array[i]; let currIdx = i; while(currIdx > 0 && value < array[currIdx-1]){ array[currIdx] = array[currIdx-1]; array[currIdx-1] = value; currIdx-- } } return array; }
console.log(insertionSort(array))
What is the time complexity of insertion sort?
O of n squared
Write the algorithm for merge sort
//Merge Sort
const array = [1,4,5,3,2] const array2 = [1,2,3,4,5]
function mergeSort(array){ if(array.length < 2) return array
let mid = Math.floor(array.length/2); let left = array.slice(0,mid); let right = array.slice(mid);
return merge(
mergeSort(left),
mergeSort(right)
)
}
function merge (left, right) { let result = [] let indexLeft = 0 let indexRight = 0
while (indexLeft < left.length && indexRight < right.length) { if (left[indexLeft] < right[indexRight]) { result.push(left[indexLeft]) indexLeft++ } else { result.push(right[indexRight]) indexRight++ } }
return result.concat(left.slice(indexLeft)).concat(right.slice(indexRight))
}
console.log(mergeSort(array))
What is a stack
A data structure that serves as a collection of elements, with two principle operations: push and pop. Push adds an element to the collection. Pop removes the last element that was added. This data structure follows the “Last In, First Out” principle (LIFO), where the elements that were most recently pushed (added) to the stack, are popped off (removed) first.
Think of a stack of pancakes - you add pancakes to the top of the stack and eat them from top to bottom.
What is a queue
A data strucure that serves as a collection of elements that preserves the order at which the elements are inserted. The principle operations are enqueue, which adds an element to the collection, and dequeue, which removes the element that was added the earliest. This data structure follows the “First In, First Out” principle (FIFO), where elements that are enqueued (added) into the queue first, are dequeue (removed) first.
Think of a line for a roller coaster - the people that enter the line first, are the ones that get on the ride first.
What is a linked list?
A data structure that serves as a collection of elements. Data is stored in nodes, and each node has a reference to the next node. In order to access elements from a linked list, the first node (or head) is checked. If the element is not present in the first node, then the node that is referenced by the head (the second node) is checked. This action is repeated until the entire list is inspected. The two principle operations are push (or add) and contains. Push creates a new node and adds
it to the end of the list. Contains checks the entire list and determines if the element is present in any of the nodes.