• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
May 11, 2022 |180.5K Views

Searching & Sorting in an Array using STL

  Share   Like
Description
Discussion

Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted.
It works by comparing the middle item of the array with our target, if it matches, it returns true otherwise if the middle term is greater than the target, the search is performed in the left sub-array. 
If the middle term is less than the target, the search is performed in the right sub-array.

We have discussed qsort() in C. C++ STL provides a similar function sort that sorts a vector or array (items with random access)

It generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin and the second parameter being the length up to which we want the array/vector to get sorted. The third parameter is optional and can be used in cases such as if we want to sort the elements lexicographically.

By default, the sort() function sorts the elements in ascending order.

Searching  in an Array using STL:https://www.geeksforgeeks.org/binary-search-algorithms-the-c-standard-template-library-stl/

Sorting in an Array using STL: https://www.geeksforgeeks.org/sort-c-stl/