#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Bubble Sort क्या है? Working, Algorithm और Examples (हिंदी में)

संक्षिप्त जवाब: Bubble Sort एक simple sorting algorithm है जो array के पास-पास (adjacent) वाले elements को compare करता है और गलत order में होने पर उन्हें swap कर देता है। यह प्रक्रिया तब तक दोहराई जाती है जब तक पूरा array sort न हो जाए। हर pass में सबसे बड़ा element "bubble" की तरह ऊपर उठकर आखिर में पहुँच जाता है। इसकी time complexity O(n²) है।

Sorting programming का सबसे basic और जरूरी topic है, और Bubble Sort अक्सर पहला sorting algorithm होता है जो students सीखते हैं। इसका logic इतना आसान है कि आप इसे कागज़ पर हाथ से भी चला सकते हैं। इसी वजह से यह college exams, viva और coding interviews के शुरुआती सवालों में बहुत आता है।

लेखक: Mr. Irshad · Category: Data Structures & Algorithms · पढ़ने का समय: 11 मिनट

Worst & average case
O(n²)
Best case (optimized, sorted)
O(n)
Space complexity (in-place)
O(1)
अधिकतम passes (n elements)
n – 1

इस लेख में आप सीखेंगे

क्या?Bubble Sort क्या है और इसका नाम "Bubble" क्यों है
Dry Run[5, 3, 8, 4, 2] का pass-by-pass dry run
ProgramsPseudocode, C program और Python program
OptimizedOptimized Bubble Sort (early stopping)

साथ ही: Time और Space Complexity, फायदे-नुकसान और alternatives।

Bubble Sort क्या है?

Bubble Sort एक comparison-based sorting algorithm है। यह list में बार-बार घूमता है, हर दो adjacent elements को compare करता है, और अगर पहला element दूसरे से बड़ा है (ascending order के लिए), तो दोनों की जगह बदल देता है।

हर pass के बाद बचे हुए elements में सबसे बड़ा element अपनी सही जगह (आखिर) पर पहुँच जाता है, ठीक वैसे ही जैसे पानी में bubble ऊपर उठता है। इसीलिए इसका नाम Bubble Sort है। इसे Sinking Sort भी कहा जाता है।

Bubble Sort कैसे काम करता है?

  1. Array के पहले element से शुरू करें।
  2. Current element को अगले element से compare करें।
  3. अगर current element बड़ा है, तो दोनों को swap करें।
  4. एक position आगे बढ़ें और step 2–3 दोहराएँ, जब तक unsorted हिस्से का आखिर न आ जाए।
  5. एक pass पूरा होने पर सबसे बड़ा element आखिर में fix हो जाता है।
  6. बाकी unsorted हिस्से पर यही process दोहराएँ। n elements के लिए अधिकतम n – 1 passes लगते हैं।

Example: [5, 3, 8, 4, 2] को Step-by-Step Sort करना

Pass 1

Compare Action Array
5 और 3 Swap [3, 5, 8, 4, 2]
5 और 8 No swap [3, 5, 8, 4, 2]
8 और 4 Swap [3, 5, 4, 8, 2]
8 और 2 Swap [3, 5, 4, 2, 8]

Pass 1 के बाद सबसे बड़ा element 8 आखिर में पहुँच गया।

Pass 2

Compare Action Array
3 और 5 No swap [3, 5, 4, 2, 8]
5 और 4 Swap [3, 4, 5, 2, 8]
5 और 2 Swap [3, 4, 2, 5, 8]

Pass 3

Compare Action Array
3 और 4 No swap [3, 4, 2, 5, 8]
4 और 2 Swap [3, 2, 4, 5, 8]

Pass 4

Compare Action Array
3 और 2 Swap [2, 3, 4, 5, 8]

Final sorted array: [2, 3, 4, 5, 8]। ध्यान दें कि हर pass में comparisons एक-एक कम होते गए (4, 3, 2, 1), क्योंकि आखिर के elements पहले से sorted हो चुके थे।

Bubble Sort का Pseudocode

procedure bubbleSort(A: list of items) n = length(A) for i = 0 to n - 2 for j = 0 to n - i - 2 if A[j] > A[j + 1] then swap(A[j], A[j + 1]) end if end for end for end procedure

C में Bubble Sort Program

#include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { // passes for (int j = 0; j < n - i - 1; j++) { // adjacent comparisons if (arr[j] > arr[j + 1]) { // swap int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original array: "); printArray(arr, n); bubbleSort(arr, n); printf("Sorted array: "); printArray(arr, n); return 0; }

