What is Sorting? Define Its Type and their advantage. What is Bubble Sort? Write an Algorithm and C Program for Bubble Sorting.
Sorting
Sorting is the process of arranging items in a specific order, typically ascending or descending. The items could be anything such as numbers, words, or objects. Sorting is a fundamental problem in computer science and is used in many applications, including data analysis, searching, and optimization.
There are various types of sorting algorithms used in Modern time :
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Quick Sort 12. Strand Sort
5. Heap Sort 13. Bitonic Sort
6. Counting Sort 14. Pancake Sort
7. Radix Sort
8. Shell Sort 16. Tag Sort
17. Tim Sort 18. Merge Sort
Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Advantages of Bubble Sort:
· Bubble sort is easy to understand and implement.
· Bubble sort is useful for small data sets.
· Bubble sort has a space complexity of O(1), meaning it does not require any additional memory space other than the original array.
· Bubble sort is a stable sorting algorithm, meaning it preserves the relative order of equal elements in the original array.
//Algorithm of Bubble Sort
Step 1. Start
Step 2. Bubble Sort(A : list of sortable items)
n = length(A)
repeat
swapped = false
for i = 1 to n-1 do
if A[i] > A[i+1] then
swap A[i] with A[i+1]
swapped = true
n = n - 1
until swapped = false
Step 3. End
//C program for Bubble Sort:
#include <stdio.h>
void bubbleSort(int array[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
int main() {
int array[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(array)/sizeof(array[0]);
bubbleSort(array, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
return 0;
}
Visit Also :-
What is Selection Sort, Insertion Sort and Merge Sort?
https://codingtuition.blogspot.com/2023/05/what-is-selection-sort-insertion-sort.html
Comments
Post a Comment