Searching Types of Sorting
Searching Types of Sorting
Sorting Algorithms in C
Sorting algorithms are used to arrange elements of an array or list in a specific order
(ascending or descending).
1. Bubble Sort
Syntax
#include <stdio.h>
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
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
2. Selection Sort
Definition: This algorithm divides the array into a sorted and an unsorted part. The
smallest element from the unsorted part is selected and swapped with the first unsorted
element.
Syntax
int min_idx = i;
Program
#include <stdio.h>
int min_idx = i;
min_idx = j;
// Swap
arr[min_idx] = arr[i];
arr[i] = temp;
printf("\n");
int main() {
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
3. Insertion Sort
Definition: Builds the final sorted array one item at a time by inserting unsorted items into
their correct position.
Syntax
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
Program
#include <stdio.h>
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
printf("\n");
int main() {
insertionSort(arr, n);
printArray(arr, n);
return 0;
4. Quick Sort
quickSort(arr, pi + 1, high);
Program
#include <stdio.h>
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
quickSort(arr, pi + 1, high);
printf("\n");
int main() {
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
5. Merge Sort
Definition: A divide-and-conquer algorithm that divides the array into halves, recursively
sorts them, and merges the sorted halves.
Syntax
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
Program
#include <stdio.h>
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0, k = l;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
printf("\n");
int main() {
mergeSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
1. Bubble Sort
Syntax
c
Copy code
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]
}
}
}
Program
#include <stdio.h>
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
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
2. Selection Sort
Definition: This algorithm divides the array into a sorted and an unsorted part. The
smallest element from the unsorted part is selected and swapped with the first unsorted
element.
Syntax
Program
#include <stdio.h>
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
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
3. Insertion Sort
Definition: Builds the final sorted array one item at a time by inserting unsorted items into
their correct position.
Syntax
Program
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
4. Quick Sort
Syntax
Program
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
5. Merge Sort
Definition: A divide-and-conquer algorithm that divides the array into halves, recursively
sorts them, and merges the sorted halves.
Syntax
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Comparison of Sorting Algorithms
Theoretical Questions
1. What is Sorting?
Answer:
Answer:
1. Internal Sorting: Sorting done in memory (e.g., Bubble Sort, Quick Sort).
2. External Sorting: Sorting large data stored in external storage (e.g., Merge Sort with
external files).
3. What is the difference between stable and unstable sorting algorithms?
Answer:
• Stable Sorting: Maintains the relative order of equal elements (e.g., Bubble Sort,
Merge Sort).
• Unstable Sorting: May not maintain the relative order of equal elements (e.g.,
Quick Sort, Heap Sort).
Answer:
Answer:
Quick Sort is faster for small datasets because of its in-place sorting mechanism and
lower constant factors in runtime. Merge Sort requires additional memory for merging.
Answer:
Insertion Sort is best for nearly sorted data due to its low overhead and O(n) complexity in
the best case.
7. What is the advantage of Merge Sort over Quick Sort?
Answer:
Merge Sort is stable and has a guaranteed O(n log n) complexity for all cases, making it
suitable for large datasets and external sorting.
Answer:
Bubble Sort is not efficient for large datasets and is mostly used for educational purposes
or when the dataset size is very small.
Answer:
10. What is the difference between Radix Sort and Bucket Sort?
Answer:
Answer:
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 process is repeated
until the list is sorted.
Algorithm:
Working:
Advantages:
1. Simple to implement.
2. Best case (already sorted array) has a time complexity of O(n).
Disadvantages:
Answer:
Quick Sort:
Quick Sort is a divide-and-conquer algorithm that partitions the array into sub-arrays and
sorts them independently.
Algorithm:
Working Example:
• Array: [10, 7, 8, 9, 1, 5]
1. Pivot = 5. Partition: [1] [5] [10, 7, 8, 9].
2. Recursively sort left: [1].
3. Recursively sort right: [7, 8, 9, 10].
Time Complexity:
• Best Case: O(n log n) when the pivot divides the array into two equal parts.
• Worst Case: O(n²) when the smallest or largest element is chosen as the pivot.
• Average Case: O(n log n).
3. Write a detailed note on Merge Sort, including its
working mechanism, advantages, and disadvantages.
Answer:
Merge Sort:
Merge Sort is a divide-and-conquer algorithm that divides the array into two halves,
recursively sorts them, and merges the sorted halves.
Algorithm:
Working Example:
Advantages:
Disadvantages:
Answer:
Answer:
Radix Sort:
Working:
Time Complexity:
• Best, Worst, and Average Case: O(nk), where n is the number of elements and k is
the number of digits.
Advantages:
Disadvantages:
Answer:
Applications:
1. Bubble Sort:
a. Educational purposes (understanding basic sorting concepts).
b. Small datasets.
2. Quick Sort:
a. Efficient sorting in software libraries.
b. Used in scenarios where memory usage needs to be minimized.
3. Merge Sort:
a. External sorting (e.g., large datasets stored on disks).
b. Stable sorting requirements.
4. Heap Sort:
a. Priority queue implementations.
b. Real-time systems due to predictable O(n log n) performance.
5. Radix Sort:
a. Sorting large datasets of fixed-length keys (e.g., phone numbers, IP
addresses).
Answer:
Selection Sort:
Selection Sort repeatedly selects the smallest element from the unsorted portion of the
array and swaps it with the first unsorted element.
Algorithm:
Drawbacks: