DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Playwright JavaScript Tutorial: A Complete Guide
  • AI Technology Is Drastically Disrupting the Background Screening Industry
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • How Web3 Is Driving Social and Financial Empowerment

Trending

  • Playwright JavaScript Tutorial: A Complete Guide
  • AI Technology Is Drastically Disrupting the Background Screening Industry
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • How Web3 Is Driving Social and Financial Empowerment
  1. DZone
  2. Coding
  3. Languages
  4. Algorithm of the Week: Shell Sort

Algorithm of the Week: Shell Sort

Stoimen Popov user avatar by
Stoimen Popov
·
Feb. 28, 12 · Interview
Like (0)
Save
Tweet
Share
28.25K Views

Join the DZone community and get the full member experience.

Join For Free

Insertion sort is a great algorithm, because it’s very intuitive and it is easy to implement, but the problem is that it makes many exchanges for each “light” element in order to put it on the right place. Thus “light” elements at the end of the list may slow down the performance of insertion sort a lot. That is why in 1959 Donald Shell proposed an algorithm that tries to overcome this problem by comparing items of the list that lie far apart.

Insertion Sort vs. Shell Sort


Insertion sort compares every single item with all the rest elements of the list in order to find its place, while Shell sort compares items that lie far apart. This means light elements move faster to the front of the list.

On the other hand, it is obvious that by comparing items that lie apart the list can’t be sorted in one pass like insertion sort. That is why on each pass we should use a fixed gap between the items, then decrease the value on every consecutive iteration.

Shell Sort


We start to compare items with a fixed gap, that becomes lesser on each iteration until it gets to 1.

However it is intuitively clear that Shell sort may need even more comparisons than insertion sort. Then why should we use it?

The thing is that insertion sort is not an effective sorting algorithm at all, but in some cases, when the list is almost sorted it can be quite useful. With Shell sort, once the list is sorted for gap = i, it is sorted for every gap = j, where j < i, and this is its main advantage.

Shell Sort Principles


Shell sort can make less exchanges than insertion sort.

How to choose gap size

A "not-so-cool" thing about Shell sort is that we have to choose “the perfect” gap sequence for our list. This is not an easy task, because it is very dependent on he input data. The good news is that there are some gap sequences proven to work well in general cases.

Shell Sequence

Donald Shell proposes a sequence that follows the formula FLOOR(N/2k), then for N = 1000, we get the following sequence: [500, 250, 125, 62, 31, 15, 7, 3, 1]

Shell Gap Sequence


Shell sequence for N=1000: (500, 250, 125, 62, 31, 15, 7, 3, 1)

Pratt Sequence

Pratt proposes another sequence that’s growing with a slower pace than the Shell’s sequence. He proposes successive numbers of the form 2p3q or [1, 2, 3, 4, 6, 8, 9, 12, ...].

Pratt Gap Sequence


Pratt sequence: (1, 2, 3, 4, 6, 8, 9, 12, ...)

Knuth Sequence

Knuth on other hand proposes his own sequence following the formula (3k – 1) / 2 or [1, 4, 14, 40, 121, ...]

Knuth Gap Sequence


Knuth sequence: (1, 4, 13, 40, 121, ...)

Of course there are many other gap sequences, proposed by various developers and researchers, but the problem is that the effectiveness of the algorithm strongly depends on the input data. But before taking a look to the complexity of Shell sort, let’s see first its implementation.

Implementation

Here’s a Shell sort implementation on PHP using the Pratt gap sequence. The thing is that for this data set other gap sequences may appear to be better solutions.

$input = array(6, 5, 3, 1, 8, 7, 2, 4);
 
function shell_sort($arr)
{
	$gaps = array(1, 2, 3, 4, 6);
	$gap  = array_pop($gaps);
	$len  = count($arr);
 
	while($gap > 0)
	{
		for($i = $gap; $i < $len; $i++) {
 
			$temp = $arr[$i];
			$j = $i;
 
			while($j >= $gap && $arr[$j - $gap] > $temp) {
				$arr[$j] = $arr[$j - $gap];
				$j -= $gap;
			}
 
			$arr[$j] = $temp;
		}
 
		$gap = array_pop($gaps);
	}
 
	return $arr;
}
 
// 1, 2, 3, 4, 5, 6, 7, 8
shell_sort($input);


It’s easy to change this code in order to work with Shell sequence.

$input = array(6, 5, 3, 1, 8, 7, 2, 4);
 
function shell_sort($arr)
{
        $len  = count($arr);
	$gap  = floor($len/2);
 
	while($gap > 0)
	{
		for($i = $gap; $i < $len; $i++) {
 
			$temp = $arr[$i];
			$j = $i;
 
			while($j >= $gap && $arr[$j - $gap] > $temp) {
				$arr[$j] = $arr[$j - $gap];
				$j -= $gap;
			}
 
			$arr[$j] = $temp;
		}
 
		$gap = floor($gap/2);
	}
 
	return $arr;
}
 
// 1, 2, 3, 4, 5, 6, 7, 8
shell_sort($input);


Complexity

Yet again we can’t determine the exact complexity of this algorithm, because it depends on the gap sequence. However we may say what is the complexity of Shell sort with the sequences of Knuth, Pratt and Donald Shell. For the Shell’s sequence the complexity is O(n2), while for the Pratt’s sequence it is O(n*log2(n)). The best approach is the Knuth sequence where the complexity is O(n3/2), as you can see on the diagram bellow.

Complexity of Shell Sort


Complexity of Shell sort with different gap sequences.

Application

Well, as insertion sort and bubble sort, Shell sort is not very effective compared to quicksort or merge sort. The good thing is that it is quite easy to implement (not easier than insertion sort), but in general it should be avoided for large data sets. Perhaps the main advantage of Shell sort is that the list can be sorted for a gap greater than 1 and thus making less exchanges than insertion sort.

Related posts:

  1. Computer Algorithms: Insertion Sort
  2. Friday Algorithms: JavaScript Bubble Sort


You are a great developer? Click here to find out how much!


Source: http://www.stoimen.com/blog/2012/02/27/computer-algorithms-shell-sort/

Sort (Unix) shell Sorting algorithm

Opinions expressed by DZone contributors are their own.

Trending

  • Playwright JavaScript Tutorial: A Complete Guide
  • AI Technology Is Drastically Disrupting the Background Screening Industry
  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • How Web3 Is Driving Social and Financial Empowerment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: