Hark data structure and algorithm practice Goblin (GNOME) sort

Source: Internet
Author: User

Algorithm description

The sort of goblin is a sort of exchange, it's an improvement of the bubble sort, I feel like a cocktail sort.

The difference is that the cocktail sort is small to large and then switched from big to small. And the Goblin sort is come up first from small to large sort, encounter exchange to again from big to small, then from small to large to sort.

As an example:

Sort the 8,6,4,5,1 in ascending order

1, 8 and 6 exchange, the result is

{6,8, 4,5,1}

2, 8 and 4 exchange, the result is

{6,4,8, 5,1}

3, 4 and 6 exchange, the result is

{4,6, 8,5,1}

4, 5 and 8 exchange, the result is

{4,6,5,8, 1}

5, 6 and 5 exchange, the result is

{4,5,6, 8,1}

6, 8 and 1 exchange, the result is

{4,5,6,1,8}

7, 6 and 1 exchange, the result is

{4,5,1,6, 8}

8, 5 and 1 exchange, the result is

{4,1,5, 6,8}

9, 4 and 1 exchange, the result is

{1,4, 5,6,8}

Code

is using the Java

Package hark.sort.exchangesort;/* * Goblin sort (Gnome sort) */public class Gnomesort {public static void main (string[] args) {int[] arr Aydata = {5, 3, 2, 4, 3, 1, 2, 1, 4, 2, 4, 21, 6, 3, 2, 1}; Gnomesortmethod (Arraydata); for (int integer:arraydata) {System.out.print (integer); System.out.print ("");}} public static void Gnomesortmethod (int[] arraydata) {int index = 0;int temp;while (Index < arraydata.length-1) {if (a Rraydata[index] <= Arraydata[index + 1]) {index++;} else {temp = Arraydata[index];arraydata[index] = Arraydata[index + 1];arraydata[index + 1] = temp;if (Index > 0) {index--;} else {index++;}}}}

  

Reference

http://blog.csdn.net/wklken/article/details/7606519

Hark data structure and algorithm practice Goblin (GNOME) sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.