thorax232

Java - Gnome Sort

Nov 13th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. public static long gnomeSort(int[] list) {
  2.     int pos = 1;
  3.     int last = 0;
  4.     int temp;
  5.        
  6.     while(pos < list.length) {
  7.         if(list[pos] >= list[pos - 1]) {
  8.             if(last != 0) {
  9.                 pos = last;
  10.                 last = 0;
  11.             }
  12.             pos += 1;
  13.         } else {
  14.             temp = list[pos];
  15.             list[pos] = list[pos - 1];
  16.             list[pos - 1] = temp;
  17.            
  18.             if(pos > 1) {
  19.                 if(last == 0) {
  20.                     last = pos;
  21.                 }
  22.                 pos -= 1;
  23.             } else {
  24.                 pos += 1;
  25.             }
  26.         }
  27.     }
  28.        
  29.     return list;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment