Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C Algorithms – Shell Sort

By Exforsys | on September 10, 2011 |
C Algorithms

Shell sort is a generalization of the Insertion Sort, it sorts subsequences that become increasingly higher, until it is reached the value n – the total number of elements.

Each subsequence i is determined by a number h_i called increment. Also, the increments must satisfy the following condition:

h_t > h_t-1 > h_t-2 > … > h_2 > h_1

Step by step example :

Having the following list, let’s try to use shell sort to arrange the numbers from lowest to greatest:

Unsorted list: 16, 4, 3, 13, 5, 6, 8, 9, 10, 11, 12, 17, 15, 18, 19, 7, 1, 2, 14, 20

This list can be divided into 3 smaller lists:

16 4 3 13 5 6 8
9 10 11 12 17 15 18
19 7 1 2 14 20  

Now we sort each column:

First we sort the first column ( 16 9 and 19 ) to give 9 16 19

Next the second column ( 4 10 and 7 ) to give 4 7 10

Column three ( 3 11 and 1 ) to give 1 3 11

Column four ( 13 12 and 2 ) to give 2 12 13

Column five ( 5 17 and 14 ) to give 5 14 17

Column six ( 6 15 and 20 ) to give 6 15 20

and column seven ( 8 and 18 ) to give 8 18

Reassembling the array we now have:

9 4 1 2 5 6 8 16 7 3 12 14 15 18 19 10 11 13 17 20

Now, this list can be divided into 5 smaller lists:

9 4 1 2
5 6 8 16
7 3 12 14
15 18 19 10
11 13 17 20

We sort again each column:

5 3 1 2
7 4 8 10
9 6 12 14
11 13 17 16
15 18 19 20

And reassembling the list, it now looks like this:

5, 3, 1, 2, 7, 4, 8, 10, 9, 6, 12, 14, 11, 13, 17, 16, 15, 18, 19, 20

Now, this list can be divided into 10 smaller lists:

5 3
1 2
7 4
8 10
9 6
12 14
11 13
17 16
15 18
19 20

Sorting the columns again :

1 2
5 3
7 4
8 6
9 10
12 14
11 13
17 16
15 18
19 20

Reassembling we get:

1, 2, 5, 3, 7, 4, 8, 6, 9, 10, 11, 13, 12, 14, 15, 16, 17, 18, 19, 20

Most of the elements are in the right place, and the last run of the algorithm is a conventional insertion sort (this is way shell sort is considered a generalization of the insertion sort).

The generation of the increments:

The increments can be generated according to a formula, or random but it’s not recommended. Hibbard suggested a sequence 1, 3, 7, … 2 ^k – 1. The sequence 1, 4, 13, 40, 121, 364, 1093, 3280, 9841 … was recommended by Knuth in 1969. It is easy to compute (start with 1, generate the next increment by multiplying by 3 and adding 1) and leads to a relatively efficient sort, even for moderately large files.

Sample code:

  1.  #include < iostream >
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. 	int a[100];
  7. 	int n;
  8. 	int h, x, i;
  9. 	int H[100];
  10. 	int t;
  11. 	H[0]=1;
  12. 	cout << "Please insert the number of elements to be sorted: ";
  13. 	cin >> n;	// The total number of elements
  14. 	for(int i=1;i< =n/2;i++)
  15. 	{
  16. 		H[i]=H[0]++;  //Generate the increments
  17. 	}
  18. 	t=n/2;
  19. 	for(int i=0;i < n;i++)
  20. 	{
  21. 		cout << "Input " << i << " element: ";
  22. 		cin >> a[i]; // Adding the elements to the array
  23. 	}
  24. 	cout << "Unsorted list:" << endl;	// Displaying the unsorted array
  25. 	for(int i=0;i < n;i++)
  26. 	{
  27. 		cout << a[i] << " ";
  28. 	}
  29. 	for(int s=t-1;s>=0;s--)
  30. 	{
  31. 		h=H[s];		//the first increment
  32. 		for(int j=h;j < = n-1;j++)
  33. 		{
  34. 			x=a[j]; 
  35. 			i=j-h;
  36. 			while (i>=0 && x < a[i])
  37. 			{
  38. 				a[i+h]=a[i]; 
  39. 				i=i-h;
  40. 				a[i+h]=x;
  41. 			}
  42.  
  43. 		}
  44. 	}
  45. 	cout << "Sorted list:" << endl;	// Displaying the sorted array
  46. 	for(int i=0;i < n;i++)
  47. 	{
  48. 		cout << a[i] << " ";
  49. 	}
  50. 	return 0;
  51. }

