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
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
Bubble Algorithm
- There are two components to an array: sorted and unsorted. Initially
- Consider consecutively each two adjacent elements of the unsorted
part and swap them if necessary. The minimal (“lighter”) element
- The “lighter” element leaves the unsorted part and joins to the
- 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.
45 |
20 |
55 |
60 |
10 |
15 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
60 |
10 |
15 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
60 |
10 |
15 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
60 |
10 |
15 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
10 |
60 |
15 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
10 |
15 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
10 |
15 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
55 |
10 |
15 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
10 |
55 |
15 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
10 |
15 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
45 |
10 |
15 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
10 |
45 |
15 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
20 |
10 |
15 |
45 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
10 |
20 |
15 |
45 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
10 |
15 |
20 |
45 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
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
10 |
15 |
20 |
45 |
55 |
60 |
0 |
1 |
2 |
3 |
4 |
5 |
Arrays or lists were finally sorted using the Bubble sort, which you
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
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] + " "); } } }
#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);
}
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)
0 Comments