0% found this document useful (0 votes)
2 views

Chapter-6

The document provides an overview of various sorting algorithms including Selection Sort, Bubble Sort, and Insertion Sort, along with their advantages and disadvantages. It also covers searching techniques such as Linear and Binary Search, highlighting their mechanisms and efficiency. Each section includes algorithm principles and example code snippets demonstrating the implementation of these algorithms.

Uploaded by

jojac32799
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter-6

The document provides an overview of various sorting algorithms including Selection Sort, Bubble Sort, and Insertion Sort, along with their advantages and disadvantages. It also covers searching techniques such as Linear and Binary Search, highlighting their mechanisms and efficiency. Each section includes algorithm principles and example code snippets demonstrating the implementation of these algorithms.

Uploaded by

jojac32799
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Searching and Sorting

Ms. Pranali Shah


Assistant Professor
Computer Science and Engineering(AIML)
Outline
Selectionsort
BubbleSort CHAPTER-1
Insertion sort Preprocessor Directives
Linear and Binary Searching Techniques
Selection Sort
• The selection sort is a simple comparison-based sorting algorithm that sorts a collection by
repeatedly finding the minimum (or maximum) element and placing it in its correct
position in the list.

• 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

• Bubble Sort is a simple comparison-based sorting algorithm that repeatedly swaps


adjacent elements if they are in the wrong order.

• This process continues until the array is sorted.


Algorithm Working Principle:

• 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.

Algorithm Working Principle:

• Assume the first element is already sorted.


• Pick the next element and compare it with the sorted part.
• Shift elements in the sorted part to the right to create space for the new element.
• Insert the new element at its correct position.
• Repeat the process for all elements.
Example:
#include <stdio.h>
int main() {
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) { int arr[] = {5, 1, 4, 2, 8};
int key = arr[i]; // The current element to be inserted
int n = sizeof(arr) / sizeof(arr[0]);
int j = i - 1; // Index of the element to compare with
key insertionSort(arr, n);
printf("Sorted array: ");
// Move elements greater than key one position ahead
while (j >= 0 && arr[j] > key) { for (int i = 0; i < n; i++) {
arr[j + 1] = arr[j];
printf("%d ", arr[i]);
j = j - 1;
} }
arr[j + 1] = key; // Insert the key in its correct position
printf("\n");
}
} return 0; }
Linear & Binary Search
Linear Search :

• 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.

• It is also known as sequential search.

• It can be used on both sorted and unsorted data.


Example
#include <stdio.h> int main() {
int numbers[] = {10, 20, 30, 40, 50};
int linearSearch(int arr[], int size, int target) { int result = linearSearch(numbers, 5, 30); // Search for 30
for (int i = 0; i < size; i++) {
if (arr[i] == target) { if (result != -1) {
return i; // Found! printf("30 found at index %d\n", result); // Output: 30
} found at index 2
} } else {
printf("30 not found\n");
return -1; // Not found }
} return 0;
}

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.

• It works by repeatedly dividing the search interval in half.

• If the middle element is the target, the search is complete.

• If the target is less than the middle element, the search continues in the left half.

• If the target is greater, the search continues in the right half.


Example
#include <stdio.h> int main() {

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.

You might also like