Output

Original array: 5 3 8 4 2 Sorted array: 2 3 4 5 8

Explanation: बाहरी loop (i) passes गिनता है और अंदर वाला loop (j) adjacent elements compare करता है। n - i - 1 इसलिए लिखा है क्योंकि हर pass के बाद आखिर के i elements sorted हो चुके होते हैं। Swap के लिए temp variable use होता है।

Python में Bubble Sort Program

def bubble_sort(arr): n = len(arr) for i in range(n - 1): # passes for j in range(n - i - 1): # adjacent comparisons if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] # swap return arr numbers = [5, 3, 8, 4, 2] print("Original:", numbers) print("Sorted: ", bubble_sort(numbers))

Output

Original: [5, 3, 8, 4, 2] Sorted: [2, 3, 4, 5, 8]

Explanation: Logic C जैसा ही है, बस Python में swap एक line में (a, b = b, a) हो जाता है।

Optimized Bubble Sort (Early Stopping)

Simple Bubble Sort हमेशा n – 1 passes चलाता है, भले ही array बीच में ही sort हो जाए। Optimized version एक swapped flag रखता है। अगर किसी pass में एक भी swap नहीं हुआ, तो मतलब array sorted है और loop वहीं रुक जाता है।

def bubble_sort_optimized(arr): n = len(arr) for i in range(n - 1): swapped = False for j in range(n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] swapped = True print(f"Pass {i + 1}: {arr}") if not swapped: # कोई swap नहीं हुआ = array sorted है print("No swaps, array already sorted. Loop रोक दिया।") break return arr bubble_sort_optimized([5, 3, 8, 4, 2]) print() bubble_sort_optimized([1, 2, 3, 4, 5])

Output

Pass 1: [3, 5, 4, 2, 8] Pass 2: [3, 4, 2, 5, 8] Pass 3: [3, 2, 4, 5, 8] Pass 4: [2, 3, 4, 5, 8] Pass 1: [1, 2, 3, 4, 5] No swaps, array already sorted. Loop रोक दिया।

दूसरे example में array पहले से sorted था, इसलिए optimized version सिर्फ 1 pass में रुक गया। यही कारण है कि इसका best case O(n) होता है।

Bubble Sort की Time और Space Complexity

Case Complexity कब होता है
Best Case O(n) Array पहले से sorted हो (सिर्फ optimized version में)
Average Case O(n²) Elements random order में हों
Worst Case O(n²) Array उल्टे (descending) order में हो
Space Complexity O(1) In-place sorting, कोई extra array नहीं चाहिए

इसके अलावा Bubble Sort एक stable sorting algorithm है, यानी बराबर values वाले elements का relative order नहीं बदलता।

Bubble Sort कब Use करें?

  • Sorting सीखने के लिए: Sorting का concept समझने का सबसे आसान तरीका।
  • Nested loops सिखाने के लिए: Teachers इसे loops और swapping समझाने में use करते हैं।
  • बहुत छोटे datasets: 10–20 elements जैसे छोटे data पर इसकी slowness खास मायने नहीं रखती।
  • Almost sorted data: Optimized version लगभग sorted data पर जल्दी खत्म हो जाता है।

Production code या बड़े data के लिए Bubble Sort सही choice नहीं है।

Bubble Sort के फायदे और नुकसान

फायदे (Advantages) नुकसान (Disadvantages)
समझने और लिखने में बहुत आसान बड़े datasets पर बहुत slow (O(n²))
कोई extra memory नहीं चाहिए (O(1) space) Swaps की संख्या बहुत ज्यादा हो सकती है
Stable sorting algorithm Real-world applications में practically use नहीं होता
Already sorted data detect कर सकता है (optimized) Insertion Sort जैसे simple algorithms भी आमतौर पर इससे तेज हैं

Bubble Sort के बेहतर Alternatives

Algorithm Average Time कब बेहतर है
Insertion Sort O(n²) छोटे या लगभग sorted data पर Bubble Sort से तेज
Merge Sort O(n log n) बड़े data, stable sorting चाहिए तब
Quick Sort O(n log n) ज्यादातर practical cases में सबसे तेज

Real-World Analogy

