0% found this document useful (0 votes)
12 views23 pages

Lec 2

The document provides an overview of search algorithms, specifically Linear and Binary Search, detailing their definitions, steps, time complexities, advantages, and disadvantages. It also covers sorting algorithms like Bubble Sort and Insertion Sort, explaining their processes, complexities, and efficiency. The importance of these algorithms in data retrieval and processing is emphasized throughout.

Uploaded by

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

Lec 2

The document provides an overview of search algorithms, specifically Linear and Binary Search, detailing their definitions, steps, time complexities, advantages, and disadvantages. It also covers sorting algorithms like Bubble Sort and Insertion Sort, explaining their processes, complexities, and efficiency. The importance of these algorithms in data retrieval and processing is emphasized throughout.

Uploaded by

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

DATA STRUCTURE

Introduction to Searching
•What is a Search Algorithm ?

A method for finding a target value within a data structure (e.g., array, list).

•Why Are They Important?

Essential for data retrieval and processing in computer science.

•Comm on Types:

❑Linear (Sequential) Search

❑Binary (Divide-and-Conquer) Search


Linear (Sequential ) Search
❑As the name suggests, the sequential searching operation traverses through each element of the

data sequentially to look for the desired data. The data need not be in a sorted manner for this type

of search

❑Checks every element in a collection sequentially until the target is found or the list ends.
Linear (Sequential ) Search
❑Algorithm Steps:

1. Start at the first element.

2. Compare each element with the target.

3. If a match is found, return the index.

4. If no match is found after checking all elements, return -1.


Linear (Sequential ) Search
❑Algorithm Steps:
Linear (Sequential ) Search
❑ Time Complexity:
Worst-case: O(n) (checks every element)
❑ Advantages:
▪ Simple to implement
▪ No need for sorted data
❑ Disadvantages:

▪ Inefficient for large datasets


▪ Performance degrades linearly with data size
Binary Search

❑Binary Search is a searching algorithm for finding an element's position in a sorted


array.
❑In this approach, the element is always searched in the middle of a portion of an
array.
Binary Search
Binary Search
❑Algorithm Steps:
1. Divide the search space into two halves by finding the middle index “mid”.
2. Compare the middle element of the search space with the key.
3. If the key is found at middle element, the process is terminated.
4. If the key is not found at middle element, choose which half will be used as the next search
space.
❑ If the key is smaller than the middle element, then the left side is used for next search.
❑ If the key is larger than the middle element, then the right side is used for next search.
5. This process is continued until the key is found or the total search space is exhausted.
Binary Search
❑Time Complexity:
O(log n) (search space halves each iteration)
❑Advantages:
Extremely efficient for large, sorted datasets
❑Disadvantages:
▪ Requires data to be sorted
▪ More complex to implement than linear search
Binary Search
Binary Search
SORTING ALGORITHMS
• Definition:
Sorting is the process of rearranging items in a list or array in a particular order (e.g., ascending or
descending).
• Importance:
Sorted data improves efficiency for searches and other operations.
• Common Algorithms:
Bubble Sort, Quick Sort, Merge Sort, Insertion Sort
Bubble Sort

• Key Idea:
• Adjacent elements are compared and swapped if they are in the wrong order, “bubbling” the
largest element to the top on each pass.
Bubble Sort
Bubble Sort
1. Start at the beginning of the array.
2. Compare adjacent elements:
• If the element on the left is greater than the one on the right, swap them.
3. Continue through the array:
• Perform comparisons for every pair in the current pass.
4. Repeat the process:
• After each pass, the largest unsorted element moves to its correct position at the end.
5. Terminate:
• When a pass is completed without any swaps, the array is sorted.
Bubble Sort
❑Time Complexity:

• Worst-case: O(n²)

❑Advantages:

• Easy to understand and implement

❑Disadvantages:

• Inefficient for large datasets due to quadratic time complexity


INSERTION SORT

• Basic Idea:
• It works similarly to how you might sort playing cards in your hand.
INSERTION SORT
❑ How Insertion Sort Works
1. Start with the first element:
• Consider it as the sorted portion.
2. Take the next element (key):
• Compare it with elements in the sorted portion.
3. Shift elements:
• Move elements that are larger than the key to the right.
4. Insert the key:
• Place the key in its correct position.
5. Repeat:
• Continue until the entire array is sorted.
INSERTION SORT
INSERTION SORT

❑ Time Complexity:
❑ Worst-case: O(n²) (e.g., reverse-sorted array)
❑ Best-case: O(n) (e.g., already sorted array)
INSERTION SORT

❑ Advantages:
➢ Simple to implement and understand
➢ Efficient for small or nearly sorted arrays
➢ Adaptive: Performs well if the array is partially sorted
❑ Disadvantages:
▪ Inefficient for large datasets due to quadratic time complexity in the worst-case
▪ High number of shifts compared to more advanced algorithms

You might also like