Resources | Subject Notes | Computer Science
This section explores fundamental algorithms used to solve computational problems. We will cover linear search, bubble sort, and their applications.
Linear search is the simplest search algorithm. It involves examining each element in a list sequentially until the target element is found or the end of the list is reached.
How it works:
Pseudocode:
function linearSearch(list, target): for each element in list: if element == target: return "Target found at index" + index of element return "Target not found"
Example:
Consider the list: [5, 2, 9, 1, 5, 6]
and the target value 9
.
The algorithm will compare 5, then 2, then 9. When it reaches 9, it will return "Target found at index 2".
Efficiency:
O(1)
O(n)
, where n
is the number of elements in the list.O(n)
Binary search is a more efficient search algorithm that works on sorted lists. It repeatedly divides the search interval in half.
How it works:
Pseudocode:
function binarySearch(list, target): low = 0 high = length of list - 1 while low <= high: mid = (low + high) / 2 if list[mid] == target: return "Target found at index" + mid else if list[mid] < target: low = mid + 1 else: high = mid - 1 return "Target not found"
Example:
Consider the sorted list: [1, 2, 5, 9, 10]
and the target value 5
.
The algorithm will first check the middle element (which is 5). Since it matches the target, the search is successful.
Efficiency:
O(1)
O(log n)
O(log n)
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The largest element "bubbles" to the end of the list with each pass.
How it works:
Pseudocode:
function bubbleSort(list): n = length of list for i = 0 to n - 2: for j = 0 to n - i - 2: if list[j] > list[j + 1]: swap list[j] and list[j + 1]
Example:
Consider the list: [5, 1, 4, 2, 8]
.
After the first pass, the largest element (8) will be at the end: [5, 1, 4, 2, 8]
becomes [1, 4, 2, 5, 8]
.
After the second pass, the second largest element (5) will be in its correct position: [1, 4, 2, 5, 8]
becomes [1, 4, 2, 5, 8]
.
Efficiency:
O(n)
O(n2)
O(n2)
Selection sort finds the minimum element in the unsorted portion of the list and places it at the beginning.
How it works:
Efficiency:
O(n2)
O(n2)
O(n2)
Insertion sort builds the final sorted list one item at a time. It iterates through the input list, picking an element and inserting it into its correct position in the already sorted portion of the list.
Efficiency:
O(n)
O(n2)
O(n2)
Algorithm | Best Case | Worst Case | Average Case |
---|---|---|---|
Linear Search | O(1) | O(n) | O(n) |
Binary Search | O(1) | O(log n) | O(log n) |
Bubble Sort | O(n) | O(n2) | O(n2) |
Selection Sort | O(n2) | O(n2) | O(n2) |
Insertion Sort | O(n) | O(n2) | O(n2) |