सोचिए कुछ लोग एक line में खड़े हैं और उन्हें height के हिसाब से लगाना है। हर व्यक्ति सिर्फ अपने बगल वाले से height compare करता है, और अगर वह लंबा है तो दोनों जगह बदल लेते हैं। बार-बार ऐसा करने पर सबसे लंबा व्यक्ति धीरे-धीरे line के आखिर में पहुँच जाता है। यही Bubble Sort है।

Common Mistakes जो Beginners करते हैं

  • Inner loop को n तक चलाना: j < n - i - 1 की जगह j < n लिखने से arr[j + 1] array से बाहर चला जाता है।
  • Swap में temp भूल जाना: arr[j] = arr[j+1]; arr[j+1] = arr[j]; लिखने से दोनों values same हो जाती हैं।
  • swapped flag को हर pass में reset न करना: Optimized version में flag हर pass की शुरुआत में False होना चाहिए।
  • Descending sort में condition न बदलना: Descending order के लिए > की जगह < use करें।

Bubble Sort से जुड़े FAQs

Q1. Bubble Sort क्या है?

Bubble Sort एक simple sorting algorithm है जो adjacent elements को compare करके गलत order में होने पर swap करता है। हर pass में सबसे बड़ा element आखिर में पहुँच जाता है।

Q2. Bubble Sort को Bubble Sort क्यों कहते हैं?

क्योंकि हर pass में सबसे बड़ा element पानी के bubble की तरह धीरे-धीरे ऊपर (array के आखिर में) पहुँच जाता है।

Q3. Bubble Sort की time complexity क्या है?

Worst और average case में O(n²) और optimized version के best case (already sorted array) में O(n)। Space complexity O(1) है।

Q4. n elements के लिए Bubble Sort में कितने passes होते हैं?

अधिकतम n – 1 passes होते हैं। Optimized version में अगर किसी pass में कोई swap न हो, तो loop पहले ही रुक जाता है।

Q5. क्या Bubble Sort stable है?

हाँ। Bubble Sort सिर्फ तब swap करता है जब पहला element strictly बड़ा हो, इसलिए बराबर values का relative order नहीं बदलता।

Q6. Bubble Sort और Selection Sort में क्या अंतर है?

Bubble Sort adjacent elements को बार-बार swap करता है, जबकि Selection Sort हर pass में minimum element ढूँढकर एक ही swap करता है। दोनों O(n²) हैं, लेकिन Selection Sort में swaps कम होते हैं।

Q7. क्या real projects में Bubble Sort use होता है?

बहुत कम। बड़े data के लिए यह slow है, इसलिए real projects में Merge Sort, Quick Sort या language के built-in sort functions use होते हैं।

Key Takeaways

  • Bubble Sort adjacent elements compare और swap करके array sort करता है।
  • हर pass के बाद सबसे बड़ा element अपनी सही जगह पहुँच जाता है।
  • Worst और average case O(n²), optimized best case O(n), space O(1)।
  • यह stable और in-place है, लेकिन बड़े data के लिए slow है।
  • Real projects में Merge Sort या Quick Sort बेहतर हैं।

निष्कर्ष (Conclusion)

Bubble Sort दुनिया का सबसे तेज algorithm नहीं है, लेकिन sorting की सोच समझने के लिए यह सबसे अच्छी शुरुआत है। Comparisons, swapping, nested loops और optimization, ये सारे concepts इसी एक algorithm से सीखे जा सकते हैं। एक बार इसे अच्छे से समझ लें, फिर Insertion Sort, Merge Sort और Quick Sort सीखना काफी आसान हो जाता है।

Sorting algorithms और पूरा DSA structured तरीके से सीखना चाहते हैं? Uncodemy का Data Structures and Algorithms Course देखें।

Related Hindi Guides

नीचे दिए Hindi pages live होने के बाद इस blog में contextual links जोड़ें — जो page अभी live नहीं है, उसका link न डालें (404 से बचें)।

Topic Planned URL
DSA Guide /hi/blog/what-is-dsa-data-structures-and-algorithms-complete-guide
Sorting Time Complexity /hi/blog/time-complexity-of-all-sorting-explained-clearly
Binary Search in C /hi/blog/learn-binary-search-in-c-with-code-example
C में Array /hi/blog/what-is-array-in-c-language-types-and-examples

Visit Us

Delhi Centre Address
2nd & 3rd Floor, Maa Katyayni Complex, MB-1E, Madhuban Rd, next to DSEU Ambedkar College, Laxmi Nagar, Shakarpur Extension, Shakarpur, Delhi, 110092
Opening hours
Monday – Sunday, 06:00 – 22:30
Website
uncodemy.com