Skip to content

Commit e15cfb2

Browse files
committed
Added GnomeSort
1 parent 18a5148 commit e15cfb2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Sorts/src/sort/GnomeSort.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package sort;
2+
3+
import static sort.SortUtils.*;
4+
5+
/**
6+
* Implementation of gnome sort
7+
*
8+
* @author Podshivalov Nikita (https://github.com/nikitap492)
9+
* @since 2018-04-10
10+
*
11+
**/
12+
public class GnomeSort implements SortAlgorithm{
13+
14+
@Override
15+
public <T extends Comparable<T>> T[] sort(T[] arr) {
16+
int i = 1;
17+
int j = 2;
18+
while (i < arr.length){
19+
if ( less(arr[i - 1], arr[i]) ) i = j++;
20+
else {
21+
swap(arr, i - 1, i);
22+
if (--i == 0){ i = j++; }
23+
}
24+
}
25+
26+
return null;
27+
}
28+
29+
public static void main(String[] args) {
30+
Integer[] integers = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 };
31+
String[] strings = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"};
32+
GnomeSort gnomeSort = new GnomeSort();
33+
34+
gnomeSort.sort(integers);
35+
gnomeSort.sort(strings);
36+
37+
System.out.println("After sort : ");
38+
print(integers);
39+
print(strings);
40+
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)