Advertisement
Psycho_Coder

Gnome Sort in Java

Oct 10th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. package Sorts;
  2.  
  3. public class GnomeSort {
  4.  
  5. private static void gnomeSort(int[] ar) {
  6. int i = 1;
  7. int n = ar.length;
  8. while (i < n) {
  9. if (i == 0 || ar[i - 1] <= ar[i]) {
  10. i++;
  11. } else {
  12. int tmp = ar[i];
  13. ar[i] = ar[i - 1];
  14. ar[--i] = tmp;
  15. }
  16. }
  17. }
  18.  
  19. public static void main(String[] args) {
  20. int[] ar= {5, 4, 3, 2, 1};
  21. gnomeSort(ar);
  22. for (int i = 0; i < ar.length; i++) {
  23. System.out.println(ar[i]);
  24. }
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement