faizan hw os algorithm
faizan hw os algorithm
Name:faizanullah "mimlawal"
f/N:Ahsanullah
Dep: software
Class:4th 7th semester
Teacher: sir Rahmanullah "shirzad"
Ph: 0789354632
1st assignment of algorithm subject.
## 1. Definitions
- **Algorithm**: A step-by-step procedure or formula for solving a problem. It
consists of a finite sequence of well-defined instructions to achieve a specific goal.
- **Algorithm (repeated)**: This appears to be a repetition of the first term. It
would still refer to the same concept as above.
- **Based on Purpose**:
- Searching Algorithms
- Sorting Algorithms
- Graph Algorithms
- **Based on Complexity**:
- Polynomial Time Algorithms
- Exponential Time Algorithms
1. **Comments**:
- Comments in code are annotations used to explain what the code is doing,
making it easier for others (or yourself) to understand later. In many
programming languages, comments begin with `//` and continue until the end of
the line. For example:
```
// This is a single-line comment
int x = 5; // This initializes x to 5
```
2. **Blocks**:
- Blocks of code are indicated with matching braces `{}`. A block can contain a
collection of simple statements and is often used to define the body of functions
or control structures (like loops and conditionals). For example:
```
if (x > 0) {
// This block runs if x is positive
print("Positive");
}
```
3. **Identifiers**:
- An identifier is a name given to a variable, function, or any other user-defined
item. Identifiers begin with a letter (A-Z, a-z) or an underscore (_) and may be
followed by letters, digits (0-9), or underscores. The data types of variables are
not explicitly declared in some languages and can be inferred from the context.
For example:
```
int count; // 'count' is an identifier of type integer
```
- Data types can include simple types like integer, float, char, boolean, etc.
Compound data types can be formed using structures or records. Here’s an
example of a record:
```
node = record {
datatype data1;
datatype data2;
link next; // Pointer to the next node
}
```
1. **Factorial Calculation**:
```
Function Factorial(n)
If n == 0 then
Return 1
Else
Return n * Factorial(n - 1)
End Function
```
2. **Fibonacci Sequence**:
```
Function Fibonacci(n)
If n <= 1 then
Return n
Else
Return Fibonacci(n - 1) + Fibonacci(n - 2)
End Function
```
3. **Tower of Hanoi**:
```
Function TowerOfHanoi(n, source, target, auxiliary)
If n == 1 then
Move disk from source to target
Else
TowerOfHanoi(n - 1, source, auxiliary, target)
Move disk from source to target
TowerOfHanoi(n - 1, auxiliary, target, source)
End Function
```
4. **Binary Search**:
```
Function BinarySearch(arr, left, right, target)
If left > right then
Return -1
mid = (left + right) / 2
If arr[mid] == target then
Return mid
Else if arr[mid] > target then
Return BinarySearch(arr, left, mid - 1, target)
Else
Return BinarySearch(arr, mid + 1, right, target)
End Function
```
2. **Binary Search**:
- Time Complexity: O(log n)
- Space Complexity: O(1) (in-place search)
3. **Bubble Sort**:
- Time Complexity: O(n^2)
- Space Complexity: O(1) (in-place sorting)
4. **Merge Sort**:
- Time Complexity: O(n log n)
- Space Complexity: O(n) (requires additional space for merging)