C Program
#include<stdio.h> int main() { int a[100], n, x, i; scanf("%d%d", &n, &x); for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n && a[i] != x; i++); printf(i < n ? "Found at %d" : "Not Found", i); }
C Output
Input: 5 12 3 7 12 9 5 Output: Found at 2
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n, x, i; cin >> n >> x; for(i = 0; i < n; i++) cin >> a[i]; for(i = 0; i < n && a[i] != x; i++); cout << (i < n ? "Found at " + to_string(i) : "Not Found"); }
C++ Output
Input: 4 8 1 2 3 8 Output: Found at 3
JAVA Program
import java.util.*; class L { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), x = sc.nextInt(), i; int[] arr = new int[n]; for(i = 0; i < n; i++) arr[i] = sc.nextInt(); for(i = 0; i < n && arr[i] != x; i++); System.out.print(i < n ? "Found at " + i : "Not Found"); } }
JAVA Output
Input: 6 10 4 5 10 23 42 11 Output: Found at 2
Python Program
n, x = map(int, input().split()) a = list(map(int, input().split())) print(f"Found at {a.index(x)}" if x in a else "Not Found")
Python Output
Input: 5 7 2 4 6 7 9 Output: Found at 3
In-Depth Learning – Whole Concept in Paragraphs
What Is Bubble Sort?
Bubble Sort is a very basic sorting algorithm used in programming. It iteratively traverses the list, compares two adjacent elements, and exchanges them if they are in the wrong order. This continues until the list is sorted. Not the most efficient sorting method for large data sets, it's ideal for understanding how sorting works at a very basic level.
How the Code Works
In bubble sort:
We employ two loops:
The outer loop counts the number of passes.
The inner loop does comparisons and swapping.
With each pass, the largest unsorted item "bubbles up" to its proper position at the end of the array.
We terminate one element short each time, since the end portion becomes sorted.
The line if a[j] > a[j+1] is checking whether two consecutive numbers are out of order and swapping them if so.
Example
Suppose:
Array = [4, 2, 5, 1, 3]
Pass 1: [2, 4, 1, 3, 5]
Pass 2: [2, 1, 3, 4, 5]
Pass 3: [1, 2, 3, 4, 5]
Pass 4: [1, 2, 3, 4, 5] (No swaps required — completely sorted)
You can observe that with every pass, the largest number "bubbles" to the proper position.
Real-Life Analogy
Imagine bubble sort as sorting bubbles by size. You line them up and check two at a time — if the one on the left is larger, you swap them. Do this repeatedly until no bubbles must move — and now they're in order from smallest to largest.
Where Is Bubble Sort Implemented?
Bubble sort is employed primarily for educational purposes and for extremely small data sets. Although not efficient for large-scale sorting, it is useful to teach beginners about:
Nested loops
Conditional swapping
Optimized comparisons
It is frequently asked in interviews to evaluate logic-construction skill and your understanding of simple algorithmic thought. It also pops up in college homework, viva questions, and MCQ rounds.
Time Complexity
Best Case (Already Sorted): O(n)
Average and Worst Case: O(n²)
Although it's simple, it's never utilized in production due to inefficiency — but it's still an excellent foundation for learning more efficient algorithms like Insertion Sort, Merge Sort, or Quick Sort.
Python-Specific Benefit
Python enables easy, readable syntax for swapping through tuple unpacking:
python
a[j], a[j+1] = a[j+1], a[j]
This does away with needing a temp variable and the code remains beautiful.
SEO-Optimized Natural Paragraph for Ranking
Want to know how Bubble Sort is the simplest? This tutorial provides you with concise and functional code for Bubble Sort in C, C++, Java, and Python, ideal for beginners studying sorting algorithms. Bubble Sort is a simple but effective tool to understand sorting through comparisons and swaps. Even though it is not the most effective for large datasets, it's amongst the best to master logic-building and nested loops. Whether you're studying for exams, interviews, or simply learning to sort data step-by-step, becoming proficient at Bubble Sort sets you off to an excellent start with algorithmic problem-solving.
Social Plugin