Practice 50 JavaScript Sorting Algorithm Programming Questions, TechnoVlogs

Practice 50 JavaScript Sorting Algorithm Programming Questions


Q1. Write a JavaScript program to sort an array of numbers in ascending order using the Bubble Sort algorithm.  
Input: [5, 3, 8, 4, 2]  
Expected Output: [2, 3, 4, 5, 8]

Q2. Write a JavaScript program to sort an array of numbers in descending order using the Bubble Sort algorithm.  
Input: [5, 3, 8, 4, 2]  
Expected Output: [8, 5, 4, 3, 2]

Q3. Write a JavaScript program to sort an array of strings alphabetically using the Bubble Sort algorithm.  
Input: ["apple", "banana", "orange", "kiwi"]  
Expected Output: ["apple", "banana", "kiwi", "orange"]

Q4. Write a JavaScript program to implement the Insertion Sort algorithm to sort an array of numbers in ascending order.  
Input: [12, 11, 13, 5, 6]  
Expected Output: [5, 6, 11, 12, 13]

Q5. Write a JavaScript program to sort an array of numbers in descending order using the Insertion Sort algorithm.  
Input: [12, 11, 13, 5, 6]  
Expected Output: [13, 12, 11, 6, 5]

Q6. Write a JavaScript program to sort an array of numbers using the Selection Sort algorithm.  
Input: [64, 34, 25, 12, 22, 11, 90]  
Expected Output: [11, 12, 22, 25, 34, 64, 90]

Q7. Write a JavaScript program to sort an array of numbers in descending order using the Selection Sort algorithm.  
Input: [64, 34, 25, 12, 22, 11, 90]  
Expected Output: [90, 64, 34, 25, 22, 12, 11]

Q8. Write a JavaScript program to implement the Merge Sort algorithm to sort an array of numbers in ascending order.  
Input: [38, 27, 43, 3, 9, 82, 10]  
Expected Output: [3, 9, 10, 27, 38, 43, 82]

Q9. Write a JavaScript program to sort an array of numbers in descending order using the Merge Sort algorithm.  
Input: [38, 27, 43, 3, 9, 82, 10]  
Expected Output: [82, 43, 38, 27, 10, 9, 3]

Q10. Write a JavaScript program to sort an array of strings alphabetically using the Merge Sort algorithm.  
Input: ["dog", "cat", "elephant", "bee"]  
Expected Output: ["bee", "cat", "dog", "elephant"]

Q11. Write a JavaScript program to implement the Quick Sort algorithm to sort an array of numbers in ascending order.  
Input: [10, 7, 8, 9, 1, 5]  
Expected Output: [1, 5, 7, 8, 9, 10]

Q12. Write a JavaScript program to sort an array of numbers in descending order using the Quick Sort algorithm.  
Input: [10, 7, 8, 9, 1, 5]  
Expected Output: [10, 9, 8, 7, 5, 1]

Q13. Write a JavaScript program to implement the Heap Sort algorithm to sort an array of numbers in ascending order.  
Input: [12, 11, 13, 5, 6, 7]  
Expected Output: [5, 6, 7, 11, 12, 13]

Q14. Write a JavaScript program to sort an array of numbers in descending order using the Heap Sort algorithm.  
Input: [12, 11, 13, 5, 6, 7]  
Expected Output: [13, 12, 11, 7, 6, 5]

Q15. Write a JavaScript program to implement the Counting Sort algorithm to sort an array of numbers in ascending order.  
Input: [4, 2, 2, 8, 3, 3, 1]  
Expected Output: [1, 2, 2, 3, 3, 4, 8]

Q16. Write a JavaScript program to sort an array of numbers in descending order using the Counting Sort algorithm.  
Input: [4, 2, 2, 8, 3, 3, 1]  
Expected Output: [8, 4, 3, 3, 2, 2, 1]

Q17. Write a JavaScript program to sort an array of numbers in ascending order using the Radix Sort algorithm.  
Input: [170, 45, 75, 90, 802, 24, 2, 66]  
Expected Output: [2, 24, 45, 66, 75, 90, 170, 802]

Q18. Write a JavaScript program to sort an array of numbers in descending order using the Radix Sort algorithm.  
Input: [170, 45, 75, 90, 802, 24, 2, 66]  
Expected Output: [802, 170, 90, 75, 66, 45, 24, 2]

Q19. Write a JavaScript program to sort an array of numbers using the Shell Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q20. Write a JavaScript program to sort an array of numbers using the Cocktail Shaker Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q21. Write a JavaScript program to sort an array of numbers using the Tim Sort algorithm (using JavaScript’s built-in sort).  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q22. Write a JavaScript program to sort an array of strings by length using the Selection Sort algorithm.  
Input: ["apple", "dog", "banana", "kiwi"]  
Expected Output: ["dog", "kiwi", "apple", "banana"]

