×

Bubble sort in C

Bubble sort in C

Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor value present within the array and then keeps it repeating until it is sorted in the correct order.

Bubble sort is the simple way of performing the sorting technique. The value of the array has to be assigned to the array first before starting the sorting.

Data sorting is considered to be an essential concept. During the times when the data needs to be arranged in a particular order or in an organized way, sorting techniques are used. Bubble sort is considered the most straightforward sorting technique among the ones present in the C standard as it is very easy to implement and understand.

It follows the approach of comparing the current element with the preceding one and then swaps it accordingly if required, such as if it is less or more significant. This gives accurate results no matter what. Every time an element is passed into an array, it will be compared with all the elements present within the array, and then it decides a final place to that inserted element and is called a pass.

Bubble sort gets the name as it filters out the elements of the top of the array, similar to the bubbles on the water. Although this algorithm is the slowest, it runs with O’s time complexity (n^2).

A bubble sort might be optimized by using the variable called to flag that exists the loop once, and then the swapping can be initiated. The best complexity of the bubble sort can be O(n).

Bubble sort algorithm:

  1. Starting from the index zero in an array, compare the element with the preceding one (a[0] and a[1] considering the name of the array as a) compare both the elements and swap if a[1] > a[2]. Repeat this process until the end of the array. After doing so, the most significant element will be placed as the end of the array. The whole thing is considered as a pass. The array elements will be processed in the first pass in the first pass [0, n - 1].
  2. Repeat the previous step but instead consider the elements from [0, n - 2] as the last one, that is, a[n - 1] is present at its correct position. After this particular step, the largest of the two elements will be compared and will be swapped.
  3. Repeat the same steps for n - 1 number of times.

E.g.:

 #include <stdio.h>
 int main()
 {
 int total_count, counter, counter1, swap_var;
 int array[20];
 printf("How many numbers do you want to input?\n");
 scanf("%d", &total_count);
 printf("Please enter %d integers that have to be sorted\n", total_count);
 for (counter = 0; counter < total_count; counter++)
 scanf("%d", &array[counter]);
 for (counter = 0 ; counter < total_count - 1; counter++)
 {
 for (counter1 = 0 ; counter1 < total_count - counter - 1; counter1++)
 {
 if (array[counter1] > array[counter1+1]) /* For decreasing order use < */
 {
 swap_var       = array[counter1];
 array[counter1]   = array[counter1+1];
 array[counter1+1] = swap_var;
 }
 }
 }
 printf("Below is the list of elements sorted in ascending order:\n");
 for (counter = 0; counter < total_count; counter++)
 printf("%d\n", array[counter]);
 return 0;
 } 

Output:

 How many numbers do you want to input?
 4
 Please enter 4 integers that have to be sorted
 4 2 5 1
 Below is the list of elements sorted in ascending order:
 1
 2
 4
 5 

Once the above program is compiled and run, it will further ask the programmer or the developer for the number of elements that they want to sort.

Once it is provided, the program will ask the user to provide values equivalent to their provided count. The values will be stored in the array and processed further using nested for loop together with decision-making using “if” to sort the array.

The first smallest value found in the array has been moved to the array’s first index, and then the search begins again to find the other smallest number.

Once the following smallest number is found, it replaces the value in the second index, and the process keeps on repeating until the array consists of a sorted list of values.

Complexities:

  • Worst case time complexity: O(n^2). If we want to sort, the array is ascending order, and the given array is in descending series.
  • Best case time complexity: O(n). if the array is already sorted in the beginning and there is no necessity of sorting.
  • Average case time complexity: O(n^2).  This occurs when the element of the array occurs in the chaotic order.

Space complexity:

  • Space complexity is always O(1) as an extra variable is used for  swapping
  • In an optimized bubble sort algorithm, an extra two of the variables will be used. Hence the space complexity will be O(2).

Applications:

  • Bubble sort can be used if the complexity does not matter
  • If the array is short and readable.

Related Topics

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.