How to Implement Bubble Sort in C with Code

Published on Aug 20,2019 5K Views

How to Implement Bubble Sort in C with Code

edureka.co

Bubble sort in C is a simple sorting algorithm which repeatedly compares the adjacent elements of the given array & swaps them if they are in the wrong order. You might be wondering about the name Bubble Sort. Following are the Pointers Covered in this article:

 

What is a Bubble Sort in C?

The sorting technique is called so because the algorithm acts like a bubble, the lighter elements come up and heavier elements settle down. Bubble Sort algorithm sorts the list in passes. Now, to sort a list with n elements Bubble sort requires n-1 passes. To make it clearer, let’s understand this step by step.

 

Algorithm of Bubble Sort

 

Example of Bubble Sort in C

Array: -5, 35, 2, 13, -15

Pass 1

The last element is the largest element.

 

Pass 2

The second last element is the second largest element.

 

Pass 3

The third last element is the third largest element.

 

Pass 4

Eventually, the first is the smallest & 2nd is the second smallest element in the array.  So, in this case, four passes were required to sort an array of 5 elements.

 

Before looking at the algorithm in detail, let’s look at the time complexity of the Bubble Sort in C algorithm.

 

The complexity of Bubble Sort

Now let us quickly look at the algorithm, so that moving ahead we can write the Bubble sort algorithm in C.

 

Bubble Sort Function

void bubbleSort(int array[], int n) 
{ 
   int i, j; 
//Pass in Bubble Sort
   for (i = 0; i < n-1; i++)       
  
       /* Comparing the two adjacent elements & swapping if elements are not at the correct position */
       for (j = 0; j < n-i-1; j++) if (array[j] > array[j+1]) 
              swap(&array[j], &array[j+1]); 
}

 

Bubble Sort in C Program

#include <stdio.h> 

// Function to swap elements 
void swap(int *a, int *b) 
{ 
    int temp = *a; 
    *a = *b; 
    *b = temp; 
} 
  
// bubble sort function
void bubbleSort(int array[], int n) 
{ 
   int i, j; 
   for (i = 0; i < n-1; i++)       
  
       for (j = 0; j < n-i-1; j++) if (array[j] > array[j+1]) 
              swap(&array[j], &array[j+1]); 
} 
  
// Function to print the elements of an array
void printArray(int array[], int size) 
{ 
    int i; 
    for (i=0; i < size; i++) 
        printf("%d ", array[i]); 
    printf("n"); 
} 
  
// Main Function
int main() 
{ 
    int array[] = {-5, 35, 2, 13, -15}; 
    int size = sizeof(array)/sizeof(array[0]); 
    bubbleSort(array, size); 
    printf("Sorted array: n"); 
    printArray(array, size); 
    return 0; 
}

Now after executing the above C program you would have understood how Bubble Sort works & how to implement it in C language. I hope this blog is informative and added value to you.

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this Bubble Sort in C article and we will get back to you as soon as possible.

BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial