← Back to DSA Course | Chapter 9: Sorting Algorithms | Lesson 1 of 9

Bubble Sort क्या है

Bubble sort एक fizzy drink में उठते bubbles जैसा है: आप neighbors compare करते हैं और अगर वे गलत order में हों तो उन्हें swap करते हैं, और सबसे बड़े आखिर तक float होते हैं।
Syntax
markup
for i in range(n - 1):
    swapped = False
    for j in range(n - 1 - i):
        if arr[j] > arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]
            swapped = True
    if not swapped:
        break

Basic Idea

Bubble sort बार-बार array से गुज़रता है, हर pair के adjacent elements compare करते हुए और अगर वे गलत order में हों तो swap करते हुए, ताकि हर पूरे pass के साथ सबसे बड़ी बची unsorted value आखिर में अपनी सही position तक 'bubble up' हो।

उदाहरण: Basic Idea

#include <iostream>
using namespace std;
int main() {
	int arr[] = {5, 1, 4, 2, 8};
	int n = 5;
	for (int i = 0; i < n - 1; i++)
		for (int j = 0; j < n - i - 1; j++)
			if (arr[j] > arr[j + 1]) swap(arr[j], arr[j + 1]);
	for (int i = 0; i < n; i++) cout << arr[i] << " ";
	return 0;
}
import java.util.*;
public class Main {
	public static void main(String[] args) {
		int[] arr = {5, 1, 4, 2, 8};
		int n = arr.length;
		for (int i = 0; i < n - 1; i++)
			for (int j = 0; j < n - i - 1; j++)
				if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; }
		System.out.println(Arrays.toString(arr));
	}
}
arr = [5, 1, 4, 2, 8]
n = len(arr)
for i in range(n - 1):
    for j in range(n - i - 1):
        if arr[j] > arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(arr)
#include <stdio.h>
int main() {
	int arr[] = {5, 1, 4, 2, 8};
	int n = 5;
	for (int i = 0; i < n - 1; i++)
		for (int j = 0; j < n - i - 1; j++)
			if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; }
	for (int i = 0; i < n; i++) printf("%d ", arr[i]);
	return 0;
}

Step by Step

हर pass left से right neighboring elements compare करता है; अगर एक pass में कहीं भी एक swap होता है, array अभी पूरी तरह sorted नहीं थी, इसलिए एक और pass चाहिए। यह तब तक repeat होता है जब तक एक पूरा pass zero swaps के साथ पूरा न हो, array sorted होने की confirm करते हुए।

उदाहरण: Step by Step

#include <iostream>
using namespace std;
int main() {
	int arr[] = {5, 1, 4, 2, 8};
	int n = 5;
	bool swapped;
	for (int i = 0; i < n - 1; i++) {
		swapped = false;
		for (int j = 0; j < n - i - 1; j++)
			if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); swapped = true; }
		if (!swapped) break;
	}
	for (int i = 0; i < n; i++) cout << arr[i] << " ";
	return 0;
}
import java.util.*;
public class Main {
	public static void main(String[] args) {
		int[] arr = {5, 1, 4, 2, 8};
		int n = arr.length;
		boolean swapped;
		for (int i = 0; i < n - 1; i++) {
			swapped = false;
			for (int j = 0; j < n - i - 1; j++)
				if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; swapped = true; }
			if (!swapped) break;
		}
		System.out.println(Arrays.toString(arr));
	}
}
arr = [5, 1, 4, 2, 8]
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
    if not swapped:
        break
print(arr)
#include <stdio.h>
#include <stdbool.h>
int main() {
	int arr[] = {5, 1, 4, 2, 8};
	int n = 5;
	bool swapped;
	for (int i = 0; i < n - 1; i++) {
		swapped = false;
		for (int j = 0; j < n - i - 1; j++)
			if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; swapped = true; }
		if (!swapped) break;
	}
	for (int i = 0; i < n; i++) printf("%d ", arr[i]);
	return 0;
}

Small Array

[5, 1, 4, 2] जैसे एक छोटे array पर bubble sort को हाथ से trace करना — हर adjacent comparison और swap देखते हुए — bubbling behavior को concrete बनाता है और बिल्कुल दिखाता है कि सबसे बड़ी values पहले क्यों जगह में settle होती हैं।

