Skip to content

Instantly share code, notes, and snippets.

@AnimeshShaw
Created September 14, 2013 18:10
Show Gist options
  • Save AnimeshShaw/6564223 to your computer and use it in GitHub Desktop.
Save AnimeshShaw/6564223 to your computer and use it in GitHub Desktop.
Gnome Sort implemented in Java
package Sorts;
public class GnomeSort {
private static void gnomeSort(int[] ar) {
int i = 1;
int n = ar.length;
while (i < n) {
if (i == 0 || ar[i - 1] <= ar[i]) {
i++;
} else {
int tmp = ar[i];
ar[i] = ar[i - 1];
ar[--i] = tmp;
}
}
}
public static void main(String[] args) {
int[] ar= {5, 4, 3, 2, 1};
gnomeSort(ar);
for (int i = 0; i < ar.length; i++) {
System.out.println(ar[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment