Wednesday, October 29, 2025

Searching and Sorting (In Hindi)

Sorting  

Sorting (सॉर्टिंग) का अर्थ है डेटा को एक निश्चित क्रम (Order) में व्यवस्थित करना — जैसे संख्याओं के लिए Ascending (छोटे से बड़े) या Descending (बड़े से छोटे) क्रम में, और अक्षरों के लिए Ascending (A से Z) या Descending (Z से A) क्रम में।

C भाषा में दो सामान्य और सरल सॉर्टिंग विधियाँ होती हैं:

  1. Bubble Sort (बबल सॉर्ट)

  2. Selection Sort (सेलेक्शन सॉर्ट)


1. Bubble Sort (बबल सॉर्ट)

Bubble Sort में हम Array के आस-पास (adjacent) तत्वों की तुलना करते हैं।

  • यदि दो आस-पास के तत्व गलत क्रम में हैं, तो उनकी स्वैपिंग (स्थान परिवर्तन) की जाती है।

  • पहले पास (Pass) के बाद, सबसे बड़ा तत्व “बबल की तरह” ऊपर यानी अंत में पहुँच जाता है।

  • हर पास के बाद तुलना (comparison) की संख्या कम होती जाती है क्योंकि अंतिम तत्व पहले से सही जगह पर होते हैं।


Algorithm (कदम दर कदम प्रक्रिया)

मान लीजिए Array में n तत्व हैं:
arr[0], arr[1], ..., arr[n-1]

Algorithm:

Step 1: Start Step 2: Input the number of elements (n) and the array elements. Step 3: Repeat steps 46 for i = 0 to n-2 Step 4: Repeat for j = 0 to n-i-2 Step 5: If arr[j] > arr[j+1] Swap arr[j] and arr[j+1] Step 6: End inner loop Step 7: End outer loop Step 8: Display the sorted array Step 9: Stop

Example: Bubble Sort

Array: [5, 3, 4, 1, 2]

हम इन संख्याओं को ascending order में व्यवस्थित करना चाहते हैं।

Pass 1:
5 और 3 की तुलना → 5 बड़ा है → Swap → [3, 5, 4, 1, 2]
5 और 4 → Swap → [3, 4, 5, 1, 2]
5 और 1 → Swap → [3, 4, 1, 5, 2]
5 और 2 → Swap → [3, 4, 1, 2, 5]

सबसे बड़ा तत्व (5) अब अंत में पहुँच गया।

Pass 2:
[3, 4, 1, 2] पर दोबारा प्रक्रिया।
4 और 1 → Swap → [3, 1, 4, 2, 5]
4 और 2 → Swap → [3, 1, 2, 4, 5]

Pass 3:
[3, 1, 2]
3 और 1 → Swap → [1, 3, 2, 4, 5]
3 और 2 → Swap → [1, 2, 3, 4, 5]

अब Array पूर्ण रूप से sort हो गया है।

Final Sorted Array: [1, 2, 3, 4, 5]


C Program for Bubble Sort

#include <stdio.h> int main() { int A[10]; int x, y, t; printf("Enter 10 elements: "); for(x = 0; x < 10; x++) scanf("%d", &A[x]); for(x = 0; x < 10-1; x++) { for(y = 0; y < 10-x-1; y++) { if(A[y] > A[y+1]) { t = A[y]; A[y] = A[y+1]; A[y+1] = t; } } } printf("Sorted array in ascending order:\n"); for(x = 0; x < 10; x++) printf("%d ", A[x]); return 0; }

2. Selection Sort (सेलेक्शन सॉर्ट)

Selection Sort में हम Array को दो भागों में बाँटते हैं:

  • Sorted भाग (बाएँ ओर)

  • Unsorted भाग (दाएँ ओर)

हर बार Unsorted भाग में से सबसे छोटा तत्व (minimum element) ढूँढकर उसे सही स्थान पर स्वैप कर देते हैं।
हर पास के बाद Sorted भाग एक तत्व बढ़ जाता है।


Algorithm (कदम दर कदम प्रक्रिया)

Step 1: Start Step 2: Input number of elements (n) and array elements. Step 3: Repeat for i = 0 to n-2 Step 4: min = i Step 5: Repeat for j = i+1 to n-1 Step 6: If arr[j] < arr[min] min = j Step 7: Swap arr[i] and arr[min] Step 8: Display the sorted array Step 9: Stop

Example: Selection Sort

Array = [5, 3, 4, 1, 2]

PassSmallest ElementArray After Pass
11 → swap with 5[1, 3, 4, 5, 2]
22 → swap with 3[1, 2, 4, 5, 3]
33 → swap with 4[1, 2, 3, 5, 4]
44 → swap with 5[1, 2, 3, 4, 5]

Final Sorted Array: [1, 2, 3, 4, 5]


C Program for Selection Sort

#include <stdio.h> int main() { int arr[100], n, i, j, min, temp; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for(i = 0; i < n; i++) scanf("%d", &arr[i]); for(i = 0; i < n-1; i++) { min = i; for(j = i+1; j < n; j++) { if(arr[j] < arr[min]) min = j; } temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } printf("Sorted array in ascending order:\n"); for(i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }

Difference Between Bubble Sort and Selection Sort (बबल और सेलेक्शन सॉर्ट में अंतर)

विशेषता (Feature)Bubble SortSelection Sort
कार्य करने का सिद्धांतआस-पास के तत्वों की बार-बार तुलना और स्वैपिंगसबसे छोटे तत्व को खोजकर सही स्थान पर स्वैप करना
स्वैप की संख्याप्रत्येक पास में कई स्वैपप्रत्येक पास में केवल एक स्वैप
दक्षता (Efficiency)बड़े डेटा पर कम दक्षबबल सॉर्ट से थोड़ी अधिक दक्ष
समय जटिलता (Time Complexity)O(n) / O(n²)सभी स्थितियों में O(n²)
एडाप्टिव नेचरएडाप्टिव (पहले से सॉर्टेड होने पर जल्दी समाप्त हो सकता है)नॉन-एडाप्टिव
स्थिरता (Stability)स्थिर (समान तत्वों का क्रम नहीं बदलता)अस्थिर (क्रम बदल सकता है)
उपयोगछोटे डेटा या सीखने के लिएजब स्वैप कम करने हों

Searching 

Searching का अर्थ है किसी सूची (List) या Array में एक विशिष्ट तत्व (key) का स्थान (position या index) खोजना।

C भाषा में दो प्रमुख खोज विधियाँ होती हैं:

  1. Sequential (Linear) Search

  2. Binary Search


1. Sequential Search 

यह सबसे सरल खोज विधि है। इसमें Array के हर तत्व को एक-एक करके जाँचा जाता है जब तक इच्छित तत्व न मिल जाए।
अगर तत्व मिल जाता है तो उसकी स्थिति (position) बताई जाती है, अन्यथा “not found” प्रदर्शित किया जाता है।

उपयोग:
जब Array unsorted या छोटा हो।


एल्गोरिद्म 

चरण 1: प्रारंभ करें
चरण 2: तत्वों की संख्या (n) और array के तत्व इनपुट करें।
चरण 3: खोजे जाने वाले तत्व (key) को इनपुट करें।
चरण 4: i = 0 से n–1 तक दोहराएँ
  यदि arr[i] == key
   तो "तत्व स्थान i पर मिला" प्रदर्शित करें
   और रोकें (Stop)
चरण 5: यदि लूप समाप्त हो जाए, तो "तत्व नहीं मिला" प्रदर्शित करें।
चरण 6: रोकें (Stop)


उदाहरण:
Array = [15, 25, 30, 12, 20]
खोजा जाने वाला तत्व (key) = 12

तुलनाएँ:
15 ≠ 12
25 ≠ 12
30 ≠ 12
12 = 12 (स्थिति 4 पर मिला)


💻 C Program for Sequential Search

#include <stdio.h> int main() { int A[10], n, i, e, found = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter array elements:\n"); for(i = 0; i < n; i++) scanf("%d", &A[i]); printf("Enter element to search: "); scanf("%d", &e); for(i = 0; i < n; i++) { if(A[i] == e) { printf("Element found at position %d\n", i + 1); break; } } if(i==n) printf("Element not found in the array.\n"); return 0; }

2. Binary Search 

Binary Search एक तेज़ (fast) खोज विधि है, लेकिन यह केवल sorted array पर काम करती है।

यह बार-बार Array को दो हिस्सों में बाँटकर खोज करती है:

  • अगर मध्य तत्व (middle element) key के बराबर है → मिला।

  • अगर key < middle → बाएँ हिस्से में खोज।

  • अगर key > middle → दाएँ हिस्से में खोज।

हर चरण में खोज क्षेत्र आधा होता जाता है → इसलिए यह बहुत तेज़ है।

उपयोग:
केवल sorted array के लिए।


एल्गोरिद्म 

चरण 1: प्रारंभ करें
चरण 2: तत्वों की संख्या (n) और क्रमबद्ध (sorted) array को इनपुट करें।
चरण 3: खोजा जाने वाला तत्व (key) इनपुट करें।
चरण 4: l = 0, h = n – 1 निर्धारित करें।
चरण 5: जब तक l ≤ h हो, दोहराएँ
  m = (l + h) / 2
  यदि arr[m] == key → तत्व मिल गया
  अन्यथा यदि arr[m] < key → l = m + 1
  अन्यथा → h = m - 1
चरण 6: यदि तत्व नहीं मिला हो, तो “तत्व नहीं मिला” प्रदर्शित करें।
चरण 7: रोकें (Stop)

उदाहरण:
Array = [10, 20, 30, 40, 50]
खोजा जाने वाला तत्व e = 30

l = 0, h = 4 → m = 2
arr[m] = 30 
तत्व स्थिति 3 पर मिला।


C Program for Binary Search

#include <stdio.h> int main() { int A[10], n, e, l, h, m, found = 0; printf("Enter 10 elements in ascending order:\n"); for(int i = 0; i < 10; i++) scanf("%d", &A[i]); printf("Enter element to search: "); scanf("%d", &e); l = 0; h = 10 - 1; while(l <= h) { m = (l + h) / 2; if(A[m] == e) { printf("Element found at position %d\n", m + 1); break; } else if(A[m] < e) l = m + 1; else h = m - 1; } if(l > h) printf("Element not found in the array.\n"); return 0; }

Difference Between Sequential Search and Binary Search 

बिंदुSequential Search Binary Search 
Definition (परिभाषा)हर तत्व को एक-एक करके जाँचते हैं जब तक कि इच्छित तत्व न मिल जाए।बीच वाले तत्व से तुलना की जाती है और हर बार खोज क्षेत्र आधा किया जाता है।
Array ConditionArray को sorted होना आवश्यक नहीं।Array को sorted होना आवश्यक है।
 (प्रक्रिया)पहला तत्व जाँचो, फिर दूसरा, और इसी तरह तब तक जब तक तत्व न मिल जाए।बीच का तत्व लो → तुलना करो → छोटा है तो बाएँ, बड़ा है तो दाएँ खोजो।
 (दक्षता)कम दक्ष — बड़े डेटा पर धीमी।बहुत दक्ष — बड़े डेटा पर तेज़।
Memory Requirementकमथोड़ी अधिक, क्योंकि मध्य गणना करनी होती है।
Algorithm Typeसरल, step-by-step searchDivide and Conquer (विभाजन और विजय)

No comments:

Post a Comment

String and Char with Function (In hindi)

  1. Character (अक्षर) अर्थ: एक Character (अक्षर) एक अकेला वर्ण, अंक या चिन्ह होता है जिसे single quotes (‘ ’) में लिखा जाता है। उदाहरण: ...