SWAMI VIVEKANANDA INSTITUTE OF SCIENCE & TECHNOLOGY
Department of CSE
sub: C Programming
Sorting Algorithms
A sorting algorithm is used to rearrange the elements of an array or list into a specific order—
typically increasing or decreasing.
For example, given an array [10, 20, 5, 2]:
Sorting in increasing order yields [2, 5, 10, 20]
Sorting in decreasing order yields [20, 10, 5, 2]
In C programming, sorting algorithms are used to sort data (such as integers, strings, or
other objects) stored in arrays or lists. Sorting can help in faster searching, organizing
data, or improving the efficiency of other algorithms.
Importance of Sorting Algorithms:
Sorting algorithms are fundamental in computer science because they help organize data and reduce
complexity in solving problems. They play a crucial role in a wide range of applications, improving
efficiency and enabling faster operations.
Some of the most common sorting algorithms are:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Heap Sort
Radix Sort
Counting Sort
Bucket Sort
Bubble Sort in C:
Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. This process continues until no more
swaps are needed, indicating the list is sorted.
1
, SWAMI VIVEKANANDA INSTITUTE OF SCIENCE & TECHNOLOGY
Department of CSE
sub: C Programming
How does Bubble Sort Work?
Example array: [5, 3, 8, 4, 2]
Pass Compare Swap? Array State
1 5 vs 3 Yes [3, 5, 8, 4, 2]
5 vs 8 No [3, 5, 8, 4, 2]
8 vs 4 Yes [3, 5, 4, 8, 2]
8 vs 2 Yes [3, 5, 4, 2, 8]
2 3 vs 5 No [3, 5, 4, 2, 8]
5 vs 4 Yes [3, 4, 5, 2, 8]
5 vs 2 Yes [3, 4, 2, 5, 8]
3 3 vs 4 No [3, 4, 2, 5, 8]
4 vs 2 Yes [3, 2, 4, 5, 8]
4 3 vs 2 Yes [2, 3, 4, 5, 8]
Final Sorted Array: [2, 3, 4, 5, 8]
Key Points:
Each pass compares adjacent elements and swaps them if needed.
The largest element "bubbles" to the end with each pass.
Repeat until no more swaps are needed, meaning the array is sorted.
Example:
#include <stdio.h>
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
// Outer loop for the number of passes through the array
for (i = 0; i < n - 1; i++) {
// Inner loop to compare adjacent elements
for (j = 0; j < n - i - 1; j++) {
// If the current element is greater than the next, swap them
if (arr[j] > arr[j + 1]) {
temp = arr[j];
2
Department of CSE
sub: C Programming
Sorting Algorithms
A sorting algorithm is used to rearrange the elements of an array or list into a specific order—
typically increasing or decreasing.
For example, given an array [10, 20, 5, 2]:
Sorting in increasing order yields [2, 5, 10, 20]
Sorting in decreasing order yields [20, 10, 5, 2]
In C programming, sorting algorithms are used to sort data (such as integers, strings, or
other objects) stored in arrays or lists. Sorting can help in faster searching, organizing
data, or improving the efficiency of other algorithms.
Importance of Sorting Algorithms:
Sorting algorithms are fundamental in computer science because they help organize data and reduce
complexity in solving problems. They play a crucial role in a wide range of applications, improving
efficiency and enabling faster operations.
Some of the most common sorting algorithms are:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Heap Sort
Radix Sort
Counting Sort
Bucket Sort
Bubble Sort in C:
Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. This process continues until no more
swaps are needed, indicating the list is sorted.
1
, SWAMI VIVEKANANDA INSTITUTE OF SCIENCE & TECHNOLOGY
Department of CSE
sub: C Programming
How does Bubble Sort Work?
Example array: [5, 3, 8, 4, 2]
Pass Compare Swap? Array State
1 5 vs 3 Yes [3, 5, 8, 4, 2]
5 vs 8 No [3, 5, 8, 4, 2]
8 vs 4 Yes [3, 5, 4, 8, 2]
8 vs 2 Yes [3, 5, 4, 2, 8]
2 3 vs 5 No [3, 5, 4, 2, 8]
5 vs 4 Yes [3, 4, 5, 2, 8]
5 vs 2 Yes [3, 4, 2, 5, 8]
3 3 vs 4 No [3, 4, 2, 5, 8]
4 vs 2 Yes [3, 2, 4, 5, 8]
4 3 vs 2 Yes [2, 3, 4, 5, 8]
Final Sorted Array: [2, 3, 4, 5, 8]
Key Points:
Each pass compares adjacent elements and swaps them if needed.
The largest element "bubbles" to the end with each pass.
Repeat until no more swaps are needed, meaning the array is sorted.
Example:
#include <stdio.h>
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
// Outer loop for the number of passes through the array
for (i = 0; i < n - 1; i++) {
// Inner loop to compare adjacent elements
for (j = 0; j < n - i - 1; j++) {
// If the current element is greater than the next, swap them
if (arr[j] > arr[j + 1]) {
temp = arr[j];
2