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 – Gnome Sort

By Exforsys | on September 14, 2011 |
C Algorithms

Gnome sort is an extremely simple sorting algorithm, very similar to insertion sort. Also, it is another comparison and exchange sort. You could say that it looks like bubble sort, and you would be right.

How it works:

In gnome sort, two adjacent elements are compared, and if they are in the wrong order, they are swapped. The comparison continues with the next element, and the same condition is checked, but the order is wrong, a swap takes place, and after the swap, the lower element is now compared to the previous element and if they are not in order, another swap takes place. Then, it continues with the comparison where it left off, after the swap, the highest element so far is compared to next, swapped if necessary and so on. Let us take a look at a example for you to understand.

Step by step example :

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

Unsorted list: 33, 98, 74, 13, 55, 20

The first step, the first two elements are compared:

33, 98, 74, 13, 55, 20

They are in the proper order, so we can go ahead with the next comparison:

33, 98, 74, 13, 55, 20

Now, the order is wrong, so the elements must swap:

33, 74, 98, 13, 55, 20

After the swap has taken place, we must compare 74 with the previous element :

33, 74, 98, 13, 55, 20

They seem to be in the proper order, so we continue from where we left off, with the head of the list 98:

33, 74, 98, 13, 55, 20

A swap is required for the elements to be in proper order:

33, 74, 13, 98, 55, 20

Now the element must percolate down, and be compared to the previous elements:

33, 74, 13, 98, 55, 20

The elements swap:

33, 13, 74, 98, 55, 20

There is one more element for 13 to be compared to:

33, 13, 74, 98, 55, 20

And again a swap is required:

13, 33, 74, 98,55, 20

We continue from where we left off, with 98, and it has to be compared to 55. The algorithm ends when there are no more elements to be compared to.

Sample code:

  1.  #include < iostream >
  2. using namespace std;
  3. void gnomeSort(int elements, int arr[]) 
  4. {
  5. 	int i = 0, temp;
  6.  
  7. 	while( i < elements ){
  8.  
  9. 		if ( i == 0 || arr [i - 1] <= arr[i] )
  10. 			i++;
  11.  
  12. 		else{
  13. 			temp = arr[i];
  14. 			arr[i] = arr[i - 1];
  15. 			arr[--i] = temp;
  16. 		}
  17.  
  18. 	}
  19.  
  20. }
  21. int main()
  22. {
  23. 	int n;
  24. 	int *a;
  25. 	cout << "Please insert the number of elements to be sorted: ";
  26. 	cin >> n;	// The total number of elements
  27. 	a = (int *)calloc(n, sizeof(int));
  28. 	for(int i=0;i< n;i++)
  29. 	{
  30.  
  31. 		cout << "Input " << i << " element: ";
  32. 		cin >>a[i]; // Adding the elements to the array
  33. 	}
  34. 	cout << "Unsorted list:" << endl;	// Displaying the unsorted array
  35. 	for(int i=0;i< n;i++)
  36. 	{
  37. 		cout << a[i] << " ";
  38. 	}
  39. 	gnomeSort(n, a);
  40. 	cout << "nSorted list:" << endl;	// Display the sorted array
  41. 	for(int i=0;i < n;i++)
  42. 	{
  43. 		cout << a[i] << " ";
  44. 	}
  45. 	return 0;
  46. }

Output:

Code explanation:

The gnomesort method takes care of the sorting. The algorithm passes through the array, and checks for order between the elements, and if they are in the wrong order, they are swapped.

Complexity:

This is not a very efficient algorithm. It’s time and space complexity is exactly that of the Bubble Sort. That is, the time complexity is O(n^2), and space complexity for in-place sorting is O(1).

Advantages:

  • is a very simple algorithm to learn;

Disadvantages:

  • the complexity is O(n^2);
  • not good on large data sets;

Conclusion:

Gnome sort is a simple algorithm, best suited for those who want to learn sorting and are at the beginning of algorithms.

« « C Algorithms – Comb Sort
C Algorithms – Graph Theory » »

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 – Insertion Sort

    September 9, 2011 - 0 Comment
  • C Algorithms – Selection Sort

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

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

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

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

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

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

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

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

    September 10, 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 – 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
  • C Algorithms – Merge 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