JavaScript Sorting Algorithms - Interview Notes
Built-in sort()
Ascending: [Link]((a,b)=>a-b)
Descending: [Link]((a,b)=>b-a).
Bubble Sort
Compare adjacent elements and swap if needed. Time O(n^2), Space O(1).
Selection Sort
Find minimum and swap. Time O(n^2), Space O(1).
Insertion Sort
Insert into sorted portion. Best O(n), Worst O(n^2).
Merge Sort
Divide and merge. Time O(n log n), Space O(n).
Quick Sort
Pivot and partition. Average O(n log n), Worst O(n^2).
Find Minimum
let min=arr[0]; for(const n of arr){ if(n<min) min=n; }
Find Maximum
let max=arr[0]; for(const n of arr){ if(n>max) max=n; }
Interview Tips
Know algorithm, complexity, stability, and when to use each.