Sorting Algorithms in Computer Science With C Code Snippets For Each
Sorting Algorithms in Computer Science With C Code Snippets For Each
FOR EACH.
1. Bubble Sort
Description: Bubble Sort 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.
c
Copy code
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
2. Selection Sort
Description: Selection Sort divides the array into a sorted and an unsorted part. It repeatedly
selects the smallest (or largest) element from the unsorted part and moves it to the end of the
sorted part.
c
Copy code
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
3. Insertion Sort
Description: Insertion Sort builds the final sorted array one item at a time. It’s much like
sorting playing cards in your hands. Elements from the unsorted part are picked and placed at
the correct position in the sorted part.
c
Copy code
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1] that are greater than key to one
position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
4. Merge Sort
Description: Merge Sort is a divide-and-conquer algorithm. It divides the input array into
two halves, calls itself for the two halves, and then merges the two sorted halves.
c
Copy code
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
5. Quick Sort
• Time Complexity: O(n log n) on average, but O(n²) in the worst case.
• Best For: Large datasets.
c
Copy code
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
6. Heap Sort
Description: Heap Sort is based on a binary heap data structure. It first builds a max heap
and then repeatedly extracts the maximum element from the heap and rebuilds it.
c
Copy code
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
7. Radix Sort
Description: Radix Sort processes integers by sorting them digit by digit, from the least
significant digit to the most significant. It requires a stable subroutine, such as Counting Sort,
for sorting digits.
• Time Complexity: O(nk), where k is the number of digits in the largest number.
• Best For: Large datasets with integer keys.
c
Copy code
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}