dsa_notes
dsa_notes
Searching algorithm is the process of finding the existence of a specific element, within
an array, list, or tree. It is a fundamental operation which is used to retrieve data efficiently.
Types of Searching Algorithms
1. Linear Search 2. Binary Search
1. Linear Search : Linear Search is a basic searching algorithm that checks each element
of the data structure sequentially until the desired element is found.
Working of Linear Search :
i) Start from the first element.
ii) Compare each element with the key.
iii) If a match is found, return its position.
iv) If no match is found after checking all elements, return "Not Found".
Time Complexity :
• Best Case: O(1) → if key is the first element
• Worst Case: O(n) → if key is the last element or not present
• Space Complexity: O(1)
Prog.
#include <stdio.h>
void linearSearch(int arr[], int n, int key) {
int i;
void main() {
int n, key, i;
int arr[n];
Time Complexities
if (arr[mid] == key) {
printf("Element found at index %d (position %d).\n", mid, mid + 1);
return;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
// If not found
printf("Element not found in the array.\n");
}
void main() {
int n, key;
Sorting Algorithms:
Sorting algorithms are methods used to arrange the elements of a list or array in a specific
order — typically ascending or descending.
Sorting helps in organizing data and makes searching and other operations more efficient.
1. Bubble Sort : Bubble Sort is a simple sorting algorithm that works by repeatedly
swapping adjacent elements if they are in the wrong order.
It continues doing this until the entire array is sorted.
Working Process of Bubble Sort :
Example
Array: 5 3 8 4 2
• Pass 1: 3 5 4 2 8
• Pass 2: 3 4 2 5 8
• Pass 3: 3 2 4 5 8
• Pass 4: 2 3 4 5 8 =➔Sorted!
Time Complexity
Average O(n²)
Worst O(n²)
Prog.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
swapped = 1;
}
}
void main() {
int n, i;
int arr[n];
2. 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 swapping it
with the first unsorted element.
It divides the array into two parts:
• Sorted part (built from the front)
• Unsorted part (remaining elements)
Working Process of Selection Sort :
Example
Array: 29, 10, 14, 37, 14
• Pass 1: Find minimum → 10 → Swap with 29 → 10, 29, 14, 37, 14
• Pass 2: Min is 14 → Swap with 29 → 10, 14, 29, 37, 14
• Pass 3: Min is 14 → Swap with 29 → 10, 14, 14, 37, 29
• Pass 4: Min is 29 → Swap with 37 → 10, 14, 14, 29, 37 ➔ Sorted
Time Complexity :
Case Time Complexity
Best O(n²)
Average O(n²)
Worst O(n²)
Prog.
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
// Swap the found minimum element with the first unsorted element
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
void main() {
int n, i;
int arr[n];
3. Insertion Sort : Insertion Sort is a sorting algorithm in which elements are picked
one by one and inserted into their correct position in the already sorted part of the
array.
It maintains a sorted subarray on the left side and gradually inserts each new element
into its correct place.
Working Process of Insertion Sort :
Example
Array: 5, 3, 4, 1
• Pass 1: 3 inserted before 5 → 3, 5, 4, 1
• Pass 2: 4 inserted between 3 and 5 → 3, 4, 5, 1
• Pass 3: 1 inserted at the beginning → 1, 3, 4, 5 ➔ Sorted
Time Complexity
O(n) (already
Best
sorted)
Average O(n²)
Worst O(n²)
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, j, key;
void main() {
int n, i;
int arr[n];
// Input elements
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
Example
Array: 6, 3, 8, 2
1. Divide: [6, 3] and [8, 2]
2. Divide again: [6] [3] [8] [2]
3. Merge: [3, 6] and [2, 8]
4. Final merge: [2, 3, 6, 8] ➔ Sorted
Time Complexity :
Time
Case
Complexity
Prog.
#include <stdio.h>
void main() {
int n, i;
int arr[n];
5. Quick Sort : Quick Sort is a fast and efficient divide and conquer sorting algorithm
that works by selecting a pivot element, partitioning the array around the pivot, and
then recursively sorting the left and right subarrays.
Working Process of Quick Sort :
Choose a pivot element from the array (commonly first, last, or middle element).
Partition the array:
Place elements smaller than pivot on the left,
Place elements greater than pivot on the right.
Recursively apply Quick Sort on the left and right parts.
The array becomes sorted after all partitions are complete.
Example
Array: 5, 3, 8, 4, 2
• Choose pivot = 2
→ Partition: [2] | [3, 8, 4, 5]
• Choose pivot = 3
→ Partition: [2, 3] | [4, 8, 5]
• Continue recursively
→ Final sorted: [2, 3, 4, 5, 8]
Time Complexity
Worst O(n²) → happens when array is already sorted and poor pivot is chosen
#include <stdio.h>
void main() {
int n;
int arr[n];
Q. What is a Heap?
Ans : A heap is a special binary tree:
o Max Heap: Each parent node is greater than or equal to its children.
o Min Heap: Each parent node is less than or equal to its children.
• In Heap Sort, we use a Max Heap to get the largest element at the root.
Working Process of Heap Sort :
Example (Array):
Input: 4, 10, 3, 5, 1
1. Build Max Heap → 10, 5, 3, 4, 1
2. Swap 10 with 1 → 1, 5, 3, 4, 10
3. Heapify again → 5, 4, 3, 1, 10
4. Repeat...
Final sorted array: 1, 3, 4, 5, 10
Time Complexity
Time
Case
Complexity
Prog .
#include <stdio.h>
void main() {
int n;
int arr[n];