3. Searching and sorting
• Searching and sorting are fundamental operations in computer science,
used to efficiently manage and retrieve data. Searching helps locate
specific elements in a dataset, while sorting arranges data in a structured
order, improving search efficiency and data organization.
5. Searching
• Searching is the process of finding a specific element in an array.
• Searching is essential when working with large data in arrays, such as
looking up a phone number or accessing a website.
Types of Searching Techniques:
1.Linear Search – Checks each element one by one.
2.Binary Search – Efficient for sorted arrays, divides data
in half.
6. What is Linear Search?
Linear search is a method to find an element in a list by checking each element
one by one.
•How it works:
•Start from the first element.
•Compare each element with the search key.
•Stop when the key is found or the list ends.
•Key Points:
•Works on unsorted and sorted arrays.
•Suitable for small datasets.
7. Steps of Linear Search
1. Start from the first element (A[0]).
2. Compare each element with the search key.
3. If a match is found:
• Return the index.
1. If no match is found after checking all elements:
• Conclude that the key is not in the list.
9. Example of Linear Search
Array: [10, 20, 30, 40, 50]
Search Key: 30
Steps:
1.Compare 10 with 30. → Not a match.
2.Compare 20 with 30. → Not a match.
3.Compare 30 with 30. → Match found at index 2.
10. Algorithm (Pseudocode)
Input: Array A of size n, Search Key "item"
Output: Index of "item" or -1 if not found
1. Set loc = -1
2. For i = 0 to n-1:
- If A[i] == item:
Set loc = i
Exit loop
3 If loc >= 0:
Display "Item found at index loc"
4. Else:
Display "Item not found"
12. Key Properties
•Time Complexity:
•Best case: O(1) (item is found at the first position).
•Worst case: O(n) (item is at the last position or not present).
•Space Complexity:
•Linear search uses O(1) extra space (no additional memory
needed).
13. Advantages and Disadvantages
Advantages
1.Simple and easy to implement.
2.Works with unsorted arrays.
3.Useful for small datasets.
Disadvantages
1.Inefficient for large datasets.
2.Requires checking every element in the worst case.
3.Slower than algorithms like Binary Search for sorted arrays.
14. Binary Search
• Binary Search: Efficient Searching
• Finding Elements Quickly in Sorted Lists
What is Binary Search?
•A highly efficient algorithm for finding a specific element in a sorted list.
•Works by repeatedly dividing the search interval in half.
•Significantly faster than linear search for large lists.
15. Cont..
• Binary Search - How it Works?
1. Find the Middle: Determine the middle element of the sorted list.
2. Compare: Compare the middle element with the target element.
3. Narrow the Search:
• If the middle element is the target, you're done!
• If the target is less than the middle, search the left half.
• If the target is greater than the middle, search the right half.
4. Repeat: Continue dividing and comparing until the target is found or the
search space is empty.
16. Example
int Binary_Search(int list[], int n, int key) {
int left = 0, right = n - 1;
int mid, index = -1;
while (left <= right) { // Fixed loop condition
mid = (left + right) / 2;
if (list[mid] == key)
return mid; // Return index immediately if found
else if (key < list[mid])
right = mid - 1; // Search left half
else
left = mid + 1; // Search right half
}
return -1; // Not found
}
17. Cont..
• Binary Search - Step-by-Step Example
Searching for 23 in [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]:
18. Cont..
• Binary Search - Time Complexity
•Time complexity: O(log n)
•This means the search time grows logarithmically with the size of the list.
•Much faster than linear search (O(n)) for large lists.
19. Binary Search vs. Linear Search
•Binary Search: Fast, requires a sorted list.
•Linear Search: Slow for large lists, works on unsorted lists.
•For 1000 items, binary search takes around 10 comparisons, linear
search an average of 500.
21. What is Sorting?
• Sorting is the process of arranging items in a specific order
(ascending or descending).
• It's a fundamental operation in computer science.
• Today, we'll cover three basic sorting algorithms: Insertion
Sort, Selection Sort, and Bubble Sort.
22. Insertion Sort
• Insertion Sort: Sorting Like Playing Cards
•Imagine sorting cards in your hand.
•You pick a card and insert it into its correct position within
the already sorted cards.
•Insertion sort does the same thing with a list of numbers.
23. Cont..
Insertion Sort - Step-by-Step Example
Insertion Sort Example: [5, 2, 4, 1, 3]
•Start: [5, 2, 4, 1, 3] (5 is considered sorted)
•Insert 2: [2, 5, 4, 1, 3] (2 is moved before 5)
•Insert 4: [2, 4, 5, 1, 3] (4 is inserted between 2 and 5)
•Insert 1: [1, 2, 4, 5, 3] (1 is moved to the beginning)
•Insert 3: [1, 2, 3, 4, 5] (3 is inserted between 2 and 4)
25. Implementation
for (int i = 1; i < n; i++) {
int key = list[i];
int j = i - 1;
while (j >= 0 && list[j] > key) {
list[j + 1] = list[j];
j--;
}
list[j + 1] = key;
}
26. Selection Sort
• Selection Sort: Finding the Smallest
•Repeatedly find the smallest element from the unsorted part.
•Place it at the beginning of the sorted part.
27. Cont..
• Selection Sort - Step-by-Step Example
•Find 1: [1, 4, 6, 5, 3] (1 is swapped with 6)
•Find 3: [1, 3, 6, 5, 4] (3 is swapped with 4)
•Find 4: [1, 3, 4, 5, 6] (4 is swapped with 6)
•Find 5: [1, 3, 4, 5, 6] (5 is already in place)
Selection Sort Example: [6, 4, 1, 5, 3]
29. Selection Sort - Implementation
void selection_sort(int list[]) {
int i, j, smallest, temp;
for (i = 0; i < n; i++) {
smallest = i;
for (j = i + 1; j < n; j++) {
if (list[j] < list[smallest])
smallest = j;
} // End of inner loop
temp = list[smallest];
list[smallest] = list[i];
list[i] = temp;
} // End of outer loop
} // End of selection_sort
30. Bubble Sort
• Bubble Sort: Comparing and Swapping
•Repeatedly step through the list.
•Compare adjacent elements and swap them if they are in the wrong order.
•Larger elements "bubble" to the end.
33. Implementation
void bubble_sort(int list[]) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = n - 1; j > i; j--) {
if (list[j] < list[j - 1]) {
temp = list[j];
list[j] = list[j - 1];
list[j - 1] = temp;
} // Swap adjacent elements
} // End of inner loop
} // End of outer loop
} // End of bubble_sort
34. Comparing Sorting Algorithms
•Insertion Sort: Good for small lists, efficient for nearly sorted lists.
•Selection Sort: Simple, consistent, but not very efficient.
•Bubble Sort: Very simple, but highly inefficient.