C++ STL Iterators and Algorithms: sort, lower_bound, binary_search Notes in Hindi & English : C++ STL इटरेटर्स और एल्गोरिदम: sort, lower_bound, binary_search और 20 महत्वपूर्ण MCQ प्रश्न (India IT Exam)

Post a Comment

UGC-NET, GATE, NIELIT (O/A/B Level), KVS/NVS Computer Teacher, PGDCA, BCA, MCA, B-Tech व All India IT Exam विशेष नोट्स: इस अध्याय में हम C++ STL Iterators and Algorithms: sort, lower_bound, binary_search (C++ STL इटरेटर्स और एल्गोरिदम: sort, lower_bound, binary_search) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और राष्ट्रीय स्तर की परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: C++ STL (Standard Template Library) Iterators act as generalized pointers that decouple containers from algorithms. Core algorithms in <algorithm> such as sort(), lower_bound(), and binary_search() operate on iterator ranges [first, last) to deliver O(N log N) sorting and O(log N) searching.

अध्याय सार: C++ STL (स्टैंडर्ड टेम्पलेट लाइब्रेरी) इटरेटर्स सामान्यीकृत पॉइंटर्स के रूप में कार्य करते हैं जो कंटेनरों को एल्गोरिदम से अलग करते हैं। <algorithm> में sort(), lower_bound(), और binary_search() जैसे मुख्य एल्गोरिदम [first, last) इटेरेटर रेंज पर काम करते हैं।

C++ STL Iterators and Algorithms: sort, lower_bound, binary_search: Detailed Notes & Architecture (C++ STL इटरेटर्स और एल्गोरिदम: sort, lower_bound, binary_search)

1. STL Iterator Categories (STL इटेरेटर श्रेणियां)

[EN] C++ defines five main iterator categories: Input, Output, Forward (forward_list), Bidirectional (list, set, map), and Random Access (vector, deque, array). Random Access iterators support pointer arithmetic like it + 5 and it1 < it2 in O(1) time, which is required by std::sort().

[HI] C++ पांच मुख्य इटेरेटर श्रेणियां परिभाषित करता है: इनपुट, आउटपुट, फॉरवर्ड, बायडायरेक्शनल (list, set, map), और रैंडम एक्सेस (vector, deque, array)। रैंडम एक्सेस इटरेटर्स O(1) समय में पॉइंटर अंकगणित का समर्थन करते हैं।

2. std::sort() and Custom Comparators (std::sort() और कस्टम कम्पेरेटर्स)

[EN] std::sort(first, last) uses Introsort (a hybrid of Quicksort, Heapsort, and Insertion Sort) to guarantee O(N log N) worst-case time complexity. It requires Random Access iterators, meaning it works on std::vector but not std::list (which has its own list::sort()). You can pass a lambda or std::greater<int>() for descending order.

[HI] std::sort(first, last) O(N log N) वर्स्ट-केस समय जटिलता की गारंटी के लिए इंट्रोसॉर्ट का उपयोग करता है। इसके लिए रैंडम एक्सेस इटरेटर्स की आवश्यकता होती है। अवरोही क्रम के लिए आप लैम्ब्डा या std::greater<int>() पास कर सकते हैं।

3. std::lower_bound() and std::upper_bound() (std::lower_bound() और std::upper_bound())

[EN] On a sorted range, std::lower_bound(first, last, val) returns an iterator to the first element >= val in O(log N) comparisons. std::upper_bound() returns an iterator to the first element > val. Subtracting the two iterators (upper_bound - lower_bound) gives the exact frequency of val in a sorted vector.

[HI] सॉर्ट की गई रेंज पर, std::lower_bound(first, last, val) O(log N) तुलनाओं में >= val वाले पहले तत्व का इटेरेटर लौटाता है। std::upper_bound() > val वाले पहले तत्व का इटेरेटर लौटाता है।

4. std::binary_search() Usage (std::binary_search() का उपयोग)

[EN] std::binary_search(first, last, val) returns a boolean (true or false) indicating whether val exists in the sorted range [first, last). Unlike lower_bound(), it does not return the position of the element. Note that calling binary_search on an unsorted range results in undefined behavior.

[HI] std::binary_search(first, last, val) एक बूलियन (true या false) लौटाता है जो बताता है कि क्या val सॉर्ट की गई रेंज [first, last) में मौजूद है। lower_bound() के विपरीत, यह तत्व की स्थिति नहीं लौटाता है।

Syntax, Formula & Technical Example (C++ STL Iterators and Algorithms: sort, lower_bound, binary_search)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    vector<int> v = {40, 10, 20, 20, 30};
    sort(v.begin(), v.end()); // {10, 20, 20, 30, 40}
    bool found = binary_search(v.begin(), v.end(), 20); // true
    auto it = lower_bound(v.begin(), v.end(), 20);      // points to index 1
    cout << found << " idx=" << (it - v.begin()) << endl;
}

C++ STL Iterators and Algorithms: sort, lower_bound, binary_search Quick Reference & Comparison Table

STL AlgorithmReturn TypeKey Exam Fact
std::sortvoidUses Introsort; O(N log N) worst-case; needs Random Access
std::lower_boundIteratorFirst element >= target in O(log N)
std::upper_boundIterator (first > val)First element > target in O(log N)
std::binary_searchboolReturns true/false if target exists in sorted range

C++ STL Iterators and Algorithms: sort, lower_bound, binary_search Top 20 Bilingual MCQs with Answers

Q1. Which header file must be included to use std::sort, std::lower_bound, and std::binary_search?

(std::sort, std::lower_bound, और std::binary_search का उपयोग करने के लिए कौन सी हेडर फ़ाइल शामिल करनी होगी?)

(A) <algorithm>

(B) <vector>

(C) <iterator>

(D) <numeric>

Correct Answer: (A)

[EN] All standard sorting and searching algorithms live in <algorithm>.

[HI] सभी मानक सॉर्टिंग और सर्चिंग एल्गोरिदम <algorithm> में होते हैं।

Q2. What is the worst-case time complexity of std::sort() in C++?

(C++ में std::sort() की वर्स्ट-केस समय जटिलता क्या है?)

(A) O(N)

(B) O(log N)

(C) O(N^2)

(D) O(N log N)

Correct Answer: (D)

[EN] std::sort uses Introsort, which switches to Heapsort if Quicksort recursion gets too deep.

[HI] std::sort इंट्रोसॉर्ट का उपयोग करता है, जो O(N log N) की गारंटी देता है।

Q3. What does std::lower_bound(v.begin(), v.end(), x) return?

(std::lower_bound(v.begin(), v.end(), x) क्या लौटाता है?)

(A) Boolean true or false

(B) Index of the last element < x

(C) Iterator to the first element >= x

(D) Iterator to the first element > x

Correct Answer: (C)

[EN] lower_bound finds the first position where x could be inserted without breaking order.

[HI] lower_bound पहला तत्व >= x ढूंढता है।

Q4. What does std::binary_search(v.begin(), v.end(), x) return?

(std::binary_search(v.begin(), v.end(), x) क्या लौटाता है?)

(A) Integer index of x

(B) Pointer to x

(C) Iterator to x

(D) bool (true or false)

Correct Answer: (D)

[EN] binary_search only checks existence and returns a boolean.

[HI] binary_search केवल अस्तित्व की जांच करता है और बूलियन लौटाता है।

Q5. Which iterator category is required by std::sort()?

(std::sort() के लिए किस इटेरेटर श्रेणी की आवश्यकता होती है?)

(A) Random Access Iterator

(B) Bidirectional Iterator

(C) Forward Iterator

(D) Input Iterator

Correct Answer: (A)

[EN] std::sort needs O(1) jump access, so it cannot be used on std::list.

[HI] std::sort को O(1) जंप एक्सेस की आवश्यकता होती है।

Q6. Which sorting algorithm is used internally by std::sort() in modern C++?

(आधुनिक C++ में std::sort() द्वारा आंतरिक रूप से किस सॉर्टिंग एल्गोरिदम का उपयोग किया जाता है?)

(A) Bubble Sort

(B) Introsort

(C) Radix Sort

(D) Merge Sort

Correct Answer: (B)

[EN] Introsort combines Quicksort, Heapsort, and Insertion Sort.

[HI] इंट्रोसॉर्ट क्विकसॉर्ट, हीपसॉर्ट और इंसर्शन सॉर्ट को जोड़ता है।

Q7. How do you sort a std::vector<int> in descending order using std::sort?

(std::sort का उपयोग करके std::vector<int> को अवरोही क्रम में कैसे सॉर्ट करें?)

(A) sort(v.begin(), v.end(), -1)

(B) sort(v.begin(), v.end(), greater<int>())

(C) rsort(v.begin(), v.end())

(D) sort(v.end(), v.begin())