Output:

Code explanation:

  1. for(int s=t-1;s>=0;s--)
  2. 	{
  3. 		h=H[s];		
  4. 		for(int j=h;j < = n-1;j++)
  5. 		{
  6. 			x=a[j]; 
  7. 			i=j-h;
  8. 			while (i>=0 && x < a[i])
  9. 			{
  10. 				a[i+h]=a[i]; 
  11. 				i=i-h;
  12. 				a[i+h]=x;
  13. 			}
  14.  
  15. 		}
  16. 	}

The variable t is equal to the number of the elements divided by 2, so the for loop starts at the highest value then it decrements by 1 until it reaches 0. The variable h takes the first increment, in the value of H[s]. At the next for loop, using the increment value, the list is parsed to check if the elements are in the right order, and while this is false, the elements are swapped.

After the first run has been completed, the increment is changed, and again the list is parsed to check the order of the elements, just like in the example from above.

Complexity:

Defining the complexity of shell sort is hard task due to the fact that besides the implementation of the algorithm, choosing the best increment is also an issue. It could be said that on the best case, the complexity could be O(n) and on worse case O(n2). Also, depending on the gap sequence, for the worst case scenario, the best complexity is O(nlog2n).

Advantages:

  • only efficient for medium size lists;
  • 5 times faster than the bubble sort and a little over twice as fast as the insertion sort, its closest competitor.

Disadvantages:

  • it is a complex algorithm and its not nearly as efficient as the merge, heap, and quick sorts.
  • is significantly slower than the merge, heap, and quick sorts;

Conclusion:

Shell sort is faster than bubble sort or insert sort, but it’s efficient only on medium size lists. It is a complex algorithm, and defining the increments play a big role in the speed of the algorithm. Knuth defined a good formula, but Robert Sedgewick came with a sequence that he claims to be 20-30% faster than the one that Knuth recommended.

« « C Algorithms – Insertion Sort
C Algorithms – Radix Sort » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • C Algorithms – Breadth-First Search (BFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Heap Sort

    September 11, 2011 - 0 Comment
  • C Algorithms – Quick Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Bubble Sort

    September 9, 2011 - 0 Comment
  • C Algorithms – Depth-First Search (DFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Comb Sort

    September 14, 2011 - 0 Comment
  • C Algorithms – Counting Sort

    September 12, 2011 - 0 Comment
  • C Algorithms – Radix Sort

    September 10, 2011 - 0 Comment
  • C Algorithms – The Problem of Sorting the Elements

    September 8, 2011 - 0 Comment
  • C Algorithms – Bucket Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Dijkstra’s Algorithm

    September 17, 2011 - 0 Comment
  • C Algorithms – Breadth-First Search (BFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Depth-First Search (DFS)

    September 16, 2011 - 0 Comment
  • C Algorithms – Topological Sort

    September 15, 2011 - 0 Comment
  • C Algorithms – Graph Theory

    September 15, 2011 - 0 Comment
  • C Algorithms – Gnome Sort

    September 14, 2011 - 0 Comment
  • C Algorithms – Comb Sort

    September 14, 2011 - 0 Comment
  • C Algorithms – Bucket Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Quick Sort

    September 13, 2011 - 0 Comment
  • C Algorithms – Counting Sort

    September 12, 2011 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C Algorithms – Dijkstra’s Algorithm
  • C Algorithms – Breadth-First Search (BFS)
  • C Algorithms – Depth-First Search (DFS)
  • C Algorithms – Topological Sort
  • C Algorithms – Graph Theory

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT