example of BUBBLE SORT IN C

0

Bubble sort in C

Bubble sort in C to arrange numbers in ascending order, you can modify it for descending order and can also sort strings. The bubble sort algorithm is not efficient as its both average-case as well as worst-case complexity are O(n2).

Bubble sort algorithm

  1. Start at index zero, compare the element with the next one (a[0] and a[1] (a is the name of the array)), and swap if a[0] > a[1]. Now compare a[1] and a[2] and swap if a[1] > a[2]. Repeat this process until the end of the array. After doing this, the largest element is present at the end. This whole thing is known as a pass. In the first pass, we process array elements from [0,n-1].
  2. Repeat step one but process array elements [0, n-2] because the last one, i.e., a[n-1], is present at its correct position. After this step, the largest two elements are present at the end.
  3. Repeat this process n-1 times.

All Contributions

/* Bubble sort code */

#include 

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

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

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

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

  for (c = 0 ; c < n - 1; c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

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

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

  return 0;
}

total contributions (1)

need a help?


find thousands of online teachers now

New to examplegeek.com?

Join us