C UNIT-5
C UNIT-5
SEARCHING:
1. Linear Search in an Array
How It Works:
1. The function linear_Search iterates through each
element of the array.
2. If the element matches the target, the function
immediately returns the index of the element.
3. If the function completes the loop without finding the
target, it returns -1 to indicate that the element is not
present in the array.
Algorithm for Linear Search:
4. If the loop ends and the target is not found, return -1.
Complexity Analysis:
Time Complexity:
o Best Case: O(1)O(1)O(1) (target is the first
element).
oWorst Case: O(n)O(n)O(n) (target is not in the
array or is the last element).
o Average Case: O(n)O(n)O(n), where n is the
for variables.
PROGRAM:
#include <stdio.h>
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
} return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int target;
printf("Enter the element to search: ");
scanf("%d", &target);
int result = linearSearch(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
}
else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
OUTPUT:
Enter the element to search: 30
Element 30 found at index 2.
Enter the element to search: 60
Element 60 not found in the array.
Steps:
Example:
Let's search for the number 25 in the array [10, 22, 35,
40, 25, 60]:
2. Binary Search:
Algorithm
Input:
Example:
Let's search for the number 25 in the sorted array [10, 22,
25, 35, 40, 60]:
}
return -1;
}
int main() {
int arr[] = {1, 3, 5, 7, 9, 11};
int size = sizeof(arr) / sizeof(arr[0]);
int target;
printf("Enter the element to search: ");
scanf("%d", &target);
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
}
else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
OUTPUT:
Enter the element to search: 7
Element 7 found at index 3.
Advantages:
impractical.
Use Binary Search:
o The dataset is large.
available.
Types of Sorting
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
1. Bubble Sort
correct position.
2. Second Pass:
o Compare 25 and 12. Since 25 > 12, swap them.
22.
o Compare 22 and 11. Since 22 > 11, swap them.
2. Selection Sort
right.
o Insert 25 in the correct position. The array becomes
right.
o Compare 12 with 25. Since 12 < 25, shift 25 to the
right.
o Insert 12 in the correct position. The array becomes
{12, 25, 64, 22, 11}.
3. Move to the fourth element 22:
o Compare 22 with 64. Since 22 < 64, shift 64 to the
right.
o Compare 22 with 25. Since 22 < 25, shift 25 to the
right.
o Insert 22 in the correct position. The array becomes
right.
o Compare 11 with 25. Since 11 < 25, shift 25 to the
right.
o Compare 11 with 22. Since 11 < 22, shift 22 to the
right.
o Compare 11 with 12. Since 11 < 12, shift 12 to the
right.
o Insert 11 in the correct position. The array becomes
right.
o Insert 25 in the correct position. The array becomes
right.
o Compare 12 with 25. Since 12 < 25, shift 25 to the
right.
o Insert 12 in the correct position. The array becomes
right.
o Compare 22 with 25. Since 22 < 25, shift 25 to the
right.
o Insert 22 in the correct position. The array becomes
4. Merge Sort
PROGRAM:
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
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];
i++;
k++;
}
OUTPUT:
125569
Quick Sort Steps:
1. Choose a Pivot: Select an element from the array as a pivot
(it can be any element, typically the last element or the
middle element).
2. Partitioning: Rearrange the array so that:
o Elements smaller than the pivot come before it.
each.
o Merge {25} and {12}: Compare elements:
1. Merge the two sorted halves {12, 25, 64} and {11,
22}:
o Compare the elements:
64}
5. Quick Sort
of equal elements.
o Recursive Nature: Can cause stack overflow for
64:
Compare 25 with 64: Swap 25 with itself (since
25 < 64).
Compare 12 with 64: Swap 12 with itself (since
12 < 64).
Compare 22 with 64: Swap 22 with itself (since
22 < 64).
After partitioning, we swap the pivot 64 with 64 (the
element at i+1), resulting in the partitioned array {11,
12, 22, 25, 64}.
< 22.
pivot selection).
Real-time systems: Quick Sort, as it is faster on
average.
Comparison Table
Applications of Sorting
1. Searching: Sorted arrays allow for efficient searching
using algorithms like binary search.
2. Data Organization: Sorting is fundamental in
organizing data for processing.
3. Data Compression: Helps in data deduplication and
compression techniques.
4. Databases: Sorting optimizes query performance.
5. Computational Geometry: Sorting is used in solving
problems like finding the convex hull.
6. Machine Learning: Sorting is used for preprocessing
data or ranking results.