Practice Top 30 C++ Programming Questions on Sorting and Searching


Q1. Write a program to sort an array of integers in ascending order using the bubble sort algorithm.

Input:  
5 3 8 6 2  

Expected Output:  
Sorted array: 2 3 5 6 8

Q2. Write a program to search for a given element in an array using linear search.

Input:  
Array: 7 14 21 28 35  
Element to search: 21  

Expected Output:  
21 found at index 2.

Q3. Write a program to sort an array of integers in descending order using the selection sort algorithm.

Input:  
4 1 7 3  

Expected Output:  
Sorted array: 7 4 3 1

Q4. Write a program to find the maximum element in an array.

Input:  
12 45 23 67 34  

Expected Output:  
Maximum element: 67

Q5. Write a program to search for an element in a sorted array using binary search.

Input:  
Array: 10 20 30 40 50  
Element to search: 30  

Expected Output:  
30 found at index 2.

Q6. Write a program to find the minimum element in an array.

Input:  
34 11 78 56 90  

Expected Output:  
Minimum element: 11

Q7. Write a program to sort an array of integers in ascending order using the insertion sort algorithm.

Input:  
9 3 1 6 7  

Expected Output:  
Sorted array: 1 3 6 7 9

Q8. Write a program to find the index of the maximum element in an array.

Input:  
8 22 19 34 15  

Expected Output:  
Index of maximum element: 3

Q9. Write a program to sort an array of integers in ascending order using the merge sort algorithm.

Input:  
20 10 30 50 40  

Expected Output:  
Sorted array: 10 20 30 40 50

Q10. Write a program to search for an element in a sorted array using the jump search technique.

Input:  
Array: 2 4 6 8 10 12 14  
Element to search: 10  

Expected Output:  
10 found at index 4.

Q11. Write a program to find all the occurrences of a given element in an array.

Input:  
Array: 5 3 7 5 9 5  
Element to search: 5  

Expected Output:  
5 found at indices: 0 3 5

Q12. Write a program to sort an array of integers in ascending order using the quick sort algorithm.

Input:  
29 12 42 8 23  

Expected Output:  
Sorted array: 8 12 23 29 42

Q13. Write a program to count the frequency of each element in an array.

Input:  
3 1 2 3 4 3  

Expected Output:  
Element 1: 1 time  
Element 2: 1 time  
Element 3: 3 times  
Element 4: 1 time

Q14. Write a program to find the second largest element in an array.

Input:  
6 13 22 14 10  

Expected Output:  
Second largest element: 14

Q15. Write a program to handle cases where an element is not found using binary search.

Input:  
Array: 1 3 5 7 9  
Element to search: 4  

Expected Output:  
4 not found in the array.

Q16. Write a program to sort an array of integers in ascending order using heap sort.

Input:  
15 6 9 12 5  

Expected Output:  
Sorted array: 5 6 9 12 15

Q17. Write a program to find the median of a sorted array.

Input:  
2 6 9 15 21  

Expected Output:  
Median: 9

Q18. Write a program to sort an array of integers using shell sort.

Input:  
50 20 30 10 40  

Expected Output:  
Sorted array: 10 20 30 40 50

Q19. Write a program to count the number of comparisons made during a bubble sort.

Input:  
8 4 1 6 3  

Expected Output:  
Sorted array: 1 3 4 6 8  
Number of comparisons: 10

Q20. Write a recursive program to search for an element in a sorted array using binary search.

Input:  
Array: 5 10 15 20 25  
Element to search: 15  

Expected Output:  
15 found at index 2.

Q21. Write a program to sort an array and remove duplicate elements.

Input:  
4 2 7 2 8 4  

Expected Output:  
Sorted array without duplicates: 2 4 7 8

Q22. Write a 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: 7

Q23. Write a program to find the closest element to a given number in a sorted array.

Input:  
Array: 1 4 6 8 10  
Number: 7  

Expected Output:  
Closest element: 6

Q24. Write a program to count the total occurrences of a given element in a sorted array using binary search.

Input:  
Array: 2 4 4 4 6 8  
Element to search: 4  

Expected Output:  
Occurrences of 4: 3

Q25. Write a program to find the missing number in a sequence of 1 to N.

Input:  
Array: 1 2 4 5 6  

Expected Output:  
Missing number: 3

Q26. Write a program to sort an array using the C++ STL sort() function.

Input:  
9 5 2 8 4  

Expected Output:  
Sorted array: 2 4 5 8 9

Q27. Write a program to search for an element in an array using the find() function from C++ STL.

Input:  
Array: 12 34 56 78  
Element to search: 34  

Expected Output:  
34 found in the array.

Q28. Write a program to find a peak element in an array.  

Input:  
1 3 20 4 1  

Expected Output:  
Peak element: 20

Q29. Write a program to sort an array in descending order using the C++ STL sort() function.

Input:  
11 7 15 3  

Expected Output:  
Sorted array: 15 11 7 3

Q30. Write a program to search for an element in a sorted array using interpolation search.

Input:  
Array: 10 20 30 40 50  
Element to search: 40  

Expected Output:  
40 found at index 3.

Social Share

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!

Programming Languages