How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the C programming language
How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the C programming language
Table of Contents
Problem Statement
A bubble sort is generally considered to be the simplest sorting algorithm.
A bubble sort is also known as a sinking sort.
Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses.
Because of its abysmal O(n2) performance, it is not used often for large (or even medium-sized) datasets.
The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them).
Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass.
A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
This can be expressed in pseudo-code as follows (assuming 1-based indexing):
Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. For languages where this is not possible, sort an array of integers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the C programming language
This C code performs bubble sort on an array of integers and then prints the sorted array. Here's a detailed explanation:
-
Bubble Sort Function (bubble_sort):
void bubble_sort(int *a, int n)
: This function takes an arraya
and its sizen
as parameters and sorts the array in ascending order.int i, t, j = n, s = 1;
: Initializes variables for loop countersi
andj
, a temporary variablet
to store the exchanged elements,j
to iterate through the array, ands
to indicate whether any swaps were made during the current pass.while (s)
: This is the main loop that continues until no swaps are made in a pass (i.e.,s
becomes 0).- The outer loop iterates from the second element (
i = 1
) to thej
-th element. - The inner loop compares each element with the previous one and swaps them if the previous one is greater.
- After each inner loop iteration,
s
is set to 1 if a swap was made, indicating that another pass is needed. j
is decremented after each outer loop iteration to reduce the sorted portion of the array.
-
Main Function (main):
int main()
is the program's entry point.- It initializes an integer array
a
with unsorted values. int n = sizeof a / sizeof a[0];
: Calculates the size of the arraya
by dividing its total size in bytes by the size of each element (in this case, an integer).- It prints the original unsorted array using a loop.
bubble_sort(a, n);
: Calls thebubble_sort
function to sort the arraya
.- It prints the sorted array using another loop.
- Finally, the program returns 0, indicating successful execution.
In summary, this code demonstrates the bubble sort algorithm to sort an array of integers in ascending order. It prints the unsorted and sorted arrays for comparison.
Source code in the c programming language
#include <stdio.h>
void bubble_sort (int *a, int n) {
int i, t, j = n, s = 1;
while (s) {
s = 0;
for (i = 1; i < j; i++) {
if (a[i] < a[i - 1]) {
t = a[i];
a[i] = a[i - 1];
a[i - 1] = t;
s = 1;
}
}
j--;
}
}
int main () {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
bubble_sort(a, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
return 0;
}
You may also check:How to resolve the algorithm String comparison step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Raku programming language
You may also check:How to resolve the algorithm Equal prime and composite sums step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Factor programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Run BASIC programming language