Unit-4 C programming
Unit-4 C programming
Linear Search (also called sequential search) is a simple searching technique where we
check each element one by one in a list or array until we find the desired value (called the
key) or reach the end of the list.
Let:
📘 Algorithm Steps
1. Start
2. Input the array arr[] of size n and the value to search, key.
3. Set index i = 0.
8End
Example :
Linear Search is a basic method to find an element (called the key) in a list or array. You start
from the first element and check each one, one by one. If the current element matches the
key, you stop and return its position. If not, you move to the next element and keep checking
until you reach the end of the array. If you go through the whole array and don't find the key,
then it means the element is not present. It’s easy to use and works well for small lists, but it
can be slow for large ones because it checks each item individually.
Advantages:
Disadvantages:
Binary Search
Binary Search is a searching algorithm used to find the position of a target value within a
sorted list. It works by repeatedly dividing the search range in half.
1. Start
Binary search is an efficient method to find a specific value in a sorted array. The algorithm
works by repeatedly dividing the search range in half. It starts by comparing the target value
to the middle element of the array. If the middle element matches the target, the search is
complete. If the target is greater than the middle value, the search continues in the right half
of the array. If the target is smaller, the search continues in the left half. This process is
repeated until the target is found or the search range becomes empty. Binary search
significantly reduces the number of comparisons needed, making it much faster than linear
search for large datasets. However, it only works if the data is sorted.
Sorting means arranging elements in a specific order — either ascending (small to large) or
descending (large to small).
Sorting is useful because it makes searching, analyzing, and processing data faster and easier.
Selection Sort
Selection Sort is a simple sorting algorithm that works by repeatedly finding the smallest
(or largest) element from the unsorted part of the array and placing it at the beginning of
the sorted part. It divides the array into two parts:
In each step, it selects the minimum from the unsorted part and swaps it with the first
element of that part.
1. Start
2. Repeat for i = 0 to n - 2
a. Set min = i
b. Repeat for j = i + 1 to n - 1
3. End
Selection sort works by repeatedly finding the smallest element from the unsorted part of
the array and placing it at the correct position in the sorted part. The algorithm starts at the
beginning of the array and assumes the first element is the minimum. It then checks the rest
of the array to find if there is any smaller element. If a smaller element is found, it updates
the minimum index. After scanning the entire unsorted part, it swaps the minimum element
found with the current position. This process is repeated for every position in the array
except the last one, as it will automatically be in the correct place by then. The sorted part of
the array grows one element at a time from the beginning, while the unsorted part keeps
shrinking. Even if the array is already sorted, selection sort performs the same number of
comparisons, making it less efficient for large datasets.
Time Complexity of Selection Sort
Best O(n²)
Average O(n²)
Worst O(n²)
2. Always takes the same time even if the array is already sorted.
3. Slower than algorithms like Merge Sort, Quick Sort, or even Insertion Sort.
Bubble Sort
Bubble Sort is a simple comparison-based sorting algorithm that repeatedly compares and
swaps adjacent elements if they are in the wrong order. This process continues until the
entire array is sorted.
It’s called "bubble" sort because larger elements gradually "bubble up" to the end of the
list in each pass.
Algorithm
1. Start
2. Repeat for i = 0 to n - 2
a. Repeat for j = 0 to n - i - 2
4. End
Bubble Sort works by repeatedly comparing each pair of adjacent elements in the array and
swapping them if they are in the wrong order (i.e., the left element is greater than the
right). After the first full pass, the largest element reaches the end of the array. In the next
pass, the second-largest element moves to its correct position, and so on. This continues
until no more swaps are needed, meaning the array is sorted. With each pass, the number of
elements to be checked decreases because the largest elements have already been placed at
the end. While it is easy to implement and understand, bubble sort is inefficient for large
datasets due to the high number of comparisons and swaps, especially if the array is initially
in reverse order.
1. Very slow for large lists due to many unnecessary comparisons and swaps.
3. Less efficient than algorithms like Merge Sort, Quick Sort, or Insertion Sort.
Best O(n)
Average O(n²)
Worst O(n²)
Insertion Sort
Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at
a time by picking elements and inserting them into their correct position in the sorted part
of the array.
Algorithm
1. Start
2. Repeat for i = 1 to n - 1
b. Set j = i - 1
- Decrement j by 1
3. End
Insertion Sort works like sorting playing cards in your hand. You start with one card
(element) and pick the next one. Then, you compare it with the cards already sorted (to its
left) and insert it in the right position by shifting larger elements one step to the right. This
process continues until all elements are placed in their correct positions. The left side of the
array is always sorted, and the algorithm keeps inserting the next element into this sorted
part. It works very efficiently for small or nearly sorted arrays, making only a few shifts in
such cases.
Best O(n)
Average O(n²)
Worst O(n²)
1. Start
3. Set `low = 0`, `high = n`, and define a precision value (e.g., 0.0001)
6. End
This algorithm uses binary search to find the square root of a number n. It starts with a
range from 0 to n (because the square root must lie in this range). In each step, it calculates
the midpoint and squares it.
If mid² < n, then the actual square root lies to the right, so we move the low to mid.
If mid² > n, then the square root lies to the left, so we move the high to mid.
This process continues until the difference between low and high is very small
Program
#include <stdio.h>
int findSquareRoot(int n) {
if (mid * mid == n)
return mid;
low = mid + 1;
} else {
high = mid - 1;
int main() {
int number;
scanf("%d", &number);
if (number < 0) {
} else {
return 0;
Array Reversal
Reversing an array means reordering its elements in the opposite direction, i.e., the first
becomes last, the second becomes second last, and so on.
Algorithm
1. Start
b. Increment start by 1
c. Decrement end by 1
5. End
We use two pointers, one starting from the beginning and one from the end of the array.
We swap the values at these positions and then move the pointers inward (start++, end--).
This continues until the pointers meet or cross. The array is now reversed in-place, meaning
no extra array is needed.
Program
#include <stdio.h>
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move pointers
start++;
end--;
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
reverseArray(arr, n);
return 0;
String Reversal
To reverse a string, we rearrange its characters so that the last character becomes the first,
the second last becomes the second, and so on.
Algorithm
1. Start
b. Increment start by 1
c. Decrement end by 1
5. End
We use two pointers — one from the beginning of the string and one from the end. We
swap the characters at these positions. Then we move the start pointer forward and the end
pointer backward. This continues until both pointers meet or cross. This way, the string gets
reversed in-place (without using extra memory).
#include <stdio.h>
#include <string.h>
int start = 0;
char temp;
while (start < end) {
// Swap characters
temp = str[start];
str[start] = str[end];
str[end] = temp;
// Move pointers
start++;
end--;
int main() {
char str[100];
gets(str);
reverseString(str);
return 0;