Code: Bubble sort

# Code: Bubble sort - Basics of C++ - Software Development

 Table of contents Understanding Bubble Sort Bubble Sort Algorithm Bubble Sort Code in C++ Conclusion Sorting is a fundamental operation in computer programming, and one of the simplest sorting algorithms is Bubble Sort. Bubble Sort is easy to understand and implement, making it an excellent starting point for beginners learning sorting algorithms in C++. In this article, we'll explore Bubble Sort in detail, along with multiple examples and code explanations.

## Understanding Bubble Sort

Bubble Sort is a comparison-based sorting algorithm that repeatedly steps through the list to be sorted, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted. The name "Bubble Sort" comes from the way smaller elements "bubble" to the top of the list during each pass.

## Bubble Sort Algorithm

Here's a step-by-step breakdown of the Bubble Sort algorithm:

• Compare the first and second elements. If they are in the wrong order, swap them.
• Move to the next pair of elements and repeat the comparison and swapping process.
• Continue this process until you reach the end of the list.
• Repeat steps 2-4 for the remaining passes until the list is sorted.

Now let's dive into some example code to see how Bubble Sort works in practice.

## Bubble Sort Code in C++

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j+1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

}

int main() {

int arr[] = {5, 2, 8, 12, 3};

int size = sizeof(arr) / sizeof(arr);

cout << "Original array: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

}

bubbleSort(arr, size);

cout << "\nSorted array: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

}

return 0;

}

### Code Explanation

• We start by defining the bubbleSort function, which takes an array (arr) and its size (size) as parameters.
• The outer for loop runs from 0 to size - 1, representing the number of passes required to sort the array.
• Inside the outer loop, we have another nested for loop that iterates from 0 to size - i - 1. The size - i - 1 ensures that we don't compare elements that are already in their correct sorted positions.
• In the inner loop, we compare adjacent elements (arr[j] and arr[j + 1]) and swap them if arr[j] is greater than arr[j + 1].
• Finally, we print the original and sorted arrays using the cout statement.

Let's see the output of the above code:

Original array: 5 2 8 12 3

Sorted array: 2 3 5 8 12

As you can see, the Bubble Sort algorithm successfully sorted the array in ascending order.

## Conclusion

Bubble Sort is a simple yet effective sorting algorithm that can be easily understood and implemented by beginners. It serves as a great starting point for learning about sorting algorithms in C++. By grasping the concepts and examining the code examples in this article, you should now have a solid understanding of how Bubble Sort works.

The document Code: Bubble sort | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development

## Basics of C++

70 videos|45 docs|15 tests

## Basics of C++

70 videos|45 docs|15 tests Explore Courses for Software Development exam Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;