thorax232

Java - Odd-Even Sort

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