Shell Sort in ADA
Algorithm | Example | Complexity |
Flowchart
Introduction
• Shell Sort is an in-place comparison-based
sorting algorithm.
• • Improves insertion sort by comparing far-
apart elements first.
• • Reduces the number of shifts, making it
faster for large lists.
Algorithm Idea
• 1. Start with a large gap.
• 2. Sort elements that are gap distance apart.
• 3. Reduce the gap and repeat.
• 4. Finish with a standard Insertion Sort (gap =
1).
Pseudocode
• ShellSort(arr, n):
• gap ← n / 2
• while gap > 0:
• for i ← gap to n-1:
• temp ← arr[i]
• j←i
• while j ≥ gap AND arr[j-gap] > temp:
• arr[j] ← arr[j-gap]
• j ← j - gap
Example
• Initial Array: [23, 12, 1, 8, 34, 54, 2, 3]
• Gap 4: [23, 12, 1, 3, 34, 54, 2, 8]
• Gap 2: [1, 3, 2, 8, 23, 12, 34, 54]
• Gap 1: [1, 2, 3, 8, 12, 23, 34, 54]
Complexity Analysis
• • Best Case: O(n log n)
• • Average Case: O(n log² n)
• • Worst Case: O(n²)
• Space Complexity: O(1)
• Stable: No
Characteristics
• ✅ Faster than insertion sort for large arrays
• ✅ In-place algorithm (uses no extra space)
• ✅ Works well for partially sorted data
• ❌ Not stable
• ❌ Performance depends on gap sequence
Flowchart (Overview)
• Start → Initialize gap = n/2 → While gap > 0 →
• For i = gap to n → Store arr[i] → Shift elements
• → Insert temp → Reduce gap → End