Table of Contents
- Iterative Process in Hindi – इटरेटिव प्रोसेस क्या है?
- Recursive Process in Hindi – रिकर्सिव प्रोसेस क्या है?
- Types of Recursion in Hindi
- Examples of Iteration and Recursion in Hindi – इटरेशन और रिकर्शन के उदाहरण
- Iterative vs Recursive Process in Hindi – इटरेटिव और रिकर्सिव में अंतर
- Advantages and Disadvantages in Hindi – फायदे और नुकसान
- FAQs
Iterative Process in Hindi – इटरेटिव प्रोसेस क्या है?
Endless Horse – Find End? Play Game- Iteration वह process है जिसमें किसी काम या statements के block को loop की मदद से तब तक बार-बार दोहराया जाता है, जब तक condition सही रहती है।
- C में इसके लिए for, while और do-while loops का इस्तेमाल होता है।
- Iterative process में पूरा काम एक ही function call के अंदर होता है, इसलिए इसमें आमतौर पर extra memory की ज़रूरत नहीं पड़ती।
- इसमें हर बार loop की condition जाँची जाती है, और condition गलत होते ही process रुक जाता है।
- Condition कभी गलत न हो, तो loop कभी खत्म नहीं होता, जिसे infinite loop कहते हैं।
- Array की values पढ़ना, numbers का जोड़ निकालना और table print करना जैसे सीधे दोहराव वाले कामों में iteration का उपयोग होता है।
Parts of an Iterative Process
- Initialization: इसमें loop variable को शुरुआती value दी जाती है, जैसे i = 1। यह loop शुरू होने से पहले सिर्फ़ एक बार होता है।
- Condition: हर बार loop body चलने से पहले इसकी जाँच होती है, और यह सही हो तभी body चलती है।
- Update: हर बार body चलने के बाद loop variable की value बदली जाती है, जैसे i++, ताकि loop अंत में condition गलत होने तक पहुँच सके।
Recursive Process in Hindi – रिकर्सिव प्रोसेस क्या है?
- Recursion वह process है जिसमें कोई function अपने ही अंदर खुद को call करके एक बड़ी problem को उसी तरह की छोटी problem में बाँट देता है।
- हर recursive function में दो हिस्से होते हैं, base case और recursive case।
- Base case वह condition है जिस पर function खुद को call करना बंद कर देता है और सीधा result लौटा देता है।
- Recursive case में function ऐसे छोटे input के साथ खुद को call करता है, जो हर बार base case के और पास पहुँचता है।
- हर function call के लिए memory में call stack पर एक नया stack frame बनता है, जिसमें उस call के parameters, local variables और return address रहते हैं।
- Base case न हो या input base case की ओर न बढ़े, तो calls खत्म नहीं होतीं और stack की memory भर जाती है, जिसे stack overflow कहते हैं।
- Divide and conquer वाले algorithms, tree traversal और Tower of Hanoi जैसी problems में recursion का उपयोग होता है।
Parts of a Recursive Function
- Base Case: यह वह सबसे छोटी problem है जिसका हल सीधे पता होता है, जैसे factorial में n = 0 या n = 1 पर result 1 होना। इसके बिना recursion कभी नहीं रुकता।
- Recursive Case: इसमें function छोटे input के साथ खुद को call करता है और मिले result का इस्तेमाल करके अपना result बनाता है, जैसे n * factorial(n - 1)।
Working of Recursion
नीचे के चित्र में factorial(3) की working दिखाई गई है। Function पहले बार-बार खुद को call करता है और stack पर frames जमा होते जाते हैं, और base case मिलने के बाद result एक-एक करके वापस आते हैं।
- main() से fact(3) call होता है, और इसका frame stack पर बनता है।
- fact(3) अपना काम पूरा करने के लिए fact(2) को call करता है, और फिर fact(2) fact(1) को call करता है।
- fact(1) में base case मिलता है, इसलिए यह खुद को call किए बिना 1 लौटा देता है।
- अब fact(2) को 1 मिलता है और वह 2 × 1 = 2 लौटाता है।
- इसके बाद fact(3) को 2 मिलता है और वह 3 × 2 = 6 लौटाता है, और stack खाली हो जाता है।
Types of Recursion in Hindi
1. Direct Recursion
जब कोई function सीधे अपने ही अंदर खुद को call करता है, तो उसे Direct Recursion कहते हैं। Factorial का recursive function इसका सबसे सामान्य उदाहरण है।
int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1); // function खुद को call कर रहा है
}
2. Indirect Recursion
जब function A, function B को call करता है और function B वापस function A को call करता है, तो इसे Indirect Recursion कहते हैं। यहाँ function सीधे खुद को call नहीं करता, पर call का चक्र फिर भी बन जाता है।
void funA(int n);
void funB(int n);
void funA(int n) {
if (n > 0) {
printf("%d ", n);
funB(n - 1); // A, B को call करता है
}
}
void funB(int n) {
if (n > 0) {
printf("%d ", n);
funA(n - 1); // B, A को call करता है
}
}
3. Tail Recursion
जब recursive call function की आखिरी statement होती है और call से लौटने के बाद कोई काम बाकी नहीं रहता, तो उसे Tail Recursion कहते हैं। जिस recursion में call के बाद भी कोई काम बचता है, जैसे n * fact(n - 1) में गुणा, उसे Non-tail Recursion कहते हैं। कई compilers tail recursion को loop की तरह चलाकर stack की memory बचा लेते हैं।
int fact(int n, int result) {
if (n <= 1) return result;
return fact(n - 1, n * result); // call के बाद कोई काम बाकी नहीं
}
// पहली call: fact(5, 1)
Examples of Iteration and Recursion in Hindi – इटरेशन और रिकर्शन के उदाहरण
1. Factorial
मान लीजिए हमें n का factorial निकालना है, जहाँ n! = n × (n − 1) × ... × 1 होता है। नीचे यही काम पहले iteration से और फिर recursion से किया गया है।
Iterative तरीका:
#include
int factorial(int n) {
int result = 1;
int i;
for (i = 1; i <= n; i++) {
result = result * i; // हर बार result में i का गुणा
}
return result;
}
int main() {
printf("%d", factorial(5));
return 0;
}
Output:
120
Recursive तरीका:
#include
int factorial(int n) {
if (n <= 1) {
return 1; // base case
}
return n * factorial(n - 1); // recursive case
}
int main() {
printf("%d", factorial(5));
return 0;
}
Output:
120
2. Fibonacci Series
मान लीजिए हमें Fibonacci series का n-वाँ term निकालना है, जिसमें fib(0) = 0, fib(1) = 1 और बाकी terms अपने पिछले दो terms के जोड़ से बनते हैं। यहाँ हम fib(6) निकाल रहे हैं।
Iterative तरीका:
#include
int fib(int n) {
int a = 0, b = 1, next, i;
if (n == 0) return 0;
for (i = 2; i <= n; i++) {
next = a + b; // अगला term
a = b;
b = next;
}
return b;
}
int main() {
printf("%d", fib(6));
return 0;
}
Output:
8
Recursive तरीका:
#include
int fib(int n) {
if (n <= 1) {
return n; // base case: fib(0) = 0, fib(1) = 1
}
return fib(n - 1) + fib(n - 2); // दो recursive calls
}
int main() {
printf("%d", fib(6));
return 0;
}
Output:
8
Recursive fib में हर call दो और calls बनाती है, इसलिए एक ही value कई बार calculate होती है। चित्र में fib(4) का recursion tree दिखाया गया है, जिसमें fib(2) दो बार calculate हो रहा है।
Iterative vs Recursive Process in Hindi – इटरेटिव और रिकर्सिव में अंतर
| आधार | Iterative Process | Recursive Process |
|---|---|---|
| Basic Idea | इसमें loop की मदद से statements का block बार-बार चलाया जाता है। | इसमें function खुद को call करके problem को छोटी sub-problems में बाँट देता है। |
| Termination | इसमें loop की condition गलत होने पर process रुकता है। | इसमें base case मिलने पर process रुकता है। |
| Memory का उपयोग | इसमें आमतौर पर extra memory की ज़रूरत नहीं पड़ती। | इसमें हर call के लिए call stack पर एक नया frame बनता है, जिससे ज़्यादा memory लगती है। |
| Speed | इसमें function call का overhead नहीं होता, इसलिए यह आमतौर पर तेज़ चलता है। | इसमें हर call पर overhead होता है, इसलिए यह आमतौर पर थोड़ा धीमा चलता है। |
| Code Size | Tree जैसी problems में इसका code लंबा और समझने में मुश्किल हो सकता है। | ऐसी problems का code इसमें छोटा और साफ़ लिखा जाता है। |
| Infinite Case | Condition कभी गलत न हो, तो इसमें infinite loop बन जाता है। | Base case न मिले, तो इसमें stack overflow हो जाता है। |
| उपयोग | इसका उपयोग array traverse करने जैसे सीधे दोहराव वाले कामों में होता है। | इसका उपयोग tree traversal, Tower of Hanoi और merge sort जैसे divide and conquer algorithms में होता है। |
Advantages and Disadvantages in Hindi – फायदे और नुकसान
Advantages of Recursion
- Recursive code छोटा और साफ़ होता है।
- Tree traversal, Tower of Hanoi और divide and conquer जैसी problems का हल आसानी से लिखा जा सकता है।
- Problem को छोटी sub-problems में बाँटने से उसका logic समझना आसान हो जाता है।
- Backtracking जैसी problems में पिछली state पर लौटना आसान होता है, क्योंकि call stack खुद state सँभाल लेता है।
Disadvantages of Recursion
- हर call के लिए stack memory लगती है, इसलिए यह ज़्यादा memory इस्तेमाल करता है।
- Function call के overhead की वजह से यह iteration से धीमा हो सकता है।
- Base case गलत हो या न हो, तो stack overflow हो जाता है।
- Fibonacci जैसी problems में एक ही sub-problem कई बार हल होता है, जिससे time बहुत बढ़ जाता है।
- बहुत गहरी recursion को debug करना मुश्किल होता है।
Advantages of Iteration
- Function call का overhead न होने से यह आमतौर पर तेज़ चलता है।
- इसमें आमतौर पर extra memory की ज़रूरत नहीं पड़ती।
- इसमें stack overflow का खतरा नहीं होता।
- Loop का flow सीधा होता है, इसलिए उसे trace करना आसान होता है।
Disadvantages of Iteration
- Tree traversal जैसी problems में इसका code लंबा और complex हो जाता है।
- Condition या update गलत हो, तो infinite loop बन जाता है।
- Nested loops ज़्यादा होने पर code समझना मुश्किल हो जाता है।
- जिन problems की बनावट अपने आप recursive होती है, उनमें यह हल को साफ़ तरीके से नहीं दिखा पाता।
