Practice 50 PHP Searching and Sorting Programming Questions
Q1. Write a PHP program to implement linear search on an array of integers.
Input: Array = [10, 20, 30, 40, 50], Search Key = 30
Expected Output: "Element found at index 2"
Q2. Write a PHP program to implement binary search on a sorted array of integers.
Input: Array = [1, 2, 3, 4, 5], Search Key = 4
Expected Output: "Element found at index 3"
Q3. Write a PHP program to sort an array of integers using the Bubble Sort algorithm.
Input: Array = [64, 34, 25, 12, 22, 11, 90]
Expected Output: Sorted Array = [11, 12, 22, 25, 34, 64, 90]
Q4. Write a PHP program to sort an array of integers using the Selection Sort algorithm.
Input: Array = [29, 10, 14, 37, 13]
Expected Output: Sorted Array = [10, 13, 14, 29, 37]
Q5. Write a PHP program to sort an array of integers using the Insertion Sort algorithm.
Input: Array = [9, 5, 1, 4, 3]
Expected Output: Sorted Array = [1, 3, 4, 5, 9]
Q6. Write a PHP program to find the maximum element in an array.
Input: Array = [3, 29, 12, 44, 5]
Expected Output: "Maximum element is 44"
Q7. Write a PHP program to find the minimum element in an array.
Input: Array = [3, 29, 12, 44, 5]
Expected Output: "Minimum element is 3"
Q8. Write a PHP program to implement recursive binary search on a sorted array.
Input: Array = [2, 4, 6, 8, 10], Search Key = 6
Expected Output: "Element found at index 2"
Q9. Write a PHP program to sort an array of integers using the Merge Sort algorithm.
Input: Array = [12, 11, 13, 5, 6, 7]
Expected Output: Sorted Array = [5, 6, 7, 11, 12, 13]
Q10. Write a PHP program to sort an array of integers using the Quick Sort algorithm.
Input: Array = [10, 80, 30, 90, 40, 50, 70]
Expected Output: Sorted Array = [10, 30, 40, 50, 70, 80, 90]
Q11. Write a PHP program to implement linear search to find all occurrences of a number in an array.
Input: Array = [10, 20, 10, 30, 10], Search Key = 10
Expected Output: "Element found at indices: 0, 2, 4"
Q12. Write a PHP program to check if an array is already sorted in ascending order.
Input: Array = [1, 2, 3, 4, 5]
Expected Output: "Array is sorted in ascending order"
Q13. Write a PHP program to implement the counting sort algorithm for integers.
Input: Array = [4, 2, 2, 8, 3, 3, 1]
Expected Output: Sorted Array = [1, 2, 2, 3, 3, 4, 8]
Q14. Write a PHP program to sort an array of strings using Bubble Sort.
Input: Array = ["banana", "apple", "cherry", "date"]
Expected Output: Sorted Array = ["apple", "banana", "cherry", "date"]
Q15. Write a PHP program to find the second smallest element in an array.
Input: Array = [7, 1, 3, 9, 5]
Expected Output: "Second smallest element is 3"
Q16. Write a PHP program to find the second largest element in an array.
Input: Array = [7, 1, 3, 9, 5]
Expected Output: "Second largest element is 7"
Q17. Write a PHP program to implement a search for a string in an array of strings.
Input: Array = ["apple", "banana", "cherry"], Search Key = "banana"
Expected Output: "String found at index 1"
Q18. Write a PHP program to count the number of times a specific number appears in an array.
Input: Array = [1, 2, 2, 3, 4, 2], Search Key = 2
Expected Output: "The number 2 appears 3 times"
Q19. Write a PHP program to sort an array of integers using the Heap Sort algorithm.
Input: Array = [4, 10, 3, 5, 1]
Expected Output: Sorted Array = [1, 3, 4, 5, 10]
Q20. Write a PHP program to sort an array of integers using the Shell Sort algorithm.
Input: Array = [12, 34, 54, 2, 3]
Expected Output: Sorted Array = [2, 3, 12, 34, 54]
Q21. Write a PHP program to find the median of an unsorted array.
Input: Array = [12, 4, 5, 3, 8, 7]
Expected Output: "Median is 6"
Q22. Write a PHP program to sort an array of integers using descending order Bubble Sort.
Input: Array = [1, 4, 2, 8, 5]
Expected Output: Sorted Array = [8, 5, 4, 2, 1]
Q23. Write a PHP program to search for an element in a 2D array.
Input: Array = [[1, 2], [3, 4]], Search Key = 3
Expected Output: "Element found at row 1, column 0"
Q24. Write a PHP program to implement jump search on a sorted array of integers.
Input: Array = [1, 3, 5, 7, 9], Search Key = 7
Expected Output: "Element found at index 3"
Q25. Write a PHP program to implement the comb sort algorithm.
Input: Array = [8, 4, 1, 56, 3, -44, 23, -6, 28, 0]
Expected Output: Sorted Array = [-44, -6, 0, 1, 3, 4, 8, 23, 28, 56]
Q26. Write a PHP program to find the kth smallest element in an array.
Input: Array = [7, 10, 4, 3, 20, 15], k = 3
Expected Output: "3rd smallest element is 7"
Q27. Write a PHP program to find the kth largest element in an array.
Input: Array = [7, 10, 4, 3, 20, 15], k = 2
Expected Output: "2nd largest element is 15"
Q28. Write a PHP program to find the position of the first negative number in an array.
Input: Array = [5, -3, 10, -8, 12]
Expected Output: "First negative number is at index 1"
Q29. Write a PHP program to sort an array of strings in reverse alphabetical order.
Input: Array = ["apple", "banana", "cherry"]
Expected Output: Sorted Array = ["cherry", "banana", "apple"]
Q30. Write a PHP program to search for multiple values in an array and return their indices.
Input: Array = [1, 5, 7, 5, 3, 7], Search Keys = [5, 7]
Expected Output: "5 found at indices: 1, 3; 7 found at indices: 2, 5"
Q31. Write a PHP program to sort an array of integers using Gnome Sort.
Input: Array = [34, 2, 10, -9]
Expected Output: Sorted Array = [-9, 2, 10, 34]
Q32. Write a PHP program to implement a case-insensitive search for a string in an array.
Input: Array = ["Apple", "Banana", "Cherry"], Search Key = "banana"
Expected Output: "String found at index 1"
Q33. Write a PHP program to implement ternary search on a sorted array.
Input: Array = [1, 3, 5, 7, 9, 11, 13], Search Key = 11
Expected Output: "Element found at index 5"
Q34. Write a PHP program to remove duplicate values from a sorted array.
Input: Array = [1, 1, 2, 3, 3, 4, 4, 5]
Expected Output: Array without duplicates = [1, 2, 3, 4, 5]
Q35. Write a PHP program to count the number of elements less than a given value in a sorted array.
Input: Array = [10, 20, 30, 40, 50], Search Value = 35
Expected Output: "3 elements are less than 35"
Q36. Write a PHP program to check if two arrays are equal after sorting.
Input: Array1 = [3, 1, 2], Array2 = [2, 3, 1]
Expected Output: "Arrays are equal"
Q37. Write a PHP program to find the largest even number in an array.
Input: Array = [5, 12, 3, 18, 7]
Expected Output: "Largest even number is 18"
Q38. Write a PHP program to find the largest odd number in an array.
Input: Array = [4, 8, 11, 15, 2]
Expected Output: "Largest odd number is 15"
Q39. Write a PHP program to count the number of even numbers in an array.
Input: Array = [5, 12, 3, 18, 7]
Expected Output: "2 even numbers found"
Q40. Write a PHP program to count the number of odd numbers in an array.
Input: Array = [4, 8, 11, 15, 2]
Expected Output: "2 odd numbers found"
Q41. Write a PHP program to implement exponential search for a number in a sorted array.
Input: Array = [1, 2, 3, 4, 5, 6, 7, 8], Search Key = 6
Expected Output: "Element found at index 5"
Q42. Write a PHP program to reverse an array without using PHP's built-in functions.
Input: Array = [1, 2, 3, 4, 5]
Expected Output: Reversed Array = [5, 4, 3, 2, 1]
Q43. Write a PHP program to find the smallest positive number missing from an unsorted array.
Input: Array = [3, 4, -1, 1]
Expected Output: "Smallest positive missing number is 2"
Q44. Write a PHP program to sort an array of numbers such that all negative numbers appear before positive numbers.
Input: Array = [-12, 11, -13, -5, 6, -7, 5, -3, -6]
Expected Output: [-12, -13, -5, -7, -3, -6, 11, 6, 5]
Q45. Write a PHP program to check if a number exists in a multidimensional array.
Input: Array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], Search Key = 5
Expected Output: "Element found"
Q46. Write a PHP program to count the number of elements greater than a given value in an array.
Input: Array = [10, 20, 30, 40, 50], Search Value = 25
Expected Output: "3 elements are greater than 25"
Q47. Write a PHP program to find the intersection of two sorted arrays.
Input: Array1 = [1, 3, 4, 5], Array2 = [3, 4, 6, 7]
Expected Output: Intersection = [3, 4]
Q48. Write a PHP program to find the union of two sorted arrays without duplicates.
Input: Array1 = [1, 2, 3], Array2 = [2, 3, 4]
Expected Output: Union = [1, 2, 3, 4]
Q49. Write a PHP program to merge two sorted arrays into one sorted array.
Input: Array1 = [1, 3, 5], Array2 = [2, 4, 6]
Expected Output: Merged Array = [1, 2, 3, 4, 5, 6]
Q50. Write a PHP program to implement a rotated binary search to find a number in a rotated sorted array.
Input: Array = [4, 5, 6, 7, 0, 1, 2], Search Key = 1
Expected Output: "Element found at index 5"
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!