Data Structures andAlgorithms - Algorithms with Examples and Applications
1. String Operations
1. String Length:
- Input: A string str[]
- Initialize a counter = 0
- While str[counter] != '0', increment counter
- Output: counter
2. String Concatenation:
- Input: Two strings str1[] and str2[]
- Traverse str1[] to the end
- Append characters of str2[] to str1[]
3. String Reverse:
- Input: A string str[]
- Use two pointers, start = 0, end = length - 1
- Swap str[start] and str[end] until start < end
Applications: Used in compiler design, pattern matching, and data validation.
2. Linear Search
Algorithm:
- Input: Array arr[], size n, key
- For i = 0 to n-1:
If arr[i] == key:
Return i
- If not found, return -1
Time Complexity: O(n)
Applications: Used in small or unsorted datasets.
2.
Data Structures andAlgorithms - Algorithms with Examples and Applications
3. Binary Search
Algorithm:
- Input: Sorted array arr[], size n, key
- Initialize: low = 0, high = n-1
- While low <= high:
mid = (low + high) / 2
If arr[mid] == key: return mid
If key < arr[mid]: high = mid - 1
Else: low = mid + 1
Time Complexity: O(log n)
Applications: Efficient search in databases, libraries.
4. Selection Sort
Algorithm:
- Input: Array arr[] of size n
- For i = 0 to n-1:
min_idx = i
For j = i+1 to n:
If arr[j] < arr[min_idx]: min_idx = j
Swap arr[i] and arr[min_idx]
Time Complexity: O(n^2)
Applications: Small dataset sorting, teaching algorithm logic.
5. Singly Linked List
Operations:
3.
Data Structures andAlgorithms - Algorithms with Examples and Applications
- Insert at End:
Create a node, traverse to end, link it
- Delete by Value:
Find node with value, unlink and free memory
- Display:
Traverse and print all nodes
- Search:
Traverse and return index of matching node
Applications: Dynamic memory allocation, real-time systems.
6. Stack (Array)
Operations:
- Push(x):
If top == size - 1: Overflow
Else: stack[++top] = x
- Pop():
If top == -1: Underflow
Else: return stack[top--]
- Display:
Print from top to 0
Applications: Recursion, expression evaluation, syntax parsing.
7. Warshall's Algorithm
4.
Data Structures andAlgorithms - Algorithms with Examples and Applications
Algorithm:
- Input: Adjacency matrix adj[][] of graph with n vertices
- path[i][j] = adj[i][j]
- For k = 0 to n-1:
For i = 0 to n-1:
For j = 0 to n-1:
path[i][j] = path[i][j] || (path[i][k] && path[k][j])
Applications: Transitive closure in graphs.
8. Binary Search Tree
Operations:
- Insert(x):
Traverse from root to leaf and insert maintaining BST
- Inorder(): Left -> Root -> Right
- Preorder(): Root -> Left -> Right
- Postorder(): Left -> Right -> Root
Applications: Sorted data storage, search-intensive applications.
9. Array Operations
Operations:
- Insert at pos i:
Shift elements right, arr[i] = new_value
- Delete at pos i:
Shift elements left
5.
Data Structures andAlgorithms - Algorithms with Examples and Applications
- Traverse:
Print all elements
Applications: Static data storage.
10. Bubble Sort
Algorithm:
- For i = 0 to n-1:
For j = 0 to n-i-1:
If arr[j] > arr[j+1]: swap
Time Complexity: O(n^2)
Applications: Educational purposes.
11. Insertion Sort
Algorithm:
- For i = 1 to n-1:
key = arr[i], j = i-1
While j >= 0 and arr[j] > key:
arr[j+1] = arr[j], j--
arr[j+1] = key
Time Complexity: O(n^2)
Applications: Good for nearly sorted arrays.
12. Linear Queue
6.
Data Structures andAlgorithms - Algorithms with Examples and Applications
Operations:
- Enqueue(x):
If rear == size - 1: Overflow Else:
queue[++rear] = x
- Dequeue():
If front > rear: Underflow Else:
return queue[front++]
- Display():
Print elements from front to rear
Applications: Task scheduling.
13. Circular Queue
Operations:
- Enqueue(x):
If (rear + 1) % size == front: Overflow
Else: rear = (rear + 1) % size, queue[rear] = x
- Dequeue():
If front == -1: Underflow
Else: front = (front + 1) % size
Applications: CPU and disk scheduling.
14. Floyd-Warshall Algorithm
Algorithm:
- Input: Cost matrix dist[][] of graph with n vertices
7.
Data Structures andAlgorithms - Algorithms with Examples and Applications
- For k = 0 to n-1:
For i = 0 to n-1:
For j = 0 to n-1:
If dist[i][j] > dist[i][k] + dist[k][j]:
dist[i][j] = dist[i][k] + dist[k][j]
Applications: All pairs shortest paths.
15. Graph using Adjacency Matrix
Algorithm:
- Input: n vertices
- For i = 0 to n-1:
For j = 0 to n-1:
Input 1 if edge exists from i to j, else 0
Applications: Dense graph representation.