Question: The Shell sort is a variation of the bubble sort. Instead of comparing adjacent values, the Shell sort adapts a concept from the binary search

The Shell sort is a variation of the bubble sort. Instead of comparing adjacent values, the Shell sort adapts a concept from the binary search to determine a ‘gap’ across which values are compared before any swap takes place. In the first pass, the gap is half the size of the array. For each subsequent pass, the gap size is cut in half. For the final pass(es), the gap size is 1, so it would be the same as a bubble sort. The passes continue until no swaps occur.

Below is the same set of values as per the bubble sort example in Chapter 18 (p.681), showing the first pass:

9 6 8 12 3 1 7 ‐‐ size of array is 7, so gap would be 3

9 6 8 12 3 1 7 ‐‐ 9 and 12 are already in order, so no swap

9 6 8 12 3 1 7 ‐‐ 6 and 3 are not in order, so swap

9 3 8 12 6 1 7 ‐‐ 8 and 1 are not in order, so swap

9 3 1 12 6 8 7 ‐‐ 12 and 7 are not in order, so swap

9 3 8 7 6 1 12 ‐‐ end of pass 1

The pseudo‐code for the Shell sort is as follows:

gap = size / 2
do until gap <= 0    
swapflag = true    
do until swapflag is false      
  swapflag = false      
  for s = 0 to size – gap          
   if num[s] > num[s + gap]            
   swap num[s] with num[s + gap]            
   swapflag = true          
   end‐if      
  end‐for    
end‐do    
gap = gap / 2
end‐do

Create a class called ShellArray. It should create and populate an array of random integers based on a size supplied to the constructor. Implement the Shell sort as a method of this class. Create a driver class to instantiate and sort several different arrays of different sizes.

Step by Step Solution

3.54 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To solve this task we need to implement a Shell sort algorithm in Java as described encapsulated within a class named ShellArray We will also create a ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!