Chapter-6
Chapter-6
• It is very simple to implement and is preferred when you have to manually implement the
sorting algorithm for a small amount of dataset.
Algorithm Working Principle:
• Start from the first element and assume it as the minimum.
• Compare this element with the remaining elements in the array.
• If a smaller element is found, update the minimum index.
• Swap the minimum element with the first element of the unsorted part.
• Move the boundary of the sorted part one step forward.
• Repeat the process until the entire array is sorted.
Example
#include <stdio.h> int main() {
void selectionSort(int arr[], int n) { int arr[] = {5, 1, 4, 2, 8};
for (int i = 0; i < n - 1; i++) { int n = sizeof(arr) / sizeof(arr[0]);
int min_idx = i; selectionSort(arr, n);
for (int j = i + 1; j < n; j++) { printf("Sorted array: ");
if (arr[j] < arr[min_idx]) { for (int i = 0; i < n; i++) {
min_idx = j; printf("%d ", arr[i]);
} }
} printf("\n");
// Swap if a smaller element was found return 0;
if (min_idx != i) { }
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp; Output :
} Sorted array : 1 2 4 5 8
}
}
Advantages & Disadvantages of selection sorting
Advantages :
• Simple and easy to implement
• Performs well on small datasets
• Works well when memory space is limited
Disadvantages :
• for large datasets
• Not a stable sorting algorithm
Bubble Sort
• Start from the first element and compare it with the next.
• If the first element is greater than the second, swap them.
• Move to the next pair and repeat step 2.
• Continue this process until the largest element is placed at the last position.
• Repeat the process for the remaining unsorted elements.
• The process stops when no more swaps are required.
Example
#include <stdio.h> int main() {
int arr[] = {5, 1, 4, 2, 8};
void bubbleSort(int arr[], int n) { int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) { bubbleSort(arr, n);
if (arr[j] > arr[j + 1]) {
// Swap if the current element is greater than the next printf("Sorted array: ");
int temp = arr[j]; for (int i = 0; i < n; i++) {
arr[j] = arr[j + 1]; printf("%d ", arr[i]);
arr[j + 1] = temp; }
} printf("\n");
}
} return 0;
} }
Output : 1 2 4 5 8
Advantages & Disadvantages of bubble sort
Advantages :
• Simple and easy to understand
• Stable sorting algorithm
• Efficient for nearly sorted data (with optimized version)
Disadvantages :
• Inefficient for large datasets
• Requires many swaps, making it slower in practice
Insertion Sort
• Insertion Sort is a simple sorting algorithm that builds the sorted list one element at a time
by taking each element and inserting it into its correct position.
• Linear search is a simple searching algorithm that checks each element of the array
sequentially until a match is found or the end of the array is reached.
Output :
30 found at index 2
Linear & Binary Search (conti..)
Binary Searching :
• Binary search is a much more efficient search algorithm, but it requires the data to be sorted.
• If the target is less than the middle element, the search continues in the left half.
int binarySearch(int arr[], int size, int target) { int arr[] = {2, 5, 7, 8, 11, 12};
int low = 0, high = size - 1, mid; int target = 11;
while (low <= high) { int index = binarySearch(arr, 6, target);
mid = low + (high - low) / 2; if (index != -1) printf("Found at index: %d\n", index);
if (arr[mid] == target) return mid; else printf("Not found\n");
if (arr[mid] < target) low = mid + 1;
else high = mid - 1; return 0;
}
}
return -1;
} Output :
Found at index: 4
Difference between Linear and Binary Search
Linear Search Binary Search
Data Requirement: Works on both sorted Data Requirement: Requires the data to be
and unsorted arrays or lists. sorted in ascending or descending order.
Search Mechanism: Sequentially checks each Search Mechanism: Repeatedly divides the
element of the array from the beginning search interval in half.Compares the target
until the target element is found or the end element with the middle element of the
of the array is reached. interval.
Time Complexity: In the worst case, it has to Time Complexity: It significantly reduces
examine all 'n' elements. the search space with each step, making it
much faster for large datasets.
Efficiency: Less efficient for large datasets. Efficiency: Highly efficient for large, sorted
datasets.