0% found this document useful (0 votes)
2 views10 pages

Assignment4 Dsa

The document outlines various algorithms including Linear Search, Binary Search, Hashing (Linear Probing and Chaining), and sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort, and Merge Sort). Each algorithm is described with its working mechanism, examples, complexities, and code implementations. The document serves as a comprehensive guide for understanding these fundamental data structure concepts.

Uploaded by

sheezajamshid
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)
2 views10 pages

Assignment4 Dsa

The document outlines various algorithms including Linear Search, Binary Search, Hashing (Linear Probing and Chaining), and sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort, and Merge Sort). Each algorithm is described with its working mechanism, examples, complexities, and code implementations. The document serves as a comprehensive guide for understanding these fundamental data structure concepts.

Uploaded by

sheezajamshid
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/ 10

Shiza Jamshaid

FA23-BCS-090
BCS-3A

Data Structure- CSC211


Class Assignment 3

Submitted to: Dr. Inayat Ur-Rehman


Submission date: June 12,2025
1. Linear Search Algorithm

How It Works:

• Scan each element in the array from start to end.

• Compare each element with the target.

• Stop when found or end of array is reached.

Example:

Array = [5, 3, 8, 4, 2], Target = 4

Step-by-step:

→ Compare 5 with 4 → Not equal

→ Compare 3 with 4 → Not equal

→ Compare 8 with 4 → Not equal

→ Compare 4 with 4 → Match found at index 3

Complexity:

• Best Case: O(1) → Target is at the first position.

• Worst Case: O(n) → Target is at the end or not found.

Code:
int linearSearch(int arr[], int size, int target) {

for (int i = 0; i < size; i++) {

if (arr[i] == target)

return i;

return -1; // Not found

2. Binary Search Algorithm (Only for sorted arrays)


How It Works:

• Find the middle element.

• If it's the target, return index.

• If target < middle, search left half.

• If target > middle, search right half.

Example:

Array = [2, 4, 6, 8, 10], Target = 8

Step-by-step:

→ Mid = 2 → Value = 6

→ 8 > 6 → Search right half [8, 10]

→ Mid = 3 → Value = 8 → Found

Complexity:

• Best Case: O(1) → Target is middle element.

• Worst Case: O(log n) → Divide array in half every time.

Code:

int binarySearch(int arr[], int size, int target) {

int low = 0, high = size - 1;

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == target)

return mid;

else if (arr[mid] < target)

low = mid + 1;

else

high = mid - 1;
}

return -1; // Not found

3. Hashing by Linear Probing

How It Works:

• Use hash function: index = key % table_size

• If collision, probe next slot (index + 1) until empty slot is found.

Example:

Insert keys: [27, 18, 29, 28], Table Size = 10


Hash function: key % 10

27 → 7 (empty) → placed

18 → 8 (empty) → placed

29 → 9 (empty) → placed

28 → 8 (occupied) → check 9 (occupied) → check 0 (empty) → placed at 0

Complexity:

• Best Case: O(1) → No collision.

• Worst Case: O(n) → Many collisions and table is nearly full.

Code:

#include <iostream>

using namespace std;

const int TABLE_SIZE = 10;

void insertLinearProbing(int hashTable[], int key) {

int index = key % TABLE_SIZE;

while (hashTable[index] != -1) {


index = (index + 1) % TABLE_SIZE;

hashTable[index] = key;

4. Hashing by Chaining

How It Works:

• Use hash function: index = key % table_size

• Store colliding elements in a linked list at that index.

Example:

Insert keys: [15, 25, 35], Table Size = 10


Hash function: key % 10

15 → 5 → list: [15]

25 → 5 → list: [15 → 25]

35 → 5 → list: [15 → 25 → 35]

Complexity:

• Best Case: O(1) → No collision.

• Worst Case: O(n) → All keys hash to same index (long chain).

Code:

#include <iostream>

#include <list>

using namespace std;

const int TABLE_SIZE = 10;

void insertChaining(list<int> hashTable[], int key) {

int index = key % TABLE_SIZE;


hashTable[index].push_back(key);

5. Bubble Sort

How It Works:

• Repeatedly swap adjacent elements if they are in the wrong order.

Example:

Array = [5, 2, 9, 1]

1st pass: [2, 5, 1, 9]

2nd pass: [2, 1, 5, 9]

3rd pass: [1, 2, 5, 9] → Sorted

Complexity:

• Best Case: O(n) → Already sorted (with optimization).

• Worst Case: O(n²) → Reverse sorted.

Code;

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

swap(arr[j], arr[j + 1]);

6. Selection Sort

How It Works:

• Find minimum element and place it at beginning.


• Repeat for all positions.

Example:

Array = [29, 10, 14, 37]

Step 1: Min = 10 → Swap with 29 → [10, 29, 14, 37]

Step 2: Min = 14 → Swap with 29 → [10, 14, 29, 37]

Step 3: Already sorted → [10, 14, 29, 37]

Complexity:

• Best Case: O(n²)

• Worst Case: O(n²)

• No advantage for sorted arrays.

Code:

void selectionSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex])

minIndex = j;

swap(arr[i], arr[minIndex]);

7. Insertion Sort

How It Works:

• Build sorted array one element at a time.


• Insert each element into correct position in the sorted part.

Example:

Array = [4, 3, 2, 10]

Step 1: 3 < 4 → [3, 4, 2, 10]

Step 2: 2 < 3 → [2, 3, 4, 10]

Step 3: 10 > 4 → No change → [2, 3, 4, 10]

Complexity:

• Best Case: O(n) → Already sorted.

• Worst Case: O(n²) → Reverse order.

Code:

void insertionSort(int arr[], int size) {

for (int i = 1; i < size; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

8. Merge Sort

How It Works:

• Divide the array into halves recursively.

• Merge sorted halves.


Example:

Array = [38, 27, 43, 3]

Divide: [38, 27], [43, 3]

→ [38], [27] → merge → [27, 38]

→ [43], [3] → merge → [3, 43]

→ merge → [3, 27, 38, 43]

Complexity:

• Best Case: O(n log n)

• Worst Case: O(n log n)

• Stable and efficient for large data.

Code:

void merge(int arr[], int left, int mid, int right) {

int n1 = mid - left + 1;

int n2 = right - mid;

int L[n1], R[n2]; // Temporary arrays

for (int i = 0; i < n1; i++)

L[i] = arr[left + i];

for (int j = 0; j < n2; j++)

R[j] = arr[mid + 1 + j];

// Merge the arrays

int i = 0, j = 0, k = left;

while (i < n1 && j < n2)


arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];

while (i < n1)

arr[k++] = L[i++];

while (j < n2)

arr[k++] = R[j++];

void mergeSort(int arr[], int left, int right) {

if (left < right) {

int mid = left + (right - left) / 2;

mergeSort(arr, left, mid); // Sort first half

mergeSort(arr, mid + 1, right); // Sort second half

merge(arr, left, mid, right); // Merge the sorted halves

You might also like