1. Introduction to Sorting
Sorting refers to arranging data in a particular order — usually ascending (smallest to largest) or descending (largest to smallest).
Python has built-in sorting: list.sort() (in-place) and sorted() (returns new list). However, understanding how sorting algorithms work is important for logic building and problem-solving.
2. Bubble Sort
Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The process repeats until no swaps are needed.
Algorithm:
- Compare adjacent elements. If the first is greater than the second, swap them.
- After each pass, the largest element "bubbles up" to its correct position at the end.
- Repeat for the remaining unsorted elements.
Python Implementation:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break # Already sorted
return arr
# Example
nums = [64, 34, 25, 12, 22, 11, 90]
print(bubble_sort(nums)) # [11, 12, 22, 25, 34, 64, 90]Complexity Analysis:
- Time: O(n²) worst case, O(n) best case (when already sorted, with optimization)
- Space: O(1) — in-place sorting
3. Selection Sort
Selection Sort repeatedly finds the smallest element from the unsorted part and puts it at the beginning.
Algorithm:
- Find the minimum element in the unsorted portion.
- Swap it with the first element of the unsorted portion.
- Move the boundary between sorted and unsorted portions one step to the right.
Python Implementation:
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
# Example
nums = [64, 25, 12, 22, 11]
print(selection_sort(nums)) # [11, 12, 22, 25, 64]Complexity Analysis:
- Time: O(n²) in all cases
- Space: O(1) — in-place sorting
4. Insertion Sort
Insertion Sort builds the final sorted array one element at a time. It picks elements from the unsorted part and inserts them into their correct position in the sorted part.
Algorithm:
- Start with the second element (first element is considered sorted).
- Pick the current element and compare it with elements in sorted portion.
- Shift all larger elements one position to the right.
- Insert the current element at its correct position.
Python Implementation:
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
# Example
nums = [12, 11, 13, 5, 6]
print(insertion_sort(nums)) # [5, 6, 11, 12, 13]Complexity Analysis:
- Time: O(n²) worst case, O(n) best case (already sorted)
- Space: O(1) — in-place sorting
5. Comparison of Sorting Algorithms
| Algorithm | Best Case | Worst Case | Average Case | Space | Stable? | In-place? |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No | Yes |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
Note: For CBSE Class 11, focus on understanding the logic and being able to trace through each algorithm step-by-step with small arrays.
6. Revision Questions and Answers
Very Short Answer Questions
1. What is sorting?
Arranging data in a specific order (ascending or descending).
2. Which sorting algorithm has the best case O(n)?
Bubble Sort (optimized) and Insertion Sort.
3. Which sorting algorithm is considered the simplest?
Bubble Sort.
4. What is the primary drawback of Bubble Sort?
It is slow for large datasets — O(n²) time complexity.
5. Which sorting algorithm performs well on nearly sorted data?
Insertion Sort (O(n) best case).
Short Answer Questions
1. Explain the working of Selection Sort with a small example.
Selection Sort finds the minimum element and places it at the beginning. Example: For [64, 25, 12, 22, 11], it finds 11 (minimum), swaps with 64 → [11, 25, 12, 22, 64]. Then finds 12 in remaining, swaps with 25 → [11, 12, 25, 22, 64], and so on.
2. Differentiate between stable and unstable sorting.
A stable sort preserves the relative order of equal elements. Bubble Sort and Insertion Sort are stable. Selection Sort is not stable (it may swap equal elements).
Long Answer Questions
1. Write Bubble Sort algorithm and trace it for the list [5, 1, 4, 2, 8] showing each pass.
Pass 1: [1, 4, 2, 5, 8] — largest element 8 bubbles to end
Pass 2: [1, 2, 4, 5, 8] — 5 bubbles to correct position
Pass 3: [1, 2, 4, 5, 8] — already sorted (optimized version stops)
Implementation: for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: swap
2. Write Insertion Sort algorithm and trace it for the list [12, 11, 13, 5, 6].
Start: [12 | 11, 13, 5, 6] (| separates sorted|unsorted)
i=1, key=11: shift 12 → [11, 12 | 13, 5, 6]
i=2, key=13: already > 12 → [11, 12, 13 | 5, 6]
i=3, key=5: shift 13,12,11 → [5, 11, 12, 13 | 6]
i=4, key=6: shift 13,12,11 → [5, 6, 11, 12, 13]