0% found this document useful (0 votes)
22 views

Bubble_Sort

Uploaded by

Mayolito Waje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Bubble_Sort

Uploaded by

Mayolito Waje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The Bubble Sort Algorithm

By Mayolito C. Waje (BCS14)

What is Bubble Sort?


Bubble sort is one of sorting algorithm in unsorted arrays. Bubble sort keeps swapping unsorted
adjacent elements until all adjacent elements are sorted. Just like movement of air bubbles, each element of
array moves to the end of each iteration, that is why they are called bubble sort. Using Big O Notation, bubble
sort has a space complexity of O (1).

Algorithm:
Below is the algorithm of the bubble sort:

1. Assign a variable of Boolean data type (let’s call it sorted) and let it have a starting value of false.
2. While the array isn’t sorted (!sorted), iterate through elements of array.
3. Compare the first and second elements (adjacent elements in array), if second element is less than first
element, swap the two elements.
4. Do this to the next element in iteration until it reached the end of the array.
5. If we found elements that need to be swapped, sorted variable will always be false, hence continuing
the loop from the start.
6. Once we can’t find elements that need swapping, the sorted will become true, then it will exit the loop.

Bubble Sort Implementation Using Java:

(See next Page)


import java.util.Arrays;

public class BubbleSort {


private static void bubbleSort(int[] arr) {

// Assume that the array is unsorted at first


boolean sorted = false;

// While array is not unsorted


while (!sorted) {

// Assume the array is sorted first, but it will change it's value to
false if we found unsorted elements in array
sorted = true;

// Iterate from index 0 to second to the last index of array


for (int i = 0; i < arr.length - 1; i++) {
// store variables for the current element and the next element
in array (in index i + 1)
int ele1 = arr[i];
int ele2 = arr[i + 1];

// if the element from right (array[i + 1]) is less than the


current element, swap the two element's position
if (ele2 < ele1) {
arr[i] = ele2;
arr[i + 1] = ele1;
// since we found unsorted elements, let's assume that the
element is still unsorted so set sorted to false
sorted = false;
}
}
}

System.out.println("Sorted: " + Arrays.toString(arr));


}

public static void main(String[] args) {


int[] arr = {4, 7, 2, 5, 1, 6, 3};

System.out.println("Unsorted: " + Arrays.toString(arr)); // Unsorted: [4,


7, 2, 5, 1, 6, 3]
bubbleSort(arr); // Sorted: [1, 2, 3, 4, 5, 6, 7]
}
Pros and Cons of Bubble Sort Algorithm:

Pro’s

• Bubble sort only takes a few lines of code; hence it is easier to write.
• There is little memory overhead as the data is sorted in place. Once sorted, the data is in memory, ready
for processing.

Con’s

• Bubble sort has a space complexity of O (1), so it is slow. It is slower than selection sort. It will take
amount of time to sort and array.
• It is very not ideal for large array; due to the time it takes to sort an array. Other fast sorting algorithms
such as merge sort and quicksort are ideal for arrays with larger data.

Sources:

• https://www.programiz.com/dsa/bubble-
sort#:~:text=Bubble%20sort%20is%20a%20sorting,is%20called%20a%20bubble%20sort.
• https://www.techwalla.com/articles/advantages-disadvantages-of-bubble-sort

You might also like