void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
The function contains a pair of loops. The initial loop iterates from the initial element to the second-to-last element in the array. The nested loop iterates from the initial element to the second-to-last element within the unsorted section of the array. The criteria for the nested loop is determined by n - i - 1 to account for the fact that the last i elements in the array have already been sorted.
During each cycle of the internal loop, we assess neighboring elements. Should the element on the left be larger than the one on the right, we interchange their positions. Once the internal loop finishes, the largest element is ensured to be positioned at the conclusion of the unordered segment within the array.
Now, we are ready to compose the primary function for evaluating our bubble sort algorithm. Below is the main function in conjunction with the preceding section:
C Program:
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
The primary function generates an array of integers named arr with a length of 7 and populates it with random values. Subsequently, we determine the array's size by dividing its total size by the size of each integer element. Following this, we invoke the bubble_sort function to arrange the elements within the array. Lastly, we display the sorted array by iterating through it using a for loop.
When executing the program, the expected output should display as follows:
Sorted array: 11 12 22 25 34 64 90
This result demonstrates that our bubble sort algorithm effectively arranged the array in ascending order.
To execute the software, it is necessary to compile it with a C compiler. Below is a sample compilation directive for GCC:
gcc -o bubble_sort bubble_sort.c
This command compiles the bubblesort.c source code file to generate an executable file called bubblesort.
In essence, the bubble sort technique involves iteratively exchanging neighboring elements until the array is in order. This method exhibits a time complexity of O(n 2 ), rendering it less effective for extensive datasets. Nonetheless, it serves as a valuable tool for educational scenarios and managing modest data collections. Our execution of the bubble sort algorithm in the C programming language was validated through a straightforward illustration.
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(n 2 ) in the worst case, where n is the size of the array.
- Bubble sort is useful for educational purposes and small data sets.
- It is not suitable for large data sets because of its time complexity.
- Bubble sort is easy to understand and implement.
- It requires minimal additional memory space to perform the sorting.
- 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.
Usage:
Advantages:
Disadvantages:
Conclusion:
Bubble sort stands out as a straightforward and easy-to-understand sorting technique, beneficial for educational contexts and when dealing with small sets of data. Nevertheless, its time complexity renders it less practical for large datasets, limiting its usage in real-world scenarios. In contrast, sorting methods like quicksort and mergesort prove to be more effective when handling extensive amounts of data.