Lesson 5.1: Introduction to Sorting Algorithms
Sorting is the process of arranging data in a particular order โ usually ascending or descending. Sorting algorithms are crucial for efficient searching and organizing data for real-time systems, e-commerce filtering, and more.
๐ Key Points
- Sorting helps in efficient searching (e.g., Binary Search).
- Popular algorithms include Bubble Sort, Merge Sort, Quick Sort.
- Some are comparison-based, others are non-comparison based (e.g., Radix Sort).
- Merge Sort and Quick Sort use divide-and-conquer.
- Sorting improves data presentation in UI/UX systems.
- Algorithms vary by stability, space, and time complexity.
- Sorting is often used before binary search and range queries.
๐งช Examples
# Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
# Merge Sort
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
return arr
Lesson 5.2: Searching Algorithms
Searching refers to finding a specific item in a data set. Efficient search methods improve performance and user experience in applications like autocomplete, file lookup, and database queries.
๐ Key Points
- Two main types: Linear Search and Binary Search.
- Linear Search scans all elements sequentially โ O(n).
- Binary Search works on sorted arrays โ O(log n).
- Searching is foundational for many real-time systems.
- Can be applied to arrays, trees, graphs, and more.
- Recursive and iterative binary search are both common.
- Efficient searching improves app responsiveness.
๐งช Examples
# Linear Search
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
# Binary Search
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Lesson 5.3: Real-World Use Cases
Sorting and searching algorithms power critical features in almost every software application. From search engines to logistics and e-commerce filtering, their performance is directly tied to user experience and operational efficiency.
๐ Key Points
- Search engines use advanced searching techniques and indexing.
- Online stores sort products by price, rating, popularity, etc.
- Databases rely on efficient sorting for indexing and joins.
- Route-finding algorithms often rely on sorted data.
- Real-time applications use binary search trees and heaps.
- Sorting helps preprocess data before machine learning analysis.
- Optimized search improves latency in chat, feed, and file apps.
๐งช Examples
# Application: Filter top 5 scores
scores = [82, 91, 78, 95, 88]
top_scores = sorted(scores, reverse=True)[:5]
print(top_scores) # [95, 91, 88, 82, 78]
# Application: Binary search on sorted list
ids = [1001, 1004, 1008, 1010]
print(binary_search(ids, 1004)) # Output: 1