Introduction:
In the fast-paced world of programming interviews, JavaScript skills play a crucial role. Sorting algorithms are a common topic that interviewers love to explore. To help you ace your JavaScript interviews, here are 3 essential sorting exercises along with sample solutions.
1: Bubble Sort Basics:
Exercise: Implement the Bubble Sort algorithm in JavaScript and explain its time complexity.
Sample Solution:
function bubbleSort(arr) {
let n = arr.length;
// Outer loop for each pass
for (let i = 0; i < n - 1; i++) {
// Inner loop for pairwise comparisons
for (let j = 0; j < n - i - 1; j++) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
2: Merge Sort Mastery:
Exercise: Write a JavaScript function to perform Merge Sort and discuss its advantages over other sorting algorithms.
Sample Solution:
<!-- Exercise: Write a JavaScript function to perform Merge Sort and discuss its advantages over other sorting algorithms. -->
function mergeSort(arr) {
if (arr.length <= 1) return arr;
// Split the array into halves
const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);
// Recursively apply mergeSort to both halves and merge them
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
let result = [];
let leftIndex = 0;
let rightIndex = 0;
// Merge the two sorted arrays
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
// Add remaining elements from both arrays
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
3: Quick Sort Challenge:
Exercise: Implement the Quick Sort algorithm in JavaScript, emphasizing its efficiency and partitioning strategy.
Sample Solution:
function quickSort(arr) {
if (arr.length <= 1) return arr;
// Choose the pivot (last element in this case)
const pivot = arr[arr.length - 1];
const left = [];
const right = [];
// Partition the array into elements smaller than pivot and larger than pivot
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
// Recursively apply quickSort to both partitions and concatenate them with pivot
return [...quickSort(left), pivot, ...quickSort(right)];
}
4: Selection Sort Unveiled:
Exercise: Write a JavaScript function for Selection Sort and discuss its simplicity compared to other algorithms.
Sample Solution:
function selectionSort(arr) {
const n = arr.length;
// Traverse through all array elements
for (let i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted part of the array
let minIndex = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
if (minIndex !== i) {
let temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
return arr;
}
Top comments (0)