Total Pageviews
Tuesday, June 2, 2020
BINARY SEARCH ALGORITHM STEP BY STEP
🎯 What is Binary Search?
Binary Search is a fast searching technique used to find an element in a sorted array by repeatedly dividing the search space into half.
📌 Definition
Binary Search is a divide-and-conquer algorithm that finds an element in a sorted list by comparing the target value with the middle element.
⚠️ Important Condition
✔ The array must be sorted (ascending or descending)
❌ Binary search does NOT work on unsorted data
🧠 How Binary Search Works
Steps:
- Find middle element
- Compare middle element with target
- If match → stop
- If target is smaller → search left half
- If target is larger → search right half
- Repeat until found or range becomes empty
📊 Example
Sorted Array:
A = [10, 20, 30, 40, 50, 60, 70]
Target:
Find 50
Step-by-step:
| Step | Low | High | Mid | Value | Action |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 40 | 50 > 40 → right half |
| 2 | 4 | 6 | 5 | 60 | 50 < 60 → left half |
| 3 | 4 | 4 | 4 | 50 | ✅ Found |
💻 Algorithm (Pseudocode)
BinarySearch(A, key)
1. low = 0
2. high = n - 1
3. while low <= high
4. mid = (low + high) / 2
5. if A[mid] == key
6. return mid
7. else if key < A[mid]
8. high = mid - 1
9. else
10. low = mid + 1
11. return -1
🐍 Python Program for Binary Search
def binary_search(arr, key):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == key:
return mid
elif key < arr[mid]:
high = mid - 1
else:
low = mid + 1
return -1
# Example
arr = [10, 20, 30, 40, 50, 60, 70]
key = 50
result = binary_search(arr, key)
if result != -1:
print("Element found at index:", result)
else:
print("Element not found")
Output:
Element found at index: 4
⚡ Time Complexity
| Case | Complexity |
|---|---|
| Best Case | O(1) |
| Worst Case | O(log n) |
| Average Case | O(log n) |
📊 Why Binary Search is Fast?
Each step reduces search space by half:
Example:
- 8 elements → 4 → 2 → 1
So it grows very slowly compared to linear search.
🆚 Binary Search vs Linear Search
| Feature | Linear Search | Binary Search |
|---|---|---|
| Data Requirement | Unsorted | Sorted |
| Speed | Slow (O(n)) | Fast (O(log n)) |
| Method | Sequential | Divide & Conquer |
| Efficiency | Low | High |
📌 Advantages of Binary Search
- Very fast for large datasets
- Reduces comparisons drastically
- Efficient algorithm
❌ Disadvantages
- Requires sorted data
- Not suitable for dynamic/unsorted lists
- More complex than linear search
🧠 Real Life Example
- Finding a word in dictionary 📖
- Searching a name in sorted telephone directory 📞
📌 Summary
- Binary search works only on sorted data.
- It divides the list into halves repeatedly.
- It is much faster than linear search.
- Time complexity is O(log n).
❓ Important Questions
Short Questions
- What is binary search?
- What is the condition for binary search?
- What is time complexity of binary search?
- What is mid element?
- Give one real-life example.
Long Questions
- Explain binary search with example.
- Write algorithm of binary search.
- Write Python program for binary search.
- Compare linear and binary search.
- Explain time complexity of binary search.
LINEAR SEARCH ALGORITHM STEP BY STEP
🎯 What is Linear Search?
Linear Search is a simple searching technique used to find an element in a list or array by checking each element one by one from the beginning to the end.
📌 Definition
Linear Search is a sequential searching algorithm where each element is compared with the target value until a match is found or the list ends.
🧠 How Linear Search Works
- Start from the first element
- Compare each element with the target value
- If match found → Stop
- If not found → Move to next element
- Continue until end of list
📊 Example
Array:
A = [10, 25, 30, 45, 60]
Target:
Find 30
Step-by-step Process:
| Step | Element Checked | Result |
|---|---|---|
| 1 | 10 | Not match |
| 2 | 25 | Not match |
| 3 | 30 | ✅ Match found |
💻 Algorithm (Pseudocode)
LinearSearch(A, n, key)
1. for i = 0 to n-1
2. if A[i] == key
3. return i
4. return -1
🐍 Linear Search Program (Python)
def linear_search(arr, key):
for i in range(len(arr)):
if arr[i] == key:
return i
return -1
# Example
arr = [10, 25, 30, 45, 60]
key = 30
result = linear_search(arr, key)
if result != -1:
print("Element found at index:", result)
else:
print("Element not found")
Output:
Element found at index: 2
⚡ Best Case, Worst Case
| Case | Condition | Complexity |
|---|---|---|
| Best Case | Element at first position | O(1) |
| Worst Case | Element at last or not present | O(n) |
| Average Case | Random position | O(n) |
📌 Advantages of Linear Search
- Very simple to understand
- Works on unsorted data
- No extra memory required
❌ Disadvantages
- Slow for large data sets
- Not efficient compared to binary search
🆚 Linear Search vs Binary Search
| Feature | Linear Search | Binary Search |
|---|---|---|
| Data Requirement | Unsorted | Sorted |
| Speed | Slow | Fast |
| Method | Sequential | Divide & Conquer |
🧠 Real Life Example
- Searching a name in attendance register
- Finding a contact in phone list (unsorted)
📌 Summary
- Linear search checks elements one by one.
- It is simple but inefficient for large data.
- Best used for small or unsorted datasets.
❓ Important Questions
Short Questions
- What is linear search?
- What is time complexity of linear search?
- Give one advantage of linear search.
- Is sorting required in linear search?
- Write one real-life example.
Long Questions
- Explain linear search with example.
- Write algorithm of linear search.
- Write a program for linear search.
- Compare linear and binary search.
- Explain best and worst case of linear search.
GRAPH THEORY ALGORITHMS
BREADTH FIRST SEARCH ( BFS) ALGORITHM IMPLEMENTATION STEP BY STEP
DEPTH FIRST SEARCH ( DFS) ALGORITHM IMPLEMENTATION STEP BY STEP
Wednesday, May 27, 2020
Monday, May 25, 2020
java theory question set 4
| SL. NO | QUESTION | MARKS | PAGE NO | LINK1 | LINK2 | LINK3 |
| 1. | PRIMITIVE ,NON PRIMITIVE DATA TYPES | 2 | 46 | |||
| 2. | SIZE AND RANGE OF DATA TYPES | 2 | 48 | |||
| 3. | SCOPE OF VARIABLES | 2 | 51 | |||
| 4. | TYPE CASTING | 2 | 53 | |||
| 5. | AUTO TYPE CASTING | 2 | 53 | |||
| 6. | DEFAULT VALUE OF DATA TYPE | 2 | 57 |
java theory question set 3
| SL. NO | QUESTION | MARKS | PAGE NO | LINK1 | LINK2 | LINK3 |
| 1. | WHY MAIN() IS STATIC | 2 | 26 | |||
| 2. | TOKEN | 2 | 30 | |||
| 3. | LITERALS | 33 | ||||
| 4. | SEPARATORS | 34 | ||||
| 5. | CLASSIFICATION OF JAVA STATEMENTS | 36 | ||||
| 6. | JAVA VIRTUAL MACHINE | 39 | ||||
| 7. | COMMAND LINE ARGUMENT | 40 |
java theory question set 2
| SL. NO | QUESTION | MARKS | PAGE NO | LINK1 | LINK2 | LINK3 |
| 1. | COMPILER AND INTERPRETER | 4 | 13 | |||
| 2. | BYTECODE | 2 | 13 | |||
| 3. | JVM | |||||
| 4. | COMPARE WITH C AND C++ | 3 | 15 | |||
| 5. | JAVA DEVELOPMENT TOOLS | TABLE 2.3 | ||||
| 6. | APPLICATION PROGRAMMING INTERFACE | 22 |
java theory question set 1
| SL. NO | QUESTION | MARKS | PAGE NO | LINK1 | LINK2 | LINK3 |
| 1. | DATA ABSTRACTION | 2 | 4 | |||
| 2. | ENCAPSULATION | 2 | 4 | |||
| 3. | INHERITANCE | 2 | 4 | |||
| 4. | POLYMORPHISM | 2 | 5 | |||
| 5. | DYNAMIC BINDING | 2 | 6 | |||
| 6. | MESSAGE COMMUNICATION | 2 | 6 | |||
| 7. | BENEFIT OF OOPS | 3 | 8 |
Tuesday, May 19, 2020
COMPUTER ARCHITECTURE LAB ASSIGNMENT
| 1. | LDA , STA instruction IN COMPUTER ARCHITECTURE | LINK |
| 2. | addition of two hexadecimal numbers | LINK |
| 3. | addition of three hexadecimal numbers | LINK |
| 4. | 1'S COMPLEMENT OF A NUMBER | LINK |
| 5. | 2'S COMPLEMENT OF A NUMBER | LINK |
| 6. | SUBTRACTION of two hexadecimal number | LINK |
| 7. | LDA DIRECT MODE , INDIRECT MODE | LINK |
| 8. | STA direct mode indirect mode | LINK |
| 9. | ADD DIRECT MODE INDIRECT MODE | LINK |
| 10. | AND INSTRUCTION DIRECT INDIRECT MODE | LINK |
| 11. | OR OPERATION USING AND INSTRUCTION | LINK |
| 12. | ADDITION OF TWO DECIMAL ADDITION | LINK |
| 13. | CIR INSTRUCTION | LINK |
| 14. | CIL INSTRUCTION | LINK |
| 15. | CLE,CME INSTRUCTION | LINK |
| 16. | 4TIMES CIL | LINK |
| 17. | 4TIMES CIR | LINK |
| 18. | BUN INSTRUCTION | LINK |
| 19. | ISZ INSTRUCTION | LINK |
| 20. | SPA INSTRUCTION | LINK |
| 21. | SNA INSTRUCTION | LINK |
| 22. | SZA INSTRUCTION | LINK |
| 23. | SZE INSTRUCTION | LINK |