Javatpoint Logo
Javatpoint Logo

Bubble sort program in C

Bubble sort is a simple and intuitive sorting algorithm. It repeatedly swaps adjacent elements if they are in the wrong order until the array is sorted. In this algorithm, the largest element "bubbles up" to the end of the array in each iteration. Bubble sort is inefficient for large data sets, but it is useful for educational purposes and small data sets. In this article, we will implement the bubble sort algorithm in C programming language.

The first step is to define the bubble sort function. This function takes an integer array and the size of the array as its parameters. The function returns nothing as it modifies the original array. Here is the function definition:

The function has two loops. The outer loop runs from the first element to the second-last element of the array. The inner loop runs from the first element to the second-last element of the unsorted part of the array. The condition of the inner loop is n - i - 1 because the last i elements of the array are already sorted.

In each iteration of the inner loop, we compare adjacent elements. If the left element is greater than the right element, we swap them. After the inner loop completes, the largest element is guaranteed to be at the end of the unsorted part of the array.

Now, we can write the main function to test our bubble sort implementation. Here is the main function along with the previous part:

C Program:

The main function creates an integer array arr of size 7 and initializes it with random numbers. We then calculate the size of the array by dividing the size of the array by the size of an integer element. Next, we call the bubble_sort function to sort the array. Finally, we print the sorted array using a for loop.

When we run the program, we should see the following output:

Sorted array: 11 12 22 25 34 64 90

This output shows that our bubble sort implementation correctly sorted the array in ascending order.

To run the program, we need to compile it using a C compiler. Here is an example compilation command for GCC:

This command compiles the bubble_sort.c file and produces an executable file named bubble_sort.

In summary, the bubble sort algorithm repeatedly swaps adjacent elements until the array is sorted. The algorithm has a time complexity of O(n2), which makes it inefficient for large data sets. However, it is useful for educational purposes and small data sets. We implemented the bubble sort algorithm in C programming language and tested it using a simple example.

Characteristics:

  • Bubble sort is a simple sorting algorithm.
  • It works by repeatedly swapping adjacent elements if they are in the wrong order.
  • The algorithm sorts the array in ascending or descending order.
  • It has a time complexity of O(n2) in the worst case, where n is the size of the array.

Usage:

  • Bubble sort is useful for educational purposes and small data sets.
  • It is not suitable for large data sets because of its time complexity.

Advantages:

  • Bubble sort is easy to understand and implement.
  • It requires minimal additional memory space to perform the sorting.

Disadvantages:

  • It is not efficient for large data sets because of its time complexity.
  • It has poor performance compared to other sorting algorithms, such as quicksort and mergesort.

Conclusion:

Bubble sort is a simple and intuitive sorting algorithm that is useful for educational purposes and small data sets. However, its time complexity makes it inefficient for large data sets. Therefore, it is not commonly used in real-world applications. Other sorting algorithms, such as quicksort and mergesort, are more efficient for large data sets.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA