Advertisement
codegod313

Sort with partitittions (duplicate keys)

Sep 3rd, 2022
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. package org.example;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         List<Character> abobus = new ArrayList<>();
  9.         abobus.add('A');
  10.         abobus.add('A');
  11.         abobus.add('A');
  12.         abobus.add('C');
  13.         abobus.add('B');
  14.         abobus.add('B');
  15.         abobus.add('A');
  16.         abobus.add('A');
  17.         abobus.add('A');
  18.         abobus.add('B');
  19.         abobus.add('B');
  20.         abobus.add('C');
  21.         abobus.add('B');
  22.         abobus.add('B');
  23.  
  24.         aboba(0, abobus.size() - 1, abobus);
  25.         System.out.println(abobus);
  26.  
  27.     }
  28.  
  29.  
  30.     public static void swap(List<Character> a, int i, int j) {
  31.         Character tmp = a.get(i);
  32.         a.set(i, a.get(j));
  33.         a.set(j, tmp);
  34.     }
  35.  
  36.     public static void aboba(int low, int high, List<Character> a) {
  37.         if(low >= high){
  38.             return;
  39.         }
  40.         int lt, gt, i;
  41.         lt = low;
  42.         gt = high;
  43.         i = lt;
  44.         while (i != gt) {
  45.             if (a.get(i).equals(a.get(lt))) {
  46.                 i++;
  47.             } else {
  48.                 if (a.get(i) < a.get(lt)) {
  49.                     swap(a, lt++, i++);
  50.                 } else {
  51.                     swap(a, gt--, i);
  52.                 }
  53.             }
  54.         }
  55.         aboba(low, lt - 1, a);
  56.         aboba(gt + 1, high, a);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement