top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Back to Basics: Bubble Sort Optimized algorithm and Code in C.

+3 votes
777 views

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists. Bubble sort is not a stable sort which means that if two same elements are there in the list, they may not get their same order with respect to each other.

Step-by-step example:
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
*First Pass:*

( 5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) \to
Pin on Pinterest
 ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

Second Pass:

( 1 4 2 5 8 ) \to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.

Third Pass:

( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

Algorithm For Bubble Sort:

Step 1: Repeat Steps 2 and 3 for i=1 to 10

Step 2: Set j=1

Step 3: Repeat while j<=n

         (A) if  a[i] < a[j]

             Then interchange a[i] and a[j]

             [End of if]

         (B) Set j = j+1
        [End of Inner Loop]

    [End of Step 1 Outer Loop]

Step 4: Exit

Optimized Algorithm For Bubble Sort:

procedure bubbleSort ( A : list of sortable items )
    n = length(A)
    repeat
       swapped = false
       for i = 1 to n-1 inclusive do
          if A[i-1] > A[i] then
             swap(A[i-1], A[i])
             swapped = true
          end if
       end for
       n = n - 1
    until not swapped
end procedure

Bubble Sort Program using Function:

#include <stdio.h>

void bubble_sort(long [], long);

int main()
{
  long array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%ld", &n);

  printf("Enter %ld integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%ld", &array[c]);

  bubble_sort(array, n);

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%ld\n", array[c]);

  return 0;
}

void bubble_sort(long list[], long n)
{
  long c, d, t;

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (list[d] > list[d+1])
      {
        /* Swapping */

        t         = list[d];
        list[d]   = list[d+1];
        list[d+1] = t;
      }
    }
  }
}
posted Apr 21, 2014 by anonymous

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Best Case of the Bubble Sort
Let X: number of interchanges (discrete).
Consider list of n elements already sorted

x1 < x2 < x3 < … < xn

Only have to run through the list once since it is already in order, thus giving you n-1 comparisons and X = 0.
Therefore, the time complexity of the best case scenario is O(n).

Worst Case of the Bubble Sort:

Let X: number of interchanges (discrete).
Consider list of n elements in descending order

x1 > x2 > x3 > … > xn.

of comparisons = # of interchanges for each pass.

X = (n-1) + (n-2) + (n-3) + … + 2 + 1

This is an arithmetic series.

(n-1) + (n-2) + … + 2 + 1 = n(n-1)/2
So X = n(n-1)/2.

Therefore, the time complexity of the worst case scenario is O(n2).

Time Complexity Model of the Worst Case:
enter image description here

Average Case:

Random Variables:
A random variable is called discrete if its range is finite or countably infinite.
Bernoulli Random Variable – type of discrete random variable with a probability mass function p(x) = P{X = x}

Bernoulli Random Variable:
X is a Bernoulli Random Variable with probability p if

px(x) = px(1-p)1-x, x = {0,1}.
Other notes:       E[X] = p

Performace:

Although popular, bubble sort is nearly universally derided for its poor performance on random data. This derision is justified as shown in Figure 1 where bubble sort is nearly three times as slow as insertion sort.

enter image description here

READ MORE
...