उदाहरण: Small Array

#include <iostream>
using namespace std;
int main() {
	int arr[] = {5, 1, 4, 2};
	int n = 4;
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			cout << "Comparing " << arr[j] << " and " << arr[j + 1] << endl;
			if (arr[j] > arr[j + 1]) swap(arr[j], arr[j + 1]);
		}
	}
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int[] arr = {5, 1, 4, 2};
		int n = arr.length;
		for (int i = 0; i < n - 1; i++) {
			for (int j = 0; j < n - i - 1; j++) {
				System.out.println("Comparing " + arr[j] + " and " + arr[j + 1]);
				if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; }
			}
		}
	}
}
arr = [5, 1, 4, 2]
n = len(arr)
for i in range(n - 1):
    for j in range(n - i - 1):
        print("Comparing", arr[j], "and", arr[j + 1])
        if arr[j] > arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]
#include <stdio.h>
int main() {
	int arr[] = {5, 1, 4, 2};
	int n = 4;
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - i - 1; j++) {
			printf("Comparing %d and %d\n", arr[j], arr[j + 1]);
			if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; }
		}
	}
	return 0;
}

Practice

Input array modify करने और loop के अंदर एक swap counter या print statement जोड़ने की कोशिश करें यह देखने के लिए कि अलग-अलग starting arrangements को कितने comparisons और swaps चाहिए, खासकर एक already-sorted array को एक reverse-sorted से compare करते हुए।

उदाहरण: Practice

#include <iostream>
using namespace std;
int main() {
	int arr[] = {9, 3, 7, 1};
	int n = 4, swaps = 0;
	for (int i = 0; i < n - 1; i++)
		for (int j = 0; j < n - i - 1; j++)
			if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); swaps++; }
	cout << "Total swaps: " << swaps;
	return 0;
}
public class Main {
	public static void main(String[] args) {
		int[] arr = {9, 3, 7, 1};
		int n = arr.length, swaps = 0;
		for (int i = 0; i < n - 1; i++)
			for (int j = 0; j < n - i - 1; j++)
				if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; swaps++; }
		System.out.println("Total swaps: " + swaps);
	}
}
arr = [9, 3, 7, 1]
n = len(arr)
swaps = 0
for i in range(n - 1):
    for j in range(n - i - 1):
        if arr[j] > arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]
            swaps += 1
print("Total swaps:", swaps)
#include <stdio.h>
int main() {
	int arr[] = {9, 3, 7, 1};
	int n = 4, swaps = 0;
	for (int i = 0; i < n - 1; i++)
		for (int j = 0; j < n - i - 1; j++)
			if (arr[j] > arr[j + 1]) { int t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; swaps++; }
	printf("Total swaps: %d", swaps);
	return 0;
}

Summary

Bubble sort worst और average case में O(n²) time में चलता है, जो इसे practice में बड़े datasets के लिए बहुत धीमा बनाता है, लेकिन इसकी simplicity इसे merge sort या quicksort जैसे तेज़ algorithms पर जाने से पहले एक उपयोगी पहला example बनाती है।

उदाहरण: Summary

#include <iostream>
using namespace std;
int main() {
	cout << "Bubble sort: O(n^2) worst/average time, simple but slow for large datasets";
	return 0;
}
public class Main {
	public static void main(String[] args) {
		System.out.println("Bubble sort: O(n^2) worst/average time, simple but slow for large datasets");
	}
}
print("Bubble sort: O(n^2) worst/average time, simple but slow for large datasets")
#include <stdio.h>
int main() {
	printf("Bubble sort: O(n^2) worst/average time, simple but slow for large datasets");
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Inner pass को n - i - 1 के बजाय n तक loop करना, इसलिए arr[j + 1] आखिर से आगे पढ़ता है और sorted tail फिर compare होता है।
  2. > के बजाय >= से swap करना, जो बराबर elements को बेकार swap करता है और sort को unstable बनाता है।
  3. जब एक pass में कोई swap न हो तो जल्दी न रुकना, इसलिए एक already-sorted array अब भी O(n^2) लेता है।
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.