Correct Answer: (B)

[EN] Passing greater<int>() or a custom comparator reverses the sort order.

[HI] greater<int>() पास करने से सॉर्ट क्रम उलट जाता है।

Q8. What happens if you call std::binary_search() on an unsorted vector?

(यदि आप अनसॉर्टेड वेक्टर पर std::binary_search() कॉल करते हैं तो क्या होता है?)

(A) It sorts the vector automatically

(B) Undefined behavior (incorrect result)

(C) Throws std::runtime_error

(D) Compile-time error

Correct Answer: (B)

[EN] Binary search requires the input range to be partitioned/sorted.

[HI] बाइनरी सर्च के लिए इनपुट रेंज का सॉर्ट होना आवश्यक है।

Q9. How can you count the occurrences of x in a sorted vector v using STL?

(STL का उपयोग करके सॉर्टेड वेक्टर v में x की घटनाओं की गिनती कैसे करें?)

(A) binary_search(v.begin(), v.end(), x)

(B) v.size() - x

(C) sort(v.begin(), v.end())

(D) upper_bound(v.begin(), v.end(), x) - lower_bound(v.begin(), v.end(), x)

Correct Answer: (D)

[EN] The distance between upper_bound and lower_bound equals the count of x.

[HI] upper_bound और lower_bound के बीच की दूरी x की गिनती के बराबर होती है।

Q10. Which STL container does NOT support Random Access iterators?

(कौन सा STL कंटेनर रैंडम एक्सेस इटरेटर्स का समर्थन नहीं करता है?)

(A) std::array

(B) std::list

(C) std::deque

(D) std::vector

Correct Answer: (B)

[EN] std::list is a doubly-linked list supporting only Bidirectional iterators.

[HI] std::list केवल बायडायरेक्शनल इटरेटर्स का समर्थन करता है।

Q11. What does v.end() point to in C++ STL?

(C++ STL में v.end() किसकी ओर इशारा करता है?)

(A) The last element

(B) One past the last element

(C) NULL

(D) The first element

Correct Answer: (B)

[EN] STL ranges are half-open [begin, end).

[HI] STL रेंज हाफ-ओपन [begin, end) होती हैं।

Q12. What is the time complexity of std::lower_bound() on a std::vector?

(std::vector पर std::lower_bound() की समय जटिलता क्या है?)

(A) O(N log N)

(B) O(1)

(C) O(N)

(D) O(log N)

Correct Answer: (D)

[EN] Random Access iterators allow binary search in O(log N) steps.

[HI] रैंडम एक्सेस इटरेटर्स O(log N) चरणों में बाइनरी सर्च की अनुमति देते हैं।

Q13. Which STL algorithm preserves the relative order of equal elements during sorting?

(सॉर्टिंग के दौरान कौन सा STL एल्गोरिदम समान तत्वों के सापेक्ष क्रम को बनाए रखता है?)

(A) std::sort

(B) std::partial_sort

(C) std::nth_element

(D) std::stable_sort

Correct Answer: (D)

[EN] std::stable_sort uses Merge Sort internally to maintain stability.

[HI] std::stable_sort स्थिरता बनाए रखने के लिए मर्ज सॉर्ट का उपयोग करता है।

Q14. What does std::upper_bound(v.begin(), v.end(), x) return if all elements are <= x?

(यदि सभी तत्व <= x हैं तो std::upper_bound(v.begin(), v.end(), x) क्या लौटाता है?)

(A) v.end()

(B) v.begin()

(C) -1

(D) nullptr

Correct Answer: (A)

[EN] When no element is > x, upper_bound returns the end iterator.

[HI] जब कोई तत्व > x नहीं होता है, तो upper_bound एंड इटेरेटर लौटाता है।

Q15. Which keyword in modern C++ (C++11+) simplifies declaring iterator types?

(आधुनिक C++ (C++11+) में कौन सा कीवर्ड इटेरेटर प्रकार घोषित करने को सरल बनाता है?)

(A) let

(B) typedef

(C) auto

(D) var

Correct Answer: (C)

[EN] auto deduces complex iterator types like vector<int>::iterator automatically.

[HI] auto स्वचालित रूप से जटिल इटेरेटर प्रकारों का अनुमान लगाता है।

Q16. Which std::list member function is used to sort a list?

(लिस्ट को सॉर्ट करने के लिए किस std::list मेंबर फ़ंक्शन का उपयोग किया जाता है?)

(A) std::qsort(l)

(B) l.order()

