Sortingn And Searching Flashcards
What is a linear search most effective for?
Small or unsorted lists where simplicity is more important than speed.
What is the time complexity of a linear search?
O(n) — it checks each item until the target is found or the end is reached.
What is the time complexity of a binary search?
O(log n) — the list is halved with each step, making it much faster than linear search for large datasets.
What is the main drawback of binary search?
The data must be sorted beforehand — if not, it won’t work correctly.
What does a sort algorithm do?
It arranges data into a specific order — usually ascending or descending.
What are the steps of the bubble sort algorithm?
- Compare two adjacent items
- Swap them if they are in the wrong order
- Repeat until no swaps are needed (the list is sorted)
What is the worst-case time complexity of bubble sort?
O(n²) — very inefficient for large lists.
What are the steps of merge sort?
- Divide the list into halves
- Recursively sort each half
- Merge the sorted halves together
What is the time complexity of merge sort?
O(n log n) — efficient for large lists.
What are the pros and cons of merge sort?
• Pros: Fast and reliable for large lists
• Cons: Uses more memory and can be harder to implement
Why is understanding sorting and searching algorithms important in Computer Science?
Because they are fundamental to handling data efficiently and are used in many real-world applications (e.g., search engines, databases).