Searching and sorting are two of the most fundamental concepts in computer science and programming. Almost every software application relies on these algorithms to organize and retrieve data efficiently.
Whether you’re developing a banking system, an e-commerce website, a student management system, or preparing for coding interviews, understanding searching and sorting algorithms is essential. C++ Searching and Sorting practice questions with solutions help to understand the concepts.
What is Searching?
Searching is the process of finding a specific element in a collection of data.
The two most common searching algorithms are:
- Linear Search
- Binary Search
Linear Search
Linear Search checks every element one by one until the required element is found.
Example:
10 20 30 40 50
↑
Time Complexity:
- Best Case → O(1)
- Worst Case → O(n)
Binary Search
Binary Search works only on sorted data.
It repeatedly divides the search space into two halves.
Example:
10 20 30 40 50 60 70
↑
Time Complexity:
- Best Case → O(1)
- Worst Case → O(log n)
What is Sorting?
Sorting arranges data in ascending or descending order.
Popular sorting algorithms include:
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
Why is Sorting Important?
Sorting improves:
- Searching Speed
- Data Organization
- Database Queries
- Report Generation
- Performance of Applications
Applications of Searching and Sorting
- Student Management Systems
- Banking Software
- E-commerce Websites
- Search Engines
- Inventory Management
- Employee Databases
- Data Analytics
In this chapter, you’ll solve practical searching and sorting problems using beginner-friendly C++ programs.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Output
- Explanation
- Concepts Covered
Let’s begin.
1. C++ Program to Perform Linear Search
Problem Statement
Write a C++ program to search an element using Linear Search.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {10,20,30,40,50};
int key = 30;
bool found = false;
for(int index = 0; index < 5; index++)
{
if(numbers[index] == key)
{
found = true;
break;
}
}
if(found)
{
cout << "Element Found";
}
else
{
cout << "Element Not Found";
}
return 0;
}
Sample Output
Element Found
Explanation
Linear Search checks every element until the required value is found.
Concepts Covered
- Linear Search
- Arrays
- Loop
2. C++ Program to Perform Binary Search
Problem Statement
Write a C++ program to search an element using Binary Search.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {10,20,30,40,50};
int key = 40;
int left = 0;
int right = 4;
while(left <= right)
{
int middle = (left + right) / 2;
if(numbers[middle] == key)
{
cout << "Element Found";
return 0;
}
if(numbers[middle] < key)
{
left = middle + 1;
}
else
{
right = middle - 1;
}
}
cout << "Element Not Found";
return 0;
}
Sample Output
Element Found
Explanation
Binary Search repeatedly divides the sorted array into two halves until the element is found.
Concepts Covered
- Binary Search
- Arrays
- Searching Algorithms
3. C++ Program to Sort an Array Using Bubble Sort
Problem Statement
Write a C++ program to sort an array using Bubble Sort.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {30,10,50,20,40};
for(int i=0;i<5;i++)
{
for(int j=0;j<4-i;j++)
{
if(numbers[j] > numbers[j+1])
{
swap(numbers[j],numbers[j+1]);
}
}
}
for(int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
Bubble Sort repeatedly swaps adjacent elements until the array becomes sorted.
Concepts Covered
- Bubble Sort
- Nested Loops
- Arrays
4. C++ Program to Sort an Array Using Selection Sort
Problem Statement
Write a C++ program to sort an array using Selection Sort.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {40,10,30,20,50};
for(int i=0;i<4;i++)
{
int minimum = i;
for(int j=i+1;j<5;j++)
{
if(numbers[j] < numbers[minimum])
{
minimum = j;
}
}
swap(numbers[i],numbers[minimum]);
}
for(int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
Selection Sort repeatedly selects the smallest element and places it in the correct position.
Concepts Covered
- Selection Sort
- Arrays
- Sorting
5. C++ Program to Sort an Array Using Insertion Sort
Problem Statement
Write a C++ program to sort an array using Insertion Sort.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {40,20,10,50,30};
for(int i=1;i<5;i++)
{
int key = numbers[i];
int j = i - 1;
while(j >= 0 && numbers[j] > key)
{
numbers[j+1] = numbers[j];
j--;
}
numbers[j+1] = key;
}
for(int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
Insertion Sort inserts each element into its correct position within the already sorted portion of the array.
Concepts Covered
- Insertion Sort
- Arrays
- Sorting Algorithms
C++ Program to Sort an Array Using Merge Sort
Problem Statement
Write a C++ program to sort an array using the Merge Sort algorithm.
C++ Solution
#include <iostream>
#include <algorithm>
using namespace std;
void mergeSort(int numbers[], int left, int right)
{
if (left >= right)
{
return;
}
int middle = (left + right) / 2;
mergeSort(numbers, left, middle);
mergeSort(numbers, middle + 1, right);
inplace_merge(numbers + left,
numbers + middle + 1,
numbers + right + 1);
}
int main()
{
int numbers[] = {40, 10, 50, 20, 30};
mergeSort(numbers, 0, 4);
for (int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
Merge Sort divides the array into smaller parts, sorts each part recursively, and merges them into a sorted array.
Concepts Covered
- Merge Sort
- Divide and Conquer
- Recursion
7. C++ Program to Sort an Array Using Quick Sort
Problem Statement
Write a C++ program to sort an array using the Quick Sort algorithm.
C++ Solution
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int numbers[] = {40, 20, 50, 10, 30};
sort(numbers, numbers + 5);
for (int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
The STL sort() function uses a highly optimized sorting algorithm (typically Introsort) that combines Quick Sort, Heap Sort, and Insertion Sort.
Concepts Covered
- Quick Sort
- STL sort()
- Efficient Sorting
8. C++ Program to Find the Largest Element in an Array
Problem Statement
Write a C++ program to find the largest element in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {25, 90, 35, 60, 15};
int largest = numbers[0];
for (int index = 1; index < 5; index++)
{
if (numbers[index] > largest)
{
largest = numbers[index];
}
}
cout << "Largest Element = "
<< largest;
return 0;
}
Sample Output
Largest Element = 90
Explanation
The program compares each element with the current largest value and updates it whenever a larger value is found.
Concepts Covered
- Arrays
- Searching
- Maximum Value
9. C++ Program to Find the Smallest Element in an Array
Problem Statement
Write a C++ program to find the smallest element in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {25, 90, 35, 60, 15};
int smallest = numbers[0];
for (int index = 1; index < 5; index++)
{
if (numbers[index] < smallest)
{
smallest = numbers[index];
}
}
cout << "Smallest Element = "
<< smallest;
return 0;
}
Sample Output
Smallest Element = 15
Explanation
The program checks every element and keeps updating the smallest value whenever a smaller element is encountered.
Concepts Covered
- Arrays
- Searching
- Minimum Value
10. C++ Program to Count Duplicate Elements in an Array
Problem Statement
Write a C++ program to count duplicate elements in an array.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {10, 20, 30, 20, 40, 10};
int duplicateCount = 0;
for (int i = 0; i < 6; i++)
{
for (int j = i + 1; j < 6; j++)
{
if (numbers[i] == numbers[j])
{
duplicateCount++;
break;
}
}
}
cout << "Duplicate Elements = "
<< duplicateCount;
return 0;
}
Sample Output
Duplicate Elements = 2
Explanation
The nested loops compare each element with the remaining elements in the array. When a duplicate is found, the counter is increased.
Concepts Covered
- Nested Loops
- Arrays
- Duplicate Detection
11. C++ Program to Find the Second Largest Element in an Array
Problem Statement
Write a C++ program to find the second largest element in an array.
C++ Solution
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int numbers[] = {45, 20, 90, 35, 75};
int largest = INT_MIN;
int secondLargest = INT_MIN;
for (int index = 0; index < 5; index++)
{
if (numbers[index] > largest)
{
secondLargest = largest;
largest = numbers[index];
}
else if (numbers[index] > secondLargest &&
numbers[index] != largest)
{
secondLargest = numbers[index];
}
}
cout << "Second Largest = "
<< secondLargest;
return 0;
}
Sample Output
Second Largest = 75
Explanation
The program keeps track of both the largest and second-largest values while traversing the array only once.
Concepts Covered
- Arrays
- Searching
- Second Largest Element
12. C++ Program to Find the Second Smallest Element in an Array
Problem Statement
Write a C++ program to find the second smallest element in an array.
C++ Solution
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int numbers[] = {45, 20, 90, 35, 10};
int smallest = INT_MAX;
int secondSmallest = INT_MAX;
for (int index = 0; index < 5; index++)
{
if (numbers[index] < smallest)
{
secondSmallest = smallest;
smallest = numbers[index];
}
else if (numbers[index] < secondSmallest &&
numbers[index] != smallest)
{
secondSmallest = numbers[index];
}
}
cout << "Second Smallest = "
<< secondSmallest;
return 0;
}
Sample Output
Second Smallest = 20
Explanation
The algorithm updates the smallest and second-smallest values in a single traversal of the array.
Concepts Covered
- Arrays
- Searching
- Second Smallest Element
13. C++ Program to Search an Element Using STL Binary Search
Problem Statement
Write a C++ program to search an element using the STL binary_search() function.
C++ Solution
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
if (binary_search(numbers, numbers + 5, 40))
{
cout << "Element Found";
}
else
{
cout << "Element Not Found";
}
return 0;
}
Sample Output
Element Found
Explanation
The STL binary_search() function performs efficient searching on sorted data.
Concepts Covered
- Binary Search
- STL Algorithm
- Searching
14. C++ Program to Sort an Array in Descending Order
Problem Statement
Write a C++ program to sort an array in descending order.
C++ Solution
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int numbers[] = {20, 50, 10, 40, 30};
sort(numbers, numbers + 5, greater<int>());
for (int number : numbers)
{
cout << number << " ";
}
return 0;
}
Sample Output
50 40 30 20 10
Explanation
The greater<int>() comparator sorts the array in descending order.
Concepts Covered
- sort()
- Descending Order
- STL Algorithms
15. C++ Program to Check Whether an Array is Sorted
Problem Statement
Write a C++ program to check whether an array is already sorted in ascending order.
C++ Solution
#include <iostream>
using namespace std;
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
bool sorted = true;
for (int index = 0; index < 4; index++)
{
if (numbers[index] > numbers[index + 1])
{
sorted = false;
break;
}
}
if (sorted)
{
cout << "Array is Sorted";
}
else
{
cout << "Array is Not Sorted";
}
return 0;
}
Sample Output
Array is Sorted
Explanation
The program compares each element with the next one. If any element is greater than its successor, the array is not sorted.
Concepts Covered
- Arrays
- Searching
- Sorting Verification
Chapter Summary
In this chapter, you learned the fundamentals of Searching and Sorting algorithms in C++. You practiced Linear Search, Binary Search, Bubble Sort, Selection Sort, Insertion Sort, Merge Sort concepts, Quick Sort using STL, and solved problems involving largest elements, smallest elements, duplicates, second largest values, and sorted array verification. These algorithms are essential for improving data organization and retrieval efficiency and are frequently asked in coding interviews.
Key Takeaways
- Searching algorithms help locate data efficiently.
- Linear Search works on both sorted and unsorted arrays.
- Binary Search requires a sorted array and is much faster than Linear Search.
- Bubble Sort repeatedly swaps adjacent elements until the array is sorted.
- Selection Sort repeatedly places the smallest element in its correct position.
- Insertion Sort inserts elements into the correct position one by one.
- Merge Sort uses the Divide and Conquer approach.
- STL
sort()provides highly optimized sorting. - Finding maximum, minimum, duplicates, and second largest elements are common interview problems.
- Searching and Sorting are core topics in competitive programming and technical interviews.
Frequently Asked Questions (FAQs)
1. What is the difference between Linear Search and Binary Search?
Linear Search checks each element one by one, while Binary Search repeatedly divides a sorted array into halves, making it much faster.
2. Which searching algorithm is faster?
Binary Search is faster than Linear Search, but it requires the data to be sorted.
3. What is the time complexity of Bubble Sort?
The worst-case time complexity of Bubble Sort is O(n²).
4. Which sorting algorithm is used by STL sort()?
The STL sort() function typically uses Introsort, which combines Quick Sort, Heap Sort, and Insertion Sort for high performance.
5. Why is Merge Sort considered efficient?
Merge Sort has a worst-case time complexity of O(n log n) and performs well on large datasets.
6. When should Binary Search be used?
Binary Search should only be used when the data is already sorted.
7. Which sorting algorithm is best for small datasets?
Insertion Sort performs efficiently on small or nearly sorted datasets because it minimizes unnecessary swaps.
8. Why are Searching and Sorting important?
Searching and Sorting improve data retrieval speed, organize information efficiently, and are fundamental algorithms used in databases, operating systems, software applications, and coding interviews.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
