Programming (Topic 6 and Topic 7) Flashcards
All Searching Types, Coding etc. (21 cards)
What is Binary Search?
A search algorithm for sorted lists that divides the list into halves to find a target.
How does Binary Search work?
- Compare the middle element to the target.
- Search left or right based on the target.
- Repeat.
What is Linear Search?
A search algorithm that checks each element one by one until the target is found or list ends.
When is Linear Search useful?
For small or unsorted lists.
How does Bubble Sort work?
- Compare two adjacent items.
- Swap if wrong order.
- Repeat for the whole list.
What is the main disadvantage of Bubble Sort?
It is slow for large lists because it repeatedly checks all elements.
How does Insertion Sort work?
- Start with one item as “sorted.”
- Insert the next item into its correct position.
- Repeat.
How does Merge Sort work?
- Divide the list into smaller sublists.
- Merge the sublists back in sorted order.
What is a While Loop?
A loop that runs as long as a condition is true.
Example: while x < 5: print(x)
What is a For Loop?
A loop that iterates over a sequence (list or range).
Example: for i in range(5): print(i)
What is an If Statement?
A decision-making construct that runs code based on a condition.
Example: if x > 0: print(“Positive”)
What is an Array?
A collection of items stored in a single variable (lists in Python).
Example: numbers = [1, 2, 3]
What is a Function?
A reusable block of code designed for a specific task.
Example: def greet(name): return f”Hello, {name}!”
What is a Subroutine?
Another term for a function or method used for specific tasks in a program.
What are Parameters in Functions?
Variables in the function header to take inputs.
Example: def add(x, y): return x + y
What is Basic File Handling?
Operations to read from and write to files in Python. Modes: “r” (read), “w” (write), “a” (append).
How do you write to a file in Python?
with open(“file.txt”, “w”) as file: file.write(“Hello, world!”)
How do you read a file in Python?
with open(“file.txt”, “r”) as file: content = file.read()
What is SQL?
Structured Query Language, used to manage and query databases.
What are common SQL commands?
SELECT (retrieve), INSERT (add), UPDATE (modify), DELETE (remove).
How do you connect Python to a database?
Use sqlite3 for simple databases:
import sqlite3; conn = sqlite3.connect(“example.db”)