waltermilner

Untitled

Mar 8th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1.  
  2.   public static void main(String[] args) {
  3.  
  4.     LinkedList<Integer> list = new LinkedList<>();
  5.     list.add(10);
  6.     list.add(1);
  7.     list.add(30);
  8.     list.add(-4);
  9.     list.add(-5);
  10.     list.add(20);
  11.     list.add(2);
  12.     partition(list, 8);
  13.     for (Integer i : list) {
  14.       System.out.println(i);
  15.     }
  16.   }
  17.  
  18.   static void partition(LinkedList<Integer> list, int val) {
  19.     int start = 0;
  20.     int endIndex = list.size() - 1;
  21.     while (true) {
  22.       if (start >= endIndex) {
  23.         break;
  24.       }
  25.       while (list.get(start) < val) {
  26.         start++;
  27.       }
  28.  
  29.       while (list.get(endIndex) > val) {
  30.         endIndex--;
  31.       }
  32.  
  33.       if (start >= endIndex) {
  34.         break;
  35.       }
  36.       // swap elements at start and endPointer
  37.       Integer temp = list.get(start);
  38.       list.set(start, list.get(endIndex));
  39.       list.set(endIndex, temp);
  40.  
  41.     }
  42.   }
Advertisement
Add Comment
Please, Sign In to add comment