Array Algorithms in C

Array algorithms in C programming involve various techniques and operations for processing and manipulating arrays efficiently. Here, we'll discuss some common array algorithms with detailed explanations and examples:

Searching Algorithms

Linear Search

This algorithm iterates through the array to find a specific element.

int linearSearch(int arr[], int size, int target) { for (int i = 0; i < size; i++) { if (arr[i] == target) { return i; // Element found at index i } } return -1; // Element not found }

Binary Search

This algorithm works on sorted arrays and efficiently searches for a target element by repeatedly dividing the search interval in half.

int binarySearch(int arr[], int size, int target) { int left = 0; int right = size - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; // Element found at index mid } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; // Element not found }

Sorting Algorithms

Bubble Sort

This simple sorting algorithm repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

void bubbleSort(int arr[], int size) { for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - 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; } } } }

Quick Sort

This efficient divide-and-conquer sorting algorithm selects a 'pivot' element and partitions the array into two sub-arrays, recursively sorting them.

void quickSort(int arr[], int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; // Swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // Swap arr[i+1] and arr[high] int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; }

Array Manipulation

Reverse Array

Reversing an array means changing the order of its elements, making the last element the first, and so on.

void reverseArray(int arr[], int size) { int left = 0; int right = size - 1; while (left < right) { // Swap arr[left] and arr[right] int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } }

Array Rotation

Array rotation involves shifting elements to the left or right by a certain number of positions.

Example (left rotation by k positions):

void leftRotate(int arr[], int size, int k) { for (int i = 0; i < k; i++) { int temp = arr[0]; for (int j = 0; j < size - 1; j++) { arr[j] = arr[j + 1]; } arr[size - 1] = temp; } }

Conclusion

Array algorithms in C programming are essential techniques for performing operations on arrays efficiently. These algorithms include searching and sorting methods like linear search, binary search, bubble sort, and quick sort, as well as array manipulation techniques like reversing and rotating arrays. Mastery of these algorithms is crucial for solving a variety of problems involving arrays and data manipulation in C.