Search & Sort Methods in C Programming
Introduction
Search and sort algorithms are fundamental concepts in computer science and programming. This guide covers various search and sort methods with their implementations in C, time complexities, and practical applications.
1. Search Algorithms
| S.No | Algorithm | Difficulty | Time Complexity | Implementation |
|---|---|---|---|---|
| 1 | Linear Search | Easy | O(n) |
// Linear Search in C
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
|
| 2 | Binary Search | Easy | O(log n) |
// Binary Search in C (Iterative)
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}
|
| 3 | Jump Search | Medium | O(√n) |
// Jump Search in C
int jumpSearch(int arr[], int n, int x) {
int step = sqrt(n);
int prev = 0;
while (arr[min(step, n)-1] < x) {
prev = step;
step += sqrt(n);
if (prev >= n) return -1;
}
while (arr[prev] < x) {
prev++;
if (prev == min(step, n)) return -1;
}
if (arr[prev] == x) return prev;
return -1;
}
|
| 4 | Interpolation Search | Medium | O(log log n) avg, O(n) worst |
// Interpolation Search in C
int interpolationSearch(int arr[], int n, int x) {
int lo = 0, hi = n - 1;
while (lo <= hi && x >= arr[lo] && x <= arr[hi]) {
int pos = lo + (((double)(hi - lo) /
(arr[hi] - arr[lo])) * (x - arr[lo]);
if (arr[pos] == x) return pos;
if (arr[pos] < x) lo = pos + 1;
else hi = pos - 1;
}
return -1;
}
|
2. Basic Sorting Algorithms
| S.No | Algorithm | Difficulty | Time Complexity | Implementation |
|---|---|---|---|---|
| 1 | Bubble Sort | Easy | O(n²) |
// Bubble Sort in C
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;
}
}
}
}
|
| 2 | Selection Sort | Easy | O(n²) |
// Selection Sort in C
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;
}
}
|
| 3 | Insertion Sort | Easy | O(n²) |
// Insertion Sort in C
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 = j - 1;
}
arr[j + 1] = key;
}
}
|
3. Efficient Sorting Algorithms
| S.No | Algorithm | Difficulty | Time Complexity | Implementation |
|---|---|---|---|---|
| 1 | Merge Sort | Medium | O(n log n) |
// Merge Sort in C
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];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
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);
}
}
|
| 2 | Quick Sort | Medium | O(n log n) avg, O(n²) worst |
// Quick Sort in C
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++;
// Swap
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap
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);
}
}
|
| 3 | Heap Sort | Medium | O(n log n) |
// Heap Sort in C
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
// Swap
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
// Swap
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
|
4. Specialized Sorting Algorithms
| S.No | Algorithm | Difficulty | Time Complexity | Implementation |
|---|---|---|---|---|
| 1 | Counting Sort | Medium | O(n + k) |
// Counting Sort in C
void countingSort(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max) max = arr[i];
int count[max + 1];
for (int i = 0; i <= max; ++i)
count[i] = 0;
for (int i = 0; i < n; i++)
count[arr[i]]++;
for (int i = 1; i <= max; i++)
count[i] += count[i - 1];
int output[n];
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
|
| 2 | Radix Sort | Medium | O(nk) |
// Radix Sort in C
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;
}
void countSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
|
| 3 | Bucket Sort | Medium | O(n + k) |
// Bucket Sort in C
void bucketSort(float arr[], int n) {
// Create n empty buckets
vector
|
Algorithm Comparison
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
Related Resources