Q23. Write a JavaScript program to sort an array of numbers in ascending order using a custom Sorting function.  
Input: [5, 3, 8, 4, 2]  
Expected Output: [2, 3, 4, 5, 8]

Q24. Write a JavaScript program to sort an array of numbers in descending order using a custom Sorting function.  
Input: [5, 3, 8, 4, 2]  
Expected Output: [8, 5, 4, 3, 2]

Q25. Write a JavaScript program to sort an array of numbers using the Bubble Sort and count the number of swaps.  
Input: [5, 3, 8, 4, 2]  
Expected Output: Number of swaps: 6

Q26. Write a JavaScript program to sort an array of strings alphabetically and handle case-insensitivity.  
Input: ["apple", "Banana", "orange", "kiwi"]  
Expected Output: ["apple", "Banana", "kiwi", "orange"]

Q27. Write a JavaScript program to sort an array of objects by a property in ascending order.  
Input: [{name: "John", age: 25}, {name: "Jane", age: 22}, {name: "Mark", age: 30}]  
Expected Output: [{name: "Jane", age: 22}, {name: "John", age: 25}, {name: "Mark", age: 30}]

Q28. Write a JavaScript program to sort an array of objects by a property in descending order.  
Input: [{name: "John", age: 25}, {name: "Jane", age: 22}, {name: "Mark", age: 30}]  
Expected Output: [{name: "Mark", age: 30}, {name: "John", age: 25}, {name: "Jane", age: 22}]

Q29. Write a JavaScript program to sort an array using the Comb Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q30. Write a JavaScript program to sort an array using the Pigeonhole Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q31. Write a JavaScript program to sort an array of numbers in ascending order using the Binary Insertion Sort algorithm.  
Input: [12, 11, 13, 5, 6]  
Expected Output: [5, 6, 11, 12, 13]

Q32. Write a JavaScript program to sort an array of numbers in descending order using the Binary Insertion Sort algorithm.  
Input: [12, 11, 13, 5, 6]  
Expected Output: [13, 12, 11, 6, 5]

Q33. Write a JavaScript program to implement the Gnome Sort algorithm.  
Input: [34, 2, 10, 6, 7]  
Expected Output: [2, 6, 7, 10, 34]

Q34. Write a JavaScript program to sort an array using the Bogo Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q35. Write a JavaScript program to sort an array of numbers in ascending order using the Bucket Sort algorithm.  
Input: [0.42, 0.32, 0.23, 0.52, 0.26, 0.34]  
Expected Output: [0.23, 0.26, 0.32, 0.34, 0.42, 0.52]

Q36. Write a JavaScript program to sort an array of integers using the LSD Radix Sort algorithm.  
Input: [170, 45, 75, 90, 802, 24, 2, 66]  
Expected Output: [2, 24, 45, 66, 75, 90, 170, 802]

Q37. Write a JavaScript program to sort an array using the MSD Radix Sort algorithm.  
Input: [170, 45, 75, 90, 802, 24, 2, 66]  
Expected Output: [2, 24, 45, 66, 75, 90, 170, 802]

Q38. Write a JavaScript program to sort an array of numbers in ascending order using the Odd-Even Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q39. Write a JavaScript program to sort an array using the Pile Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q40. Write a JavaScript program to implement the Stooge Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q41. Write a JavaScript program to sort an array using the Cycle Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q42. Write a JavaScript program to sort an array using the Patience Sorting algorithm.  
Input: [12, 11, 13, 5, 6, 7]  
Expected Output: [5, 6, 7, 11, 12, 13]

Q43. Write a JavaScript program to sort an array using the Binary Heap Sort algorithm.  
Input: [12, 11, 13, 5, 6, 7]  
Expected Output: [5, 6, 7, 11, 12, 13]

Q44. Write a JavaScript program to sort an array of strings in lexicographical order.  
Input: ["apple", "banana", "grape"]  
Expected Output: ["apple", "banana", "grape"]

Q45. Write a JavaScript program to sort an array using the K-way Merge Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q46. Write a JavaScript program to sort an array using the Split-Merge Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q47. Write a JavaScript program to sort an array of numbers using the Adaptive Merge Sort algorithm.  
Input: [12, 11, 13, 5, 6, 7]  
Expected Output: [5, 6, 7, 11, 12, 13]

Q48. Write a JavaScript program to implement the Topological Sort algorithm.  
Input: [{vertex: "A", edges: ["B", "C"]}, {vertex: "B", edges: ["D"]}, {vertex: "C", edges: ["D"]}, {vertex: "D", edges: []}]  
Expected Output: ["A", "B", "C", "D"]

Q49. Write a JavaScript program to sort an array using the Randomized Quick Sort algorithm.  
Input: [5, 2, 9, 1, 5, 6]  
Expected Output: [1, 2, 5, 5, 6, 9]

Q50. Write a JavaScript program to sort an array using the Top-Down Merge Sort algorithm.  
Input: [12, 11, 13, 5, 6, 7]  
Expected Output: [5, 6, 7, 11, 12, 13]

Share on Social Media