Discover Serenity: Your Guide to Mindfulness Apps
Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps
Discover How Smartwatches Measure Blood Pressure: Explained Simply
13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!
Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo
Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!
Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals
Affordable VR Adventures: The Best Budget VR Headsets
Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying
Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More
Discover Exciting New Games on NVIDIA GeForce NOW!
Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games
The Future of iPhones: Apple's Bold Step into AI with DarwinAI
Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!
Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!
Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches
Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024
Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!
Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent
Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer
Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!
JavaScript Essentials: Your Quick Start to Web Coding
Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!
Gaming Joy Awaits: Explore the Best PS5 Games of 2024
Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!
Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit
Feel the Beat: Exploring Top Over-Ear Headphones
Explore the Web Development Strategies in 2024: A Developers Handbook
Explore Must-Play Nintendo Switch Games in 2024!
Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle
Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World
Discover Waze's cool new features for safer and happier drives!
Discover the Top Picks: Best Smartwatches for Your Lifestyle
Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!
Sound Revolution: Discover the Best Bluetooth Speakers of 2024!
Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks
Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024
Crack the Code: Your Guide to Computer Programming Magic in 2024
Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity
10 Best Tech Companies in Silicon Valley
Top 10 Web Development Interview Questions you can...
Learn how to Answer Tell me about Yourself
5 Books You Need to Read Right Now
25 Practical Ways to Earn Money Online
Translate this page in your preferred language:
Article by: Manish Methani
Last Updated: October 10, 2021 at 10:04am IST
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.
Suppose we have to sort the array of elements(1,8,5,4,2) in ascending order using Bubble Sort:
1) Array is 1, 8, 5, 4, 2 .
In the given array first two elements are compared 1 & 8 and since 1 < 8, no swapping takes place.
2) Array becomes 1, 8, 5, 4, 2.
Now the next two elements are compared 8 & 5 and since 8 > 5, a swap takes place.
After swapping 8 & 5 array looks like this 1,5,8,4,2.
3) Array becomes 1, 5, 8, 4, 2.
Now the next two elements are compared 8 & 4 as and since 8 > 4, a swap takes place.
After swapping 8 & 4, the array looks like this 1,5,4,8,2.
4) Array becomes 1, 5, 4, 8, 2.
Now the next two elements are compared 8 & 2 as and since 8 > 2, a swap takes place.
After swapping 8 & 2, the array looks like this 1, 5, 4, 2, 8.
1) Array becomes 1, 5, 4, 2, 8.
Now the next two elements are compared 1 & 5 and since 1 < 5, no swap here.
2) Array becomes 1, 5, 4, 2, 8.
Now the next two elements are compared 5 & 4 as and since 5 > 4, a swap takes place.
After swapping 5 & 4, the array looks like this 1, 4, 5, 2, 8.
3) Array becomes 1, 4, 5, 2, 8.
Now the next two elements are compared 5 & 2 as and since 5 > 2, a swap takes place.
After swapping 5 & 2, the array looks like this 1, 4, 2, 5, 8.
1) Array becomes 1, 4, 2, 5, 8.
Now the next two elements are compared 1 & 4 and since 1 < 4, no swap here.
2) Array becomes 1, 4, 2, 5, 8.
Now the next two elements are compared 4 & 2 as and since 4 > 2, a swap takes place.
After swapping 5 & 4, the array looks like this 1, 2, 4, 5, 8.
1) Array becomes 1, 2, 4, 5, 8.
Now the next two elements are compared 1 & 2 and since 1 < 2, no swap here.
Notice that a total of 4 passes was required to sort the array of 5 elements. In the first pass, 4 comparisons were done and the largest element was bubbled out at the end of the array. Similarly, in the second pass, 3 comparisons were done, in the third pass, 2 comparisons were done, and so on... At the end of each pass, the largest element gets bubbled up near the end of the array.
In general,
To bubble sort an array of N elements, total N-1 passes are required in the worst case.
In the first pass, N-1 comparisons will be done, in the second pass, N-2 comparisons will be made, in the third pass, N-3 comparisons and so on until only 1 comparison is needed.
So total number of comparisons
= (N-1) + (N-2) + (N-3) + .... + 1 = (N-1)(N-2) / 2 = O(N2) ....ignoring constants and lower order terms..
Thus, the complexity of bubble sort in the worst case is O(N2) where N is the number of elements of the array.
This basic Bubble Sort in C programming explains the concept of how to sort the elements using bubble sort in Data Structures.
#include void swap(int *a, int *b){ *a = *a + *b; *b = *a - *b; *a = *a -*b; } void bubbleSort(int N, int arr[]) { int pass = 0; int flag = 1; int i; for(pass = 0; pass < N && flag; pass++) { printf("Pass %d : ", pass); for (i = 0; i < N - pass -1; ++i) { if (arr[i] > arr[i+1]) { swap(&arr[i], &arr[i+1]); flag = 1; } else{ flag = 0;+ } } for (i = 0; i < N; ++i) { printf("%d ", arr[i]); } printf(" "); printf(" "); } } int main(void) { int N; scanf("%d", &N); int arr[N], i; for(i = 0; i < N; i++) { scanf("%d", &arr[i]); } printf("Input Array is: "); for(i = 0; i < N; i++) { printf("%d ", arr[i]); } printf(" "); bubbleSort(N, arr); printf("Sortrd Array: "); for(i = 0; i < N; i++) { printf("%d ", arr[i]); } printf(" "); return 0; }
Test your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test