// O(n^2)
 public void sortNutsAndBoltsNaive(String[] nuts, String[] bolts, NBComparator compare) {
   for (int i = 0; i < nuts.length; i++)
     for (int j = 0; j < bolts.length; j++) {
       if (compare.cmp(nuts[i], bolts[j]) == 0) {
         String temp = bolts[j];
         bolts[j] = bolts[i];
         bolts[i] = temp;
         break;
       }
     }
 }
  private int partition(String[] target, String pivot, int l, int r, NBComparator compare) {
    int m = l;
    for (int i = l + 1; i <= r; i++) {
      int result1 = compare.cmp(target[i], pivot);
      int result2 = compare.cmp(pivot, target[i]);
      if (result1 == -1 || result2 == 1) {
        m++;
        swap(target, i, m);
      } else if (result1 == 0 || result2 == 0) {
        swap(target, i, l);
        i--;
      }
    }

    // move pivot to proper index
    swap(target, m, l);

    return m;
  }
Ejemplo n.º 3
0
 public static void main(String[] args) {
   NBComparator comp = new NBComparator();
   System.out.println(comp.cmp("abssss", "absdgs"));
 }