Assignment4 Dsa
Assignment4 Dsa
FA23-BCS-090
BCS-3A
How It Works:
Example:
Step-by-step:
Complexity:
Code:
int linearSearch(int arr[], int size, int target) {
if (arr[i] == target)
return i;
Example:
Step-by-step:
→ Mid = 2 → Value = 6
Complexity:
Code:
if (arr[mid] == target)
return mid;
low = mid + 1;
else
high = mid - 1;
}
How It Works:
Example:
27 → 7 (empty) → placed
18 → 8 (empty) → placed
29 → 9 (empty) → placed
Complexity:
Code:
#include <iostream>
hashTable[index] = key;
4. Hashing by Chaining
How It Works:
Example:
15 → 5 → list: [15]
Complexity:
• Worst Case: O(n) → All keys hash to same index (long chain).
Code:
#include <iostream>
#include <list>
5. Bubble Sort
How It Works:
Example:
Array = [5, 2, 9, 1]
Complexity:
Code;
6. Selection Sort
How It Works:
Example:
Complexity:
Code:
int minIndex = i;
minIndex = j;
swap(arr[i], arr[minIndex]);
7. Insertion Sort
How It Works:
Example:
Complexity:
Code:
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
8. Merge Sort
How It Works:
Complexity:
Code:
int i = 0, j = 0, k = left;
arr[k++] = L[i++];
arr[k++] = R[j++];