Shell sort
…
OUTLINE
• 1 Introduction to Shell Sort
• 2 How Shell Sort Works
• 3 Steps of Shell Sort
• 4 Example
• 5 Time Complexity of shell sort
• 6 Advantages and Disadvantages
• 7 Conclusion
INTRODUCATION
• Shell sort is an efficient sorting algorithm.
• Also known as Shell's method or the diminishing increment sort.
• Invented by Donald Shell in 1959.
• It's an improvement over insertion sort.
How Shell Sort Works
• Shell Sort divides the input into smaller subarrays.
• It sorts the subarrays using the insertion sort algorithm.
• The subarrays are chosen based on a predefined sequence of gaps.
Steps of Shell Sort
1.Define a sequence of gaps.
2.Start with the largest gap and divide the list into subarrays.
3.Sort each subarray using the insertion sort algorithm.
4.Reduce the gap and repeat steps 2 and 3 until the gap is 1.
5.Finally, perform a final insertion sort on the entire array.
Example
• Let's take an example to understand Shell Sort better.
• Consider the following unsorted array: [9, 5, 7, 3, 2, 1, 8, 4, 6].
• We'll use the sequence of gaps: [5, 3, 1].
•
Example (continued)
• With a gap of 5: [1, 5], [2, 1], [8, 4], [7, 3], [9, 6].
• Using insertion sort on each subarray:
• [1, 5] -> [1, 5]
• [2, 1] -> [1, 2]
• [8, 4] -> [4, 8]
• [7, 3] -> [3, 7]
• [9, 6] -> [6, 9]
Example (continued)
• With a gap of 3: [1, 2, 4, 3, 6, 5], [5, 7, 8, 9].
• Using insertion sort on each subarray:
• [1, 2, 4, 3, 6, 5] -> [1, 2, 3, 4, 5, 6]
• [5, 7, 8, 9] -> [5, 7, 8, 9]
• With a gap of 1, perform a final insertion sort on the entire array:
[1, 2, 3, 4, 5, 6, 7, 8, 9].
• The array is now sorted.
Time Complexity
• The time complexity of Shell Sort depends on the chosen gap
sequence.
• In the worst-case scenario, the time complexity is O(n^2).
• In the best-case scenario, it can be as low as O(n log^2 n).
Advantages and Disadvantages
• Advantages:
• Shell Sort is an in-place sorting algorithm.
• It performs well on small to medium-sized lists.
• Disadvantages:
• The choice of gap sequence affects the efficiency of the
algorithm.
• It is not stable, meaning it may change the relative order of equal
elements
Conclusion
• Shell Sort is an efficient sorting algorithm based on the insertion
sort technique.
• It divides the input into smaller subarrays and sorts them using
insertion sort.
• Although it has some limitations, Shell Sort can be a good choice
for small to medium-sized lists.

Shell sort by group .pptx kkjskanjkkanank

  • 1.
  • 2.
    OUTLINE • 1 Introductionto Shell Sort • 2 How Shell Sort Works • 3 Steps of Shell Sort • 4 Example • 5 Time Complexity of shell sort • 6 Advantages and Disadvantages • 7 Conclusion
  • 3.
    INTRODUCATION • Shell sortis an efficient sorting algorithm. • Also known as Shell's method or the diminishing increment sort. • Invented by Donald Shell in 1959. • It's an improvement over insertion sort.
  • 4.
    How Shell SortWorks • Shell Sort divides the input into smaller subarrays. • It sorts the subarrays using the insertion sort algorithm. • The subarrays are chosen based on a predefined sequence of gaps.
  • 5.
    Steps of ShellSort 1.Define a sequence of gaps. 2.Start with the largest gap and divide the list into subarrays. 3.Sort each subarray using the insertion sort algorithm. 4.Reduce the gap and repeat steps 2 and 3 until the gap is 1. 5.Finally, perform a final insertion sort on the entire array.
  • 6.
    Example • Let's takean example to understand Shell Sort better. • Consider the following unsorted array: [9, 5, 7, 3, 2, 1, 8, 4, 6]. • We'll use the sequence of gaps: [5, 3, 1]. •
  • 7.
    Example (continued) • Witha gap of 5: [1, 5], [2, 1], [8, 4], [7, 3], [9, 6]. • Using insertion sort on each subarray: • [1, 5] -> [1, 5] • [2, 1] -> [1, 2] • [8, 4] -> [4, 8] • [7, 3] -> [3, 7] • [9, 6] -> [6, 9]
  • 8.
    Example (continued) • Witha gap of 3: [1, 2, 4, 3, 6, 5], [5, 7, 8, 9]. • Using insertion sort on each subarray: • [1, 2, 4, 3, 6, 5] -> [1, 2, 3, 4, 5, 6] • [5, 7, 8, 9] -> [5, 7, 8, 9] • With a gap of 1, perform a final insertion sort on the entire array: [1, 2, 3, 4, 5, 6, 7, 8, 9]. • The array is now sorted.
  • 9.
    Time Complexity • Thetime complexity of Shell Sort depends on the chosen gap sequence. • In the worst-case scenario, the time complexity is O(n^2). • In the best-case scenario, it can be as low as O(n log^2 n).
  • 10.
    Advantages and Disadvantages •Advantages: • Shell Sort is an in-place sorting algorithm. • It performs well on small to medium-sized lists. • Disadvantages: • The choice of gap sequence affects the efficiency of the algorithm. • It is not stable, meaning it may change the relative order of equal elements
  • 11.
    Conclusion • Shell Sortis an efficient sorting algorithm based on the insertion sort technique. • It divides the input into smaller subarrays and sorts them using insertion sort. • Although it has some limitations, Shell Sort can be a good choice for small to medium-sized lists.