😚Bubble_sort😀
Bubble sort is simplest & the most popular sorting
Method
a idea behind bubble sort is bubble rises up in water the
.Smallest element goes to the beginning. This method is
Based on successive selection the smallest element
Through exchange of adjacent element
Syntax :- bubble_sort
{
int data
};
Formula bubble_sort :- n - 1
Examples :- it is array list like { 5, 9 , 6 , 2 , 8 , 1 }
Solve it :- total no = 6 - 1 = 05 pass
Given 5 , 9 , 6 , 2 , 8 , 1
Pass 1{
5 9 6 2 8 1
5 9 6 2 8 1
5 6 9 2 8 1
5 6 2 9 8 1
5 6 2 8 9 1
}
Pass 2{
5 6 2 8 1 9
5 6 2 8 1 9
5 2 6 8 1 9
5 2 6 8 1 9
}
Pass 3{
5 2 6 1 8 9
2 5 6 1 8 9
2 5 6 1 8 9
}
Pass 4 {
2 5 6 1 8 9
2 5 1 6 8 9
}
Pass 5 { 2 1 5 6 8 9 }
Sorted array { 1 2 5 6 8 9 }
Bubble_sort Logic :-
Void bubble_sort ( int a[] , int)
{
int i , j , temp ;
For( i = 1 ; i < n ; i ++)
For( j = 1 ; j < n ; j ++)
If ( a[j]>a[ j+1] )
{
temp=a[j] ;
a[ j ] = a [ j + 1] ;
a[ j +1] = temp ;
}
}
Definition bubble_sort :-
Bubble sort is a simple sorting algorithm.
This sorting algorithm is comparison-based algorithm in
Which each pair of adjacent elements is compared and
The elements are swapped if they are not in order.
For Examples :-
Take unsorted array list like { 17 , 46 , 44 , 27 , 57 , 42 , 45 , 21 , 90 }
Take This Rules :- 1) bubble_sort starts with first two element comparing them to check which one is greater.
2) in case of "17&46" . 46 is also greater than 14 so it already in sorted location
3) next we camper "46&44" We find 44 is smaller than 46 & These two value are swapped.
The new array is looked like this
{ 17 , 21 , 27 , 42 , 44 , 45 , 46 , 57 , 90 }
Bubble_sort Algorithm - :
Bubble_sort time complexity :- the
*
Best of time complexity:- the time complexity of bubble sort simplicity of algorithm the best-case complexity requiring a single pass to determine order.
* worst of time complexity :- it is worth-case average time completely it is a faster algorithm.
* Average of time complexity :- it is easy_to_implement
stable sorting algorithm the passes required (n / 2) & 0(n)
Comparison of each pass
* space complexity :- it is only a single additional memory
space is required .
* Stability :- yes
Mini_project
bubble_sort
Check this link :- https://code.sololearn.com/W3Uf1HaHfJqF/?ref=app
Input-output screens :-
Comments
Post a Comment