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