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

CaseTime Complexity
Best CaseO(n)
Average CaseO(n2)
Worst CaseO(n2)

2. Space Complexity

Space ComplexityO(1)
StableYES

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)

  1. Starting from the first index, compare the first and the second elements.
  2. If the first element is greater than the second element, they are swapped.
  3. Now, compare the second and the third elements. Swap them if they are not in order.
  4. 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


Put the 2nd largest element at the 2nd largest index position

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


The array is sorted if all elements are kept in the right order

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

πŸ‘‰ The array is sorted when all the unsorted elements are placed at their correct positions.
πŸ‘‰ 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

Popular posts from this blog

String Functions in C Language(C Language)

PHP Array Functions

Home Menu Options in Ms Word