Data Structures (Bubble Sort in Data Structures)
Bubble sort
π Bubble sort is the simplest sorting algorithm in data structures.
π In Bubble sort, Each element of the array is compared with its adjacent element.
π The algorithm processes the list in passes.
π A list with n elements requires n-1 passes for sorting.
π In every pass the highest element is places at the highest Index position in a list.
Technique :
π In Pass 1, A[0] is compared with A[1], A[1] is compared with A[2], A[2] is compared
with A[3] and so on. At the end of pass 1, the largest element of the list is placed at
the highest index of the list.
π In Pass 2, A[0] is compared with A[1], A[1] is compared with A[2] and so on. At the
end of Pass 2 the second largest element of the list is placed at the second highest
index of the list.
π In pass n-1, A[0] is compared with A[1], A[1] is compared with A[2] and so on. At
the end of this pass. The smallest element of the list is placed at the first index of
the list.
Algorithm :
Step 1: Repeat Step 2 For i = 0 to N-1
Step 2: Repeat For J = i to N – i - 1
Step 3: IF A[J] > A[J+1]
SWAP A[J] and A[i]
[END OF INNER LOOP]
[END OF OUTER LOOP
Step 4: EXIT
Bubble sort complexity
1. Time Complexity
Case | Time Complexity |
---|---|
Best Case | O(n) |
Average Case | O(n2) |
Worst Case | O(n2) |
2. Space Complexity
Space Complexity | O(1) |
Stable | YES |
Working of Bubble sort Algorithm:
Let the elements are -2,45,0,11,-9
Suppose we are trying to sort the elements in ascending order.
1. First Iteration (Compare and Swap)
- Starting from the first index, compare the first and the second elements.
- If the first element is greater than the second element, they are swapped.
- Now, compare the second and the third elements. Swap them if they are not in order.
- The above process goes on until the last element.
After Completion of the first iteration the highest element of an array in the list is placed at the highest index position in the list.
2. Iteration-2
In each iteration, the comparison takes place up to the last unsorted element.
After completion of the second iteration the second highest element of an array in the list is placed at the second highest index position in a list
3. Iteration-3
After completion of the second iteration the third highest element of an array in the list is placed at the third highest index position in a list
4. Iteration-4
After completion of the second iteration the fourth highest element of an array in the list is placed at the fourth highest index position in a list
π In this example we are taking 5 elements in an array then the maximum number of iteration =( n-1) i.e (5-1) =4 iterations
Implementation of Bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,temp,i,j;
clrscr();
printf("Enter the size of an array");
scanf("%d",&n);
printf("Enter the values in to an array\n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
//bubble sort logic
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The array elements after implementing the bubble sort");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}
OTUPUT:
Comments
Post a Comment