(C) std::sort(l.begin(), l.end())

(D) l.sort()

Correct Answer: (D)

[EN] std::list provides its own member function sort() using Merge Sort.

[HI] std::list मर्ज सॉर्ट का उपयोग करके अपना स्वयं का मेंबर फ़ंक्शन sort() प्रदान करता है।

Q17. Within C++ STL Iterators and Algorithms: sort, lower_bound, binary_search, which stl algorithm directly maps to: "void"?

(C++ STL Iterators and Algorithms: sort, lower_bound, binary_search में कौन सा STL Algorithm सीधे "void" से संबंधित है?)

(A) std::binary_search

(B) std::upper_bound

(C) std::sort

(D) std::lower_bound

Correct Answer: (C)

[EN] In C++ STL Iterators and Algorithms: sort, lower_bound, binary_search, 'std::sort' handles 'void' (Uses Introsort; O(N log N) worst-case; needs Random Access).

[HI] C++ STL Iterators and Algorithms: sort, lower_bound, binary_search में 'std::sort' का कार्य/विवरण 'void' (Uses Introsort; O(N log N) worst-case; needs Random Access) है।

Q18. Which option accurately describes 'std::lower_bound' in C++ STL Iterators and Algorithms: sort, lower_bound, binary_search?

(C++ STL Iterators and Algorithms: sort, lower_bound, binary_search के संदर्भ में 'std::lower_bound' की सही विशेषता कौन सी है?)

(A) Iterator (first > val)

(B) bool

(C) void

(D) Iterator

Correct Answer: (D)

[EN] 'std::lower_bound' provides 'Iterator' — key point: First element >= target in O(log N).

[HI] 'std::lower_bound' का मुख्य विवरण 'Iterator' है (First element >= target in O(log N))।

Q19. In C++ STL Iterators and Algorithms: sort, lower_bound, binary_search, identify the correct parameter for the specification: "Iterator (first > val)".

(C++ STL Iterators and Algorithms: sort, lower_bound, binary_search में "Iterator (first > val)" विवरण किस विकल्प के लिए सही है?)

(A) std::upper_bound

(B) std::binary_search

(C) std::lower_bound

(D) std::sort

Correct Answer: (A)

[EN] 'std::upper_bound' corresponds to 'Iterator (first > val)' (First element > target in O(log N)).

[HI] 'std::upper_bound' का संबंध 'Iterator (first > val)' (First element > target in O(log N)) से है।

Q20. Considering C++ STL Iterators and Algorithms: sort, lower_bound, binary_search, what key behavior defines 'std::binary_search'?

(C++ STL Iterators and Algorithms: sort, lower_bound, binary_search में 'std::binary_search' की मुख्य भूमिका या मान क्या है?)

(A) Iterator (first > val)

(B) Iterator

(C) bool

(D) void

Correct Answer: (C)

[EN] 'std::binary_search' represents 'bool' (Returns true/false if target exists in sorted range).

[HI] 'std::binary_search' का अर्थ/मान 'bool' है (Returns true/false if target exists in sorted range)।

C++ STL Iterators and Algorithms: sort, lower_bound, binary_search — Exam FAQs

Q1: Why does std::sort not work on std::list?

std::list पर std::sort काम क्यों नहीं करता?

[EN] std::sort requires Random Access iterators for O(1) indexing, whereas std::list only provides Bidirectional iterators; use l.sort() instead.

[HI] std::sort को O(1) इंडेक्सिंग के लिए रैंडम एक्सेस इटरेटर्स की आवश्यकता होती है, जबकि std::list केवल बायडायरेक्शनल इटरेटर्स प्रदान करता है।

Q2: When should I use lower_bound instead of binary_search?

मुझे binary_search के बजाय lower_bound का उपयोग कब करना चाहिए?

[EN] Use lower_bound when you need the actual position/index of the element; use binary_search when you only need a true/false check.

[HI] जब आपको तत्व की वास्तविक स्थिति/इंडेक्स की आवश्यकता हो तो lower_bound का उपयोग करें; जब केवल true/false की आवश्यकता हो तो binary_search का उपयोग करें।

Q3: What is the difference between v.begin() and v.cbegin()?

v.begin() और v.cbegin() में क्या अंतर है?

[EN] cbegin() returns a const_iterator, preventing modification of elements through the iterator.

[HI] cbegin() एक const_iterator लौटाता है, जो इटेरेटर के माध्यम से तत्वों के संशोधन को रोकता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)