0% found this document useful (0 votes)
3 views10 pages

Searching and Sorting

The document discusses searching and sorting algorithms in C++, focusing on binary search and its implementation. It explains how binary search works by dividing the search space and provides a code example. Additionally, it covers sorting arrays in C++ and includes a code snippet for sorting elements in ascending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Searching and Sorting

The document discusses searching and sorting algorithms in C++, focusing on binary search and its implementation. It explains how binary search works by dividing the search space and provides a code example. Additionally, it covers sorting arrays in C++ and includes a code snippet for sorting elements in ascending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C++

Searching and sorting

gaurav singh
Searching
Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and
returns an iterator to its first element, or last1 if no occurrences are found.
Binary Search
Binary Search is defined as a searching algorithm used in a
sorted array by repeatedly dividing the search interval in half.
How does it works?
Divide the search space into two halves by finding the middle index “mid”.
Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.

First Step: Calculate the mid and compare the mid element with the key. If the key is less than mid element,
move to left and if it is greater than the mid then move search space to the right.

Key (i.e., 23) is greater than current mid element (i.e., 16). The search space moves to the right.
Key is less than the current mid 56. The search space moves to the left.
Second Step: If the key matches the value of the mid element, the element is
found and stop search.
Implementation in C++
#include <iostream>
#include <algorithm>
using namespace std;

int main() { Initializing the array with the elements and the value is
int arr[] = {1, 2, 3, 4, 5}; give which we have to search.
int value = 3;

if (binary_search(begin(arr),end(arr), value)) { begin(arr): starting of the array


cout << "Value found" <<endl; end(arr): end of the array
} else { value: which we have to find.
cout << "Value not found" << endl;
}

return 0;
}
Sorting
Sorting in C++ is a concept in which the elements of an array are rearranged in a logical order. This order can be from
lowest to highest or highest to lowest. Sorting an unsorted array helps to solve many problems such as searching for the
minimum or maximum element, etc.

Arranging things in a sorted manner makes it easier to analyze and search for a particular element among the collection
of elements.

For example, In the below example, we are sorting or arranging the elements of an unsorted array in ascending order, i.e.,
from lowest to highest.
Implementation in C++
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
For sorting we need to declare like:
sort(arr, arr + 6);
sort(arr,arr+6)
for (int i = 0; i < 6; ++i) {
cout << arr[i] << " ";
} Begning of Upto where you
the array. ending of have to sort the
return 0; the array. array.
}

You might also like