Bubble Sort || Why and Where to use || In Different Language 2023



<script type="text/javascript"> atOptions = { 'key' : '60aaa4994174dd43d1cb670beb31c89a', 'format' : 'iframe', 'height' : 600, 'width' : 160, 'params' : {} }; document.write('<scr' + 'ipt type="text/javascript" src="//www.profitablecreativeformat.com/60aaa4994174dd43d1cb670beb31c89a/invoke.js"></scr' + 'ipt>'); </script>
Bubble Sort

What is sorting?

Sorting refers to the process of arranging items or data elements in a 

specific order based on some criteria. In the field of computer science 

and data processing, sorting is a fundamental procedure. It is used to 

organize data in a way that makes it easier to search, retrieve, and 

analyze.


Bubble sort

An easy sorting method called bubble sort iteratively goes through 

the list to be sorted, compares nearby components, and swaps them 

if they are out of order. The process continue until no swaps are 

needed, which means the list is sorted.


Bubble Algorithm

  • There are two components to an array: sorted and unsorted. Initially 
       the array unsorted.

  • Consider consecutively each two adjacent elements of the unsorted 

       part and swap them if necessary. The minimal (“lighter”) element 

       comes out on top as a bubble.

  • The “lighter” element leaves the unsorted part and joins to the 
        sorted one.

  • Repeat steps 2 and 3 until array is sorted.

 

Bubble sort Property

  • Easy to implement.
  • Required n (n+1)/2 number of comparison.
  • Not suitable for large number of data.

 

Tracing of Bubble sort

Original Array

45

20

55

60

10

15

0

1

2

3

4

5



During First Iteration (0-----5):

Compare first index (45) with all the index’s Values.

‘45’ greater than ‘20’ swap them [45, 20, 55, 60, 10, 15]

20

45

55

60

10

15

0

1

2

3

4

5

 
‘45’ less than ‘55’ skip [20, 45, 55, 60, 10, 15]

20

45

55

60

10

15

0

1

2

3

4

5

 
‘55’ less than ‘60’ skip [20, 45, 55, 60, 10, 15]

20

45

55

60

10

15

0

1

2

3

4

5


‘60’ greater than ‘10’ swap them [20, 45, 55, 60, 10, 15]

20

45

55

10

60

15

0

1

2

3

4

5


‘60’ greater than ‘15’ swap them [20, 45, 55, 10, 60, 15]

20

45

55

10

15

60

0

1

2

3

4

5

 

During Second Iteration (0-----4):


‘20’ less than ‘45’ skip [20, 45, 55, 10, 15, 60]

20

45

55

10

15

60

0

1

2

3

4

5


‘45’ less than ‘55’ skip [20, 45, 55, 10, 15, 60]

20

45

55

10

15

60

0

1

2

3

4

5


‘55’ greater than ‘10’ swap them [20, 45, 55, 10, 15, 60]

20

45

10

55

15

60

0

1

2

3

4

5


‘55’ greater than ‘15’ swap them [20, 45, 10, 55, 15, 60]

20

45

10

15

55

60

0

1

2

3

4

5


During Third Iteration (0-----3):


‘20’ less than ‘45’ skip [20, 45, 10, 15, 55, 60]

20

45

10

15

55

60

0

1

2

3

4

5


‘45’ greater than ‘10’ swap them [20, 45, 10, 15, 55,60]

20

10

45

15

55

60

0

1

2

3

4

5


‘45’ greater than ‘15’ swap them [20, 10, 45, 15, 55, 60]

20

10

15

45

55

60

0

1

2

3

4

5

 

During Fourth Iteration (0-----2): 


‘20’ greater than ‘10’ swap them [20, 10, 15, 45, 55,60]

10

20

15

45

55

60

0

1

2

3

4

5


‘20’ greater than ‘15’ swap them [10, 20, 15, 45, 55,60]

10

15

20

45

55

60

0

1

2

3

4

5

 

During Fifth Iteration (0-----1):


‘10’ less than ‘15’ skip [10, 20, 15, 45, 55,60]

10

15

20

45

55

60

0

1

2

3

4

5


Next iteration does not fulfill the condition requirement as we don’t have 

enough values to compare!

 

Final Product

10

15

20

45

55

60

0

1

2

3

4

5


Arrays or lists were finally sorted using the Bubble sort, which you 

now understand.

The algorithms start with very first index and compare it with second index 

(1) and this process continue till last and swap them according to condition 

(greater than or less than) after that in next iteration it start from first index 

(0) as usual but compare value till second last then third last and so on so 

forth.

This procedure is repeated until the array is sorted and no additional swaps 

are needed to complete a full run of the array. Although bubble sort is well 

known for its simplicity, its time complexity of O (n^2) makes it not the 

most effective sorting algorithm, especially for large datasets.


Program in Different Language


JAVA

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {6, 4, 15, 98, 10, 7};
        int size = 6;
        bubbleSort(arr, size);
    }
    public static void bubbleSort(int[] arr, int size) {
        display(arr);
        System.out.println();
        System.out.println("-----------------");
        for (int i = size - 1; i > 0; i--) {
            for (int j = 0; j < i; 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;
                }
            }
        }
        // Final
        System.out.println("-----------------");
        display(arr);
        System.out.println();
    }

    public static void display(int[] arr) {
       for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

C++

        #include<iostream>
        using namespace std
;
        void
display(int arr[]);
        void
booblesort(int arr[], int size)
        {
        display(arr)
;
       
cout<<endl;
       
cout<<"-----------------"<<endl;

        for
(int i=size; i>0 ; i--)
        {
       
for(int j =0 ; j<size ; j++)
        {
       
if(arr[j]>arr[j+1])
        {
        swap(arr[j]
,arr[j+1]);
       
}
        }
        }
       
// final
       
cout<<"-----------------"<<endl;
        
display(arr);
       
cout<<endl;
       
}
       
void display(int arr[])
        {
       
for(int i=0 ; i<=6 ; i++)
        {
        cout<<arr[i]<<
" ";
       
}
        }
       
int main()
        {
       
int arr[]={6,4,15,98,10,7};
        int
size=6;
       
booblesort(arr,size);
       
}



Python

        arr = [6, 4, 15, 98, 10, 7]
        size =
6         def bubble_sort(arr):         display(arr)         print()         print("-----------------")
       
for i in range(size, 0, -1):         for j in range(i - 1):         if arr[j] > arr[j + 1]:         # Swap arr[j] and arr[j+1]         arr[j], arr[j + 1] = arr[j + 1], arr[j]
        # Final
        print(
"-----------------"         display(arr)         print()
        def display(arr):        
for num in arr:         print(num, end=" ")         bubble_sort(arr)

Post a Comment

0 Comments