Discrete structures
Discrete structures
DISCRETE STRUCTURES
🔷 Introduction to Sorting
Bubble Sort
Insertion Sort
🔶 1. Bubble Sort
📌 Definition:
Bubble Sort is a comparison-based sorting algorithm in which each pair of adjacent elements is
compared and swapped if they are in the wrong order. This process is repeated until the array is sorted.
🧠 Working Principle:
After each pass, the largest unsorted element "bubbles up" to its correct position.
🔁 Example:
Pass 1:
Pass 2:
Time Complexity:
🧮 Space Complexity:
✅ Pros:
Simple to implement.
Easy to understand.
❌ Cons:
🔶 2. Insertion Sort
📌 Definition:
Insertion Sort is a comparison-based sorting algorithm that builds the final sorted array one element at a
time. It is much like sorting playing cards in your hands.
🧠 Working Principle:
🔁 Example:
Time Complexity:
🧮 Space Complexity:
✅ Pros:
Simple to implement.
❌ Cons:
Still has quadratic time complexity in the average and worst case.