forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18a5148
commit e15cfb2
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package sort; | ||
|
||
import static sort.SortUtils.*; | ||
|
||
/** | ||
* Implementation of gnome sort | ||
* | ||
* @author Podshivalov Nikita (https://github.com/nikitap492) | ||
* @since 2018-04-10 | ||
* | ||
**/ | ||
public class GnomeSort implements SortAlgorithm{ | ||
|
||
@Override | ||
public <T extends Comparable<T>> T[] sort(T[] arr) { | ||
int i = 1; | ||
int j = 2; | ||
while (i < arr.length){ | ||
if ( less(arr[i - 1], arr[i]) ) i = j++; | ||
else { | ||
swap(arr, i - 1, i); | ||
if (--i == 0){ i = j++; } | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Integer[] integers = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 }; | ||
String[] strings = {"c", "a", "e", "b","d", "dd","da","zz", "AA", "aa","aB","Hb", "Z"}; | ||
GnomeSort gnomeSort = new GnomeSort(); | ||
|
||
gnomeSort.sort(integers); | ||
gnomeSort.sort(strings); | ||
|
||
System.out.println("After sort : "); | ||
print(integers); | ||
print(strings); | ||
|
||
|
||
} | ||
